Search code examples
computer-science

Semantics of Decimal Digit Natural Numerals


The following example is in a book on Software Engineering. I am failing to understand how this morphism can convert a decimal numeral to the equivalent natural number. Doesn't it just sum all of the digits?


Let 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 "somehow be" the natural numbers corresponding, left to right, to the decimal digits zero, one, two, three, four, five, six, seven, eight, nine; then

type
    <NatNum> ::= <DecDig> | <DecDig> <NatNum>
    <DecDig> ::= zero | one | two | three | ... | nine


value
    M: <NatNum> -> Num
    M(d,n)≡10*M(d)+M(n)
    M(d)≡case d of zero->0,one->1,...,nine->9 end

informally explicates the meaning of a natural number numeral


The morphism distinguishes between a natural number that is just a decimal, and a natural number that is a composite of a decimal and a natural number. Thank you for your help.


Solution

  • This is effectively:

    n = 0
    for digit in number:
        n = n * 10
        n = n + digit
    return n