doc-src/IsarAdvanced/Codegen/Thy/examples/pick1.ML
author haftmann
Fri, 02 Mar 2007 15:43:17 +0100
changeset 22386 4ebe883b02ff
parent 21994 dfa5133dbe73
child 22751 1bfd75c1f232
permissions -rw-r--r--
new code theorems
haftmann@21147
     1
structure ROOT = 
haftmann@21147
     2
struct
haftmann@21147
     3
haftmann@21190
     4
structure Nat = 
haftmann@21147
     5
struct
haftmann@21147
     6
haftmann@21190
     7
datatype nat = Zero_nat | Suc of nat;
haftmann@21147
     8
haftmann@22386
     9
fun less_nat n (Suc m) = less_eq_nat n m
haftmann@21994
    10
  | less_nat n Zero_nat = false
haftmann@22386
    11
and less_eq_nat (Suc n) m = less_nat n m
haftmann@22386
    12
  | less_eq_nat Zero_nat m = true;
haftmann@21147
    13
haftmann@21994
    14
fun minus_nat (Suc m) (Suc n) = minus_nat m n
haftmann@21994
    15
  | minus_nat Zero_nat n = Zero_nat
haftmann@21994
    16
  | minus_nat y Zero_nat = y;
haftmann@21147
    17
haftmann@21190
    18
end; (*struct Nat*)
haftmann@21147
    19
haftmann@21147
    20
structure Codegen = 
haftmann@21147
    21
struct
haftmann@21147
    22
haftmann@21147
    23
fun pick ((k, v) :: xs) n =
haftmann@21994
    24
  (if Nat.less_nat n k then v else pick xs (Nat.minus_nat n k))
haftmann@21994
    25
  | pick (x :: xs) n =
haftmann@21994
    26
    let
haftmann@21994
    27
      val (k, v) = x;
haftmann@21994
    28
    in
haftmann@21994
    29
      (if Nat.less_nat n k then v else pick xs (Nat.minus_nat n k))
haftmann@21994
    30
    end;
haftmann@21147
    31
haftmann@21147
    32
end; (*struct Codegen*)
haftmann@21147
    33
haftmann@21147
    34
end; (*struct ROOT*)