doc-src/Codegen/Thy/examples/example.ML
author paulson
Mon, 21 Dec 2009 16:50:28 +0000
changeset 34156 3a7937841585
parent 33707 68841fb382e0
child 37610 1b09880d9734
permissions -rw-r--r--
Changes in generated code, apparently caused by changes to the code generation system itself.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34156
3a7937841585 Changes in generated code, apparently caused by changes to the code generation system itself.
paulson
parents: 33707
diff changeset
     1
structure Example : sig
3a7937841585 Changes in generated code, apparently caused by changes to the code generation system itself.
paulson
parents: 33707
diff changeset
     2
  val foldl : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a
3a7937841585 Changes in generated code, apparently caused by changes to the code generation system itself.
paulson
parents: 33707
diff changeset
     3
  val rev : 'a list -> 'a list
3a7937841585 Changes in generated code, apparently caused by changes to the code generation system itself.
paulson
parents: 33707
diff changeset
     4
  val list_case : 'a -> ('b -> 'b list -> 'a) -> 'b list -> 'a
3a7937841585 Changes in generated code, apparently caused by changes to the code generation system itself.
paulson
parents: 33707
diff changeset
     5
  datatype 'a queue = AQueue of 'a list * 'a list
3a7937841585 Changes in generated code, apparently caused by changes to the code generation system itself.
paulson
parents: 33707
diff changeset
     6
  val empty : 'a queue
3a7937841585 Changes in generated code, apparently caused by changes to the code generation system itself.
paulson
parents: 33707
diff changeset
     7
  val dequeue : 'a queue -> 'a option * 'a queue
3a7937841585 Changes in generated code, apparently caused by changes to the code generation system itself.
paulson
parents: 33707
diff changeset
     8
  val enqueue : 'a -> 'a queue -> 'a queue
3a7937841585 Changes in generated code, apparently caused by changes to the code generation system itself.
paulson
parents: 33707
diff changeset
     9
end = struct
28421
05d202350b8d reorganized examples
haftmann
parents:
diff changeset
    10
05d202350b8d reorganized examples
haftmann
parents:
diff changeset
    11
fun foldl f a [] = a
05d202350b8d reorganized examples
haftmann
parents:
diff changeset
    12
  | foldl f a (x :: xs) = foldl f (f a x) xs;
05d202350b8d reorganized examples
haftmann
parents:
diff changeset
    13
05d202350b8d reorganized examples
haftmann
parents:
diff changeset
    14
fun rev xs = foldl (fn xsa => fn x => x :: xsa) [] xs;
05d202350b8d reorganized examples
haftmann
parents:
diff changeset
    15
05d202350b8d reorganized examples
haftmann
parents:
diff changeset
    16
fun list_case f1 f2 (a :: lista) = f2 a lista
05d202350b8d reorganized examples
haftmann
parents:
diff changeset
    17
  | list_case f1 f2 [] = f1;
05d202350b8d reorganized examples
haftmann
parents:
diff changeset
    18
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29297
diff changeset
    19
datatype 'a queue = AQueue of 'a list * 'a list;
28421
05d202350b8d reorganized examples
haftmann
parents:
diff changeset
    20
33707
68841fb382e0 dropped obsolete documentation; updated generated sources
haftmann
parents: 30226
diff changeset
    21
val empty : 'a queue = AQueue ([], []);
28421
05d202350b8d reorganized examples
haftmann
parents:
diff changeset
    22
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29297
diff changeset
    23
fun dequeue (AQueue ([], [])) = (NONE, AQueue ([], []))
6df726203e39 proper datatype abstraction example
haftmann
parents: 29297
diff changeset
    24
  | dequeue (AQueue (xs, y :: ys)) = (SOME y, AQueue (xs, ys))
6df726203e39 proper datatype abstraction example
haftmann
parents: 29297
diff changeset
    25
  | dequeue (AQueue (v :: va, [])) =
28421
05d202350b8d reorganized examples
haftmann
parents:
diff changeset
    26
    let
05d202350b8d reorganized examples
haftmann
parents:
diff changeset
    27
      val y :: ys = rev (v :: va);
05d202350b8d reorganized examples
haftmann
parents:
diff changeset
    28
    in
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29297
diff changeset
    29
      (SOME y, AQueue ([], ys))
28421
05d202350b8d reorganized examples
haftmann
parents:
diff changeset
    30
    end;
05d202350b8d reorganized examples
haftmann
parents:
diff changeset
    31
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29297
diff changeset
    32
fun enqueue x (AQueue (xs, ys)) = AQueue (x :: xs, ys);
28421
05d202350b8d reorganized examples
haftmann
parents:
diff changeset
    33
05d202350b8d reorganized examples
haftmann
parents:
diff changeset
    34
end; (*struct Example*)