TFL/test.sml
author oheimb
Wed, 27 Nov 1996 20:36:33 +0100
changeset 2263 c741309167bf
parent 2112 3902e9af752f
permissions -rw-r--r--
moved split_tac
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     1
fun cread thy s = read_cterm (sign_of thy) (s, (TVar(("DUMMY",0),[])));
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     2
fun read thy = term_of o cread thy;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     3
fun Term s = read WF1.thy s;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     4
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     5
fun Rfunc thy R eqs =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     6
   let val {induction,rules,theory,tcs} = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     7
              timeit(fn () => Tfl.Rfunction thy (read thy R) (read thy eqs))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     8
   in {induction=induction, rules=rules, theory=theory, 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     9
       tcs = map (cterm_of (sign_of theory)) tcs}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    10
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    11
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    12
val Rfunction = Rfunc WF1.thy;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    13
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    14
fun function tm = timeit (fn () => Tfl.function WF1.thy (Term tm));
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    15
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    16
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    17
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    18
 * Factorial. Notice how functions without pattern matching are often harder 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    19
 * to deal with than those with! Unfortunately, not all functions can be 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    20
 * described purely by pattern matching, e.g., "variant" as below.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    21
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    22
function "fact x = (if (x = 0) then Suc 0 else x * fact (x - Suc 0))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    23
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    24
Rfunction"pred_nat"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    25
         "fact x = (if (x = 0) then Suc 0 else x * fact (x - Suc 0))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    26
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    27
function "(Fact 0 = (Suc 0)) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    28
     \    (Fact (Suc x) = (Fact x * Suc x))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    29
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    30
Rfunction "pred_nat"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    31
          "(Fact 0 = (Suc 0)) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    32
      \    (Fact (Suc x) = (Fact x * Suc x))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    33
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    34
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    35
 * Fibonacci.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    36
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    37
function "(Fib 0 = (Suc 0)) &  \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    38
     \    (Fib (Suc 0) = (Suc 0)) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    39
     \    (Fib (Suc(Suc x)) = (Fib x + Fib (Suc x)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    40
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    41
(* "<" doesn't currently work smoothly *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    42
Rfunction"{p::(nat*nat). fst p < snd p}"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    43
         "(Fib 0 = (Suc 0)) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    44
     \    (Fib (Suc 0) = (Suc 0)) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    45
     \    (Fib (Suc(Suc x)) = (Fib x + Fib (Suc x)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    46
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    47
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    48
(* "trancl pred" means "<" and works better *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    49
Rfunction"trancl pred_nat"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    50
         "(Fib 0 = (Suc 0)) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    51
     \    (Fib (Suc 0) = (Suc 0)) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    52
     \    (Fib (Suc(Suc x)) = (Fib x + Fib (Suc x)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    53
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    54
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    55
 * Ackermann.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    56
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    57
Rfunction"pred_nat ** pred_nat"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    58
         "(Ack (0,n) =  (n + Suc 0)) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    59
    \    (Ack (Suc m,0) = (Ack (m, Suc 0))) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    60
    \    (Ack (Suc m, Suc n) = Ack (m, Ack (Suc m, n)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    61
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    62
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    63
 * Almost primitive recursion. 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    64
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    65
function"(map2(f, [], L) = []) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    66
    \    (map2(f, h#t, []) = []) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    67
    \    (map2(f, h1#t1, h2#t2) = f h1 h2 # map2 (f, t1, t2))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    68
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    69
(* Swap arguments *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    70
function"(map2(([],L), f) = []) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    71
    \    (map2((h#t, []), f) = []) &  \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    72
    \    (map2((h1#t1, h2#t2), f) = f h1 h2 # map2((t1,t2),f))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    73
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    74
Rfunction
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    75
   "measure((length o fst o snd)::('a=>'b=>'c)*'a list*'b list => nat)"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    76
    "(map2((f::'a=>'b=>'c), ([]::'a list), (L::'b list)) = [])  & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    77
\    (map2(f, h#t, []) = []) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    78
\    (map2(f, h1#t1, h2#t2) = f h1 h2 # map2 (f, t1, t2))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    79
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    80
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    81
 * Relation "R" holds stepwise in a list
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    82
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    83
function"(finiteRchain ((R::'a=>'a=>bool),  ([]::'a list)) = True) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    84
    \    (finiteRchain (R, [x]) = True) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    85
    \    (finiteRchain (R, x#y#rst) = (R x y & finiteRchain(R, y#rst)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    86
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    87
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    88
Rfunction"measure ((length o snd)::('a=>'a=>bool) * 'a list => nat)"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    89
         "(finiteRchain((R::'a=>'a=>bool),  ([]::'a list)) = True) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    90
     \    (finiteRchain(R, [x]) = True) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    91
     \    (finiteRchain(R, x#y#rst) = (R x y & finiteRchain(R, y#rst)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    92
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    93
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    94
 * Quicksort.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    95
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    96
function"(qsort(ord,  []) = []) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    97
    \    (qsort(ord, x#rst) = \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    98
    \       qsort(ord,filter(not o ord x) rst) \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    99
    \       @[x]@      \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   100
    \       qsort(ord,filter(ord x) rst))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   101
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   102
Rfunction"measure ((length o snd)::('a=>'a=>bool) * 'a list => nat)"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   103
         "(qsort((ord::'a=>'a=>bool),  ([]::'a list))  = []) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   104
     \    (qsort(ord, x#rst) = \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   105
     \       qsort(ord,filter(not o ord x) rst) \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   106
     \       @[x]@  \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   107
     \       qsort(ord,filter(ord x) rst))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   108
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   109
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   110
 * Variant.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   111
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   112
function"variant(x, L) = (if (x mem L) then variant(Suc x, L) else x)";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   113
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   114
Rfunction
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   115
 "measure(%(p::nat*nat list). length(filter(%y. fst(p) <= y) (snd p)))"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   116
 "variant(x, L) = (if (x mem L) then variant(Suc x, L) else x)";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   117
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   118
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   119
 * Euclid's algorithm
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   120
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   121
function"(gcd ((0::nat),(y::nat)) = y) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   122
    \    (gcd (Suc x, 0) = Suc x) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   123
    \    (gcd (Suc x, Suc y) =      \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   124
    \        (if (y <= x) then gcd(x - y, Suc y) \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   125
    \                     else gcd(Suc x, y - x)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   126
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   127
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   128
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   129
 * Wrong answer because Isabelle rewriter (going bottom-up) attempts to
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   130
 * apply congruence rule for split to "split" but can't because split is only
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   131
 * partly applied. It then fails, instead of just not doing the rewrite.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   132
 * Tobias has said he'll fix it.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   133
 *
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   134
 * ... July 96 ... seems to have been fixed.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   135
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   136
 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   137
Rfunction"measure (split (op+) ::nat*nat=>nat)"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   138
         "(gcd ((0::nat),(y::nat)) = y) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   139
        \ (gcd (Suc x, 0) = Suc x) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   140
        \ (gcd (Suc x, Suc y) = \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   141
        \     (if (y <= x) then gcd(x - y, Suc y) \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   142
        \                  else gcd(Suc x, y - x)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   143
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   144
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   145
 * A simple nested function.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   146
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   147
Rfunction"trancl pred_nat"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   148
         "(g 0 = 0) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   149
        \ (g(Suc x) = g(g x))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   150
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   151
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   152
 * A clever division algorithm. Primitive recursive.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   153
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   154
function"(Div(0,x) = (0,0)) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   155
       \ (Div(Suc x, y) =     \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   156
       \     (let (q,r) = Div(x,y) \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   157
       \      in if (y <= Suc r) then (Suc q,0) else (q, Suc r)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   158
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   159
Rfunction"inv_image pred_nat (fst::nat*nat=>nat)"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   160
         "(Div(0,x) = (0,0)) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   161
        \ (Div(Suc x, y) =     \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   162
        \    (let q = fst(Div(x,y)); \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   163
        \         r = snd(Div(x,y))  \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   164
        \     in                     \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   165
        \     if (y <= Suc r) then (Suc q,0) else (q, Suc r)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   166
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   167
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   168
 * Testing nested contexts.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   169
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   170
function"(f(0,x) = (0,0)) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   171
       \ (f(Suc x, y) = \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   172
       \     (let z = x \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   173
       \      in        \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   174
       \      if (0<y) then (0,0) else f(z,y)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   175
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   176
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   177
function"(f(0,x) = (0,0)) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   178
       \ (f(Suc x, y) =     \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   179
       \      (if y = x     \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   180
       \       then (if (0<y) then (0,0) else f(x,y)) \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   181
       \       else (x,y)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   182
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   183
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   184
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   185
 * Naming trickery in lets.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   186
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   187
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   188
(* No trick *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   189
function "(test(x, []) = x) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   190
        \ (test(x,h#t) = (let y = Suc x in test(y,t)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   191
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   192
(* Trick *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   193
function"(test(x, []) = x) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   194
       \ (test(x,h#t) =      \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   195
       \     (let x = Suc x  \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   196
       \      in             \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   197
       \      test(x,t)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   198
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   199
(* Tricky naming, plus nested contexts *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   200
function "vary(x, L) =  \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   201
        \   (if (x mem L) \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   202
        \    then (let x = Suc x \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   203
        \          in vary(x,L)) \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   204
        \    else x)";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   205
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   206
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   207
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   208
 * Handling paired lets of various kinds
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   209
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   210
function
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   211
    "(Fib(0) = Suc 0) &  \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   212
   \ (Fib (Suc 0) = Suc 0) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   213
   \ (Fib (Suc (Suc n)) =    \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   214
   \     (let (x,y) = (Fib (Suc n), Fib n) in x+y))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   215
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   216
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   217
function
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   218
   "(qsort((ord::'a=>'a=>bool),  ([]::'a list))   = []) &  \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   219
  \ (qsort(ord, x#rst) = \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   220
  \     (let (L1,L2) = (filter(not o ord x) rst, \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   221
  \                     filter (ord x) rst) \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   222
  \      in  \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   223
  \      qsort(ord,L1)@[x]@qsort(ord,L2)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   224
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   225
function"(qsort((ord::'a=>'a=>bool),  ([]::'a list))   = []) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   226
       \ (qsort(ord, x#rst) = \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   227
       \    (let (L1,L2,P) = (filter(not o ord x) rst, \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   228
       \                      filter (ord x) rst, x)   \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   229
       \     in \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   230
       \     qsort(ord,L1)@[x]@qsort(ord,L2)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   231
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   232
function"(qsort((ord::'a=>'a=>bool),  ([]::'a list))   = []) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   233
       \ (qsort(ord, x#rst) = \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   234
       \     (let (L1,L2) = (filter(not o ord x) rst, \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   235
       \                     filter (ord x) rst);     \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   236
       \            (p,q) = (x,rst) \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   237
       \      in \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   238
       \      qsort(ord,L1)@[x]@qsort(ord,L2)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   239
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   240
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   241
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   242
 * A biggish function
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   243
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   244
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   245
function"(acc1(A,[],s,xss,zs,xs) = \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   246
\              (if xs=[] then (xss, zs) \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   247
\                        else acc1(A, zs,s,(xss @ [xs]),[],[]))) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   248
\         (acc1(A,(y#ys), s, xss, zs, xs) = \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   249
\              (let s' = s; \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   250
\                  zs' = (if fst A s' then [] else zs@[y]); \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   251
\                  xs' = (if fst A s' then xs@zs@[y] else xs) \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   252
\               in  \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   253
\               acc1(A, ys, s', xss, zs', xs')))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   254
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   255
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   256
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   257
 * Nested, with context.
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   258
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   259
Rfunction"pred_nat"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   260
  "(k 0 = 0) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   261
\  (k (Suc n) = (let x = k (Suc 0)  \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   262
\                in if (0=Suc 0) then k (Suc(Suc 0)) else n))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   263
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   264
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   265
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   266
 * A function that partitions a list into two around a predicate "P".
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   267
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   268
val {theory,induction,rules,tcs} = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   269
  Rfunction
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   270
   "inv_image pred_list \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   271
    \  ((fst o snd)::('a=>bool)*'a list*'a list*'a list => 'a list)"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   272
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   273
  "(part(P::'a=>bool, [], l1,l2) = (l1,l2)) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   274
\  (part(P, h#rst, l1,l2) = \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   275
\       (if P h then part(P,rst, h#l1,  l2) \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   276
\               else part(P,rst,  l1,  h#l2)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   277
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   278
  
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   279
(*---------------------------------------------------------------------------
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   280
 * Another quicksort. 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   281
 *---------------------------------------------------------------------------*)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   282
Rfunc theory "measure ((length o snd)::('a=>'a=>bool) * 'a list => nat)"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   283
 "(fqsort(ord,[]) = []) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   284
\ (fqsort(ord, x#rst) = \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   285
 \   (let less = fst(part((%y. ord y x), rst,([],[]))); \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   286
  \       more = snd(part((%y. ord y x), rst,([],[]))) \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   287
   \  in \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   288
    \ fqsort(ord,less)@[x]@fqsort(ord,more)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   289
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   290
Rfunc theory "measure ((length o snd)::('a=>'a=>bool) * 'a list => nat)"
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   291
 "(fqsort(ord,[]) = []) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   292
\  (fqsort(ord, x#rst) = \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   293
 \   (let (less,more) = part((%y. ord y x), rst,([],[])) \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   294
  \   in \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   295
   \  fqsort(ord,less)@[x]@fqsort(ord,more)))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   296
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   297
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   298
(* Should fail on repeated variables. *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   299
function"(And(x,[]) = x) & \
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   300
      \  (And(y, y#t) = And(y, t))";
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
   301