doc-src/IsarAdvanced/Codegen/Thy/examples/pick1.ML
author wenzelm
Fri, 03 Aug 2007 22:33:03 +0200
changeset 24148 2d4ee876c215
parent 23850 f1434532a562
child 24193 926dde4d96de
permissions -rw-r--r--
named some CRITICAL sections;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21190
08ec81dfc7fb (continued)
haftmann
parents: 21172
diff changeset
     1
structure Nat = 
21147
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
datatype nat = Zero_nat | Suc of nat;
21147
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
     5
22386
4ebe883b02ff new code theorems
haftmann
parents: 21994
diff changeset
     6
fun less_nat n (Suc m) = less_eq_nat n m
21994
dfa5133dbe73 updated manual
haftmann
parents: 21993
diff changeset
     7
  | less_nat n Zero_nat = false
22386
4ebe883b02ff new code theorems
haftmann
parents: 21994
diff changeset
     8
and less_eq_nat (Suc n) m = less_nat n m
4ebe883b02ff new code theorems
haftmann
parents: 21994
diff changeset
     9
  | less_eq_nat Zero_nat m = true;
21147
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    10
21994
dfa5133dbe73 updated manual
haftmann
parents: 21993
diff changeset
    11
fun minus_nat (Suc m) (Suc n) = minus_nat m n
dfa5133dbe73 updated manual
haftmann
parents: 21993
diff changeset
    12
  | minus_nat Zero_nat n = Zero_nat
22751
1bfd75c1f232 updated
haftmann
parents: 22386
diff changeset
    13
  | minus_nat m Zero_nat = m;
21147
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    14
21190
08ec81dfc7fb (continued)
haftmann
parents: 21172
diff changeset
    15
end; (*struct Nat*)
21147
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    16
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    17
structure Codegen = 
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    18
struct
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    19
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    20
fun pick ((k, v) :: xs) n =
21994
dfa5133dbe73 updated manual
haftmann
parents: 21993
diff changeset
    21
  (if Nat.less_nat n k then v else pick xs (Nat.minus_nat n k))
dfa5133dbe73 updated manual
haftmann
parents: 21993
diff changeset
    22
  | pick (x :: xs) n =
dfa5133dbe73 updated manual
haftmann
parents: 21993
diff changeset
    23
    let
dfa5133dbe73 updated manual
haftmann
parents: 21993
diff changeset
    24
      val (k, v) = x;
dfa5133dbe73 updated manual
haftmann
parents: 21993
diff changeset
    25
    in
dfa5133dbe73 updated manual
haftmann
parents: 21993
diff changeset
    26
      (if Nat.less_nat n k then v else pick xs (Nat.minus_nat n k))
dfa5133dbe73 updated manual
haftmann
parents: 21993
diff changeset
    27
    end;
21147
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    28
737a94f047e3 continued tutorial
haftmann
parents:
diff changeset
    29
end; (*struct Codegen*)