doc-src/IsarAdvanced/Codegen/Thy/examples/pick1.ML
author wenzelm
Tue Oct 16 17:07:40 2007 +0200 (2007-10-16)
changeset 25056 743f3603ba8b
parent 24421 acfb2413faa3
child 26318 967323f93c67
permissions -rw-r--r--
updated;
haftmann@24193
     1
structure HOL = 
haftmann@24193
     2
struct
haftmann@24193
     3
haftmann@24193
     4
fun leta s f = f s;
haftmann@24193
     5
haftmann@24193
     6
end; (*struct HOL*)
haftmann@24193
     7
haftmann@21190
     8
structure Nat = 
haftmann@21147
     9
struct
haftmann@21147
    10
haftmann@24421
    11
datatype nat = Suc of nat | Zero_nat;
haftmann@21147
    12
haftmann@22386
    13
fun less_nat n (Suc m) = less_eq_nat n m
haftmann@21994
    14
  | less_nat n Zero_nat = false
haftmann@22386
    15
and less_eq_nat (Suc n) m = less_nat n m
haftmann@22386
    16
  | less_eq_nat Zero_nat m = true;
haftmann@21147
    17
haftmann@21994
    18
fun minus_nat (Suc m) (Suc n) = minus_nat m n
haftmann@21994
    19
  | minus_nat Zero_nat n = Zero_nat
haftmann@22751
    20
  | minus_nat m Zero_nat = m;
haftmann@21147
    21
wenzelm@25056
    22
end; (*struct Nat*)
haftmann@24193
    23
wenzelm@25056
    24
structure Product_Type = 
wenzelm@25056
    25
struct
wenzelm@25056
    26
wenzelm@25056
    27
fun split c (a, b) = c a b;
wenzelm@25056
    28
wenzelm@25056
    29
end; (*struct Product_Type*)
haftmann@21147
    30
haftmann@21147
    31
structure Codegen = 
haftmann@21147
    32
struct
haftmann@21147
    33
haftmann@21147
    34
fun pick ((k, v) :: xs) n =
haftmann@21994
    35
  (if Nat.less_nat n k then v else pick xs (Nat.minus_nat n k))
haftmann@21994
    36
  | pick (x :: xs) n =
haftmann@21994
    37
    let
wenzelm@25056
    38
      val a = x;
wenzelm@25056
    39
      val (k, v) = a;
haftmann@21994
    40
    in
haftmann@21994
    41
      (if Nat.less_nat n k then v else pick xs (Nat.minus_nat n k))
haftmann@21994
    42
    end;
haftmann@21147
    43
haftmann@21147
    44
end; (*struct Codegen*)