doc-src/IsarAdvanced/Codegen/Thy/examples/pick2.ML
author haftmann
Thu, 04 Jan 2007 17:17:48 +0100
changeset 21994 dfa5133dbe73
parent 21993 4b802a9e0738
child 22386 4ebe883b02ff
permissions -rw-r--r--
updated manual
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21147
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
     1
structure ROOT = 
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
     2
struct
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
     3
21190
08ec81dfc7fb (continued)
haftmann
parents: 21172
diff changeset
     4
structure Nat = 
21147
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
     5
struct
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
     6
21190
08ec81dfc7fb (continued)
haftmann
parents: 21172
diff changeset
     7
datatype nat = Zero_nat | Suc of nat;
21147
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
     8
21994
dfa5133dbe73 updated manual
haftmann
parents: 21993
diff changeset
     9
fun less_nat Zero_nat (Suc n) = true
dfa5133dbe73 updated manual
haftmann
parents: 21993
diff changeset
    10
  | less_nat n Zero_nat = false
dfa5133dbe73 updated manual
haftmann
parents: 21993
diff changeset
    11
  | less_nat (Suc m) (Suc n) = less_nat m n;
21147
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    12
21994
dfa5133dbe73 updated manual
haftmann
parents: 21993
diff changeset
    13
fun minus_nat (Suc m) (Suc n) = minus_nat m n
dfa5133dbe73 updated manual
haftmann
parents: 21993
diff changeset
    14
  | minus_nat Zero_nat n = Zero_nat
dfa5133dbe73 updated manual
haftmann
parents: 21993
diff changeset
    15
  | minus_nat y Zero_nat = y;
21147
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    16
21190
08ec81dfc7fb (continued)
haftmann
parents: 21172
diff changeset
    17
end; (*struct Nat*)
21147
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    18
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    19
structure Codegen = 
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    20
struct
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    21
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    22
fun pick ((k, v) :: xs) n =
21190
08ec81dfc7fb (continued)
haftmann
parents: 21172
diff changeset
    23
  (if Nat.less_nat n k then v else pick xs (Nat.minus_nat n k));
21147
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    24
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    25
end; (*struct Codegen*)
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    26
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    27
end; (*struct ROOT*)