| author | wenzelm | 
| Tue, 30 Sep 2008 22:02:44 +0200 | |
| changeset 28429 | 3d5fbf964a7e | 
| parent 28083 | 103d9282a946 | 
| child 28965 | 1de908189869 | 
| permissions | -rw-r--r-- | 
| 24584 | 1 | (* Title: HOL/Tools/function_package/fundef_lib.ML | 
| 19564 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 2 | ID: $Id$ | 
| 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 3 | Author: Alexander Krauss, TU Muenchen | 
| 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 4 | |
| 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 5 | A package for general recursive function definitions. | 
| 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 6 | Some fairly general functions that should probably go somewhere else... | 
| 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 7 | *) | 
| 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 8 | |
| 21051 
c49467a9c1e1
Switched function package to use the new package for inductive predicates.
 krauss parents: 
20876diff
changeset | 9 | structure FundefLib = struct | 
| 
c49467a9c1e1
Switched function package to use the new package for inductive predicates.
 krauss parents: 
20876diff
changeset | 10 | |
| 22166 | 11 | fun map_option f NONE = NONE | 
| 12 | | map_option f (SOME x) = SOME (f x); | |
| 13 | ||
| 14 | fun fold_option f NONE y = y | |
| 15 | | fold_option f (SOME x) y = f x y; | |
| 16 | ||
| 17 | fun fold_map_option f NONE y = (NONE, y) | |
| 18 | | fold_map_option f (SOME x) y = apfst SOME (f x y); | |
| 19 | ||
| 23216 | 20 | (* Ex: "The variable" ^ plural " is" "s are" vs *) | 
| 21 | fun plural sg pl [x] = sg | |
| 22 | | plural sg pl _ = pl | |
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 23 | |
| 22166 | 24 | (* lambda-abstracts over an arbitrarily nested tuple | 
| 25 | ==> hologic.ML? *) | |
| 19564 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 26 | fun tupled_lambda vars t = | 
| 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 27 | case vars of | 
| 21237 | 28 | (Free v) => lambda (Free v) t | 
| 21188 | 29 | | (Var v) => lambda (Var v) t | 
| 30 |     | (Const ("Pair", Type ("fun", [Ta, Type ("fun", [Tb, _])]))) $ us $ vs =>  
 | |
| 21237 | 31 | (HOLogic.split_const (Ta,Tb, fastype_of t)) $ (tupled_lambda us (tupled_lambda vs t)) | 
| 21188 | 32 | | _ => raise Match | 
| 33 | ||
| 34 | ||
| 19564 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 35 | fun dest_all (Const ("all", _) $ Abs (a as (_,T,_))) =
 | 
| 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 36 | let | 
| 21237 | 37 | val (n, body) = Term.dest_abs a | 
| 19564 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 38 | in | 
| 21237 | 39 | (Free (n, T), body) | 
| 19564 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 40 | end | 
| 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 41 | | dest_all _ = raise Match | 
| 21188 | 42 | |
| 19564 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 43 | |
| 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 44 | (* Removes all quantifiers from a term, replacing bound variables by frees. *) | 
| 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 45 | fun dest_all_all (t as (Const ("all",_) $ _)) = 
 | 
| 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 46 | let | 
| 21319 
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
 krauss parents: 
21237diff
changeset | 47 | val (v,b) = dest_all t | 
| 
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
 krauss parents: 
21237diff
changeset | 48 | val (vs, b') = dest_all_all b | 
| 19564 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 49 | in | 
| 21319 
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
 krauss parents: 
21237diff
changeset | 50 | (v :: vs, b') | 
| 19564 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 51 | end | 
| 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 52 | | dest_all_all t = ([],t) | 
| 21319 
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
 krauss parents: 
21237diff
changeset | 53 | |
| 19564 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 54 | |
| 21319 
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
 krauss parents: 
21237diff
changeset | 55 | (* FIXME: similar to Variable.focus *) | 
| 19922 | 56 | fun dest_all_all_ctx ctx (Const ("all", _) $ Abs (a as (n,T,b))) =
 | 
| 57 | let | |
| 20154 
c709a29f1363
renamed Variable.rename_wrt to Variable.variant_frees;
 wenzelm parents: 
19922diff
changeset | 58 | val [(n', _)] = Variable.variant_frees ctx [] [(n,T)] | 
| 28083 
103d9282a946
explicit type Name.binding for higher-specification elements;
 wenzelm parents: 
27792diff
changeset | 59 | val (_, ctx') = ProofContext.add_fixes_i [(Name.binding n', SOME T, NoSyn)] ctx | 
| 19922 | 60 | |
| 61 | val (n'', body) = Term.dest_abs (n', T, b) | |
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
21319diff
changeset | 62 | val _ = (n' = n'') orelse error "dest_all_ctx" | 
| 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
21319diff
changeset | 63 | (* Note: We assume that n' does not occur in the body. Otherwise it would be fixed. *) | 
| 19922 | 64 | |
| 65 | val (ctx'', vs, bd) = dest_all_all_ctx ctx' body | |
| 66 | in | |
| 21237 | 67 | (ctx'', (n', T) :: vs, bd) | 
| 19922 | 68 | end | 
| 69 | | dest_all_all_ctx ctx t = | |
| 70 | (ctx, [], t) | |
| 71 | ||
| 72 | ||
| 19564 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 73 | fun map3 _ [] [] [] = [] | 
| 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 74 | | map3 f (x :: xs) (y :: ys) (z :: zs) = f x y z :: map3 f xs ys zs | 
| 19841 | 75 | | map3 _ _ _ _ = raise Library.UnequalLengths; | 
| 19564 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 76 | |
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 77 | fun map4 _ [] [] [] [] = [] | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 78 | | map4 f (x :: xs) (y :: ys) (z :: zs) (u :: us) = f x y z u :: map4 f xs ys zs us | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 79 | | map4 _ _ _ _ _ = raise Library.UnequalLengths; | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 80 | |
| 19922 | 81 | fun map6 _ [] [] [] [] [] [] = [] | 
| 82 | | map6 f (x :: xs) (y :: ys) (z :: zs) (u :: us) (v :: vs) (w :: ws) = f x y z u v w :: map6 f xs ys zs us vs ws | |
| 83 | | map6 _ _ _ _ _ _ _ = raise Library.UnequalLengths; | |
| 84 | ||
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 85 | fun map7 _ [] [] [] [] [] [] [] = [] | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 86 | | map7 f (x :: xs) (y :: ys) (z :: zs) (u :: us) (v :: vs) (w :: ws) (b :: bs) = f x y z u v w b :: map7 f xs ys zs us vs ws bs | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 87 | | map7 _ _ _ _ _ _ _ _ = raise Library.UnequalLengths; | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 88 | |
| 19564 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 89 | |
| 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 90 | |
| 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 91 | (* forms all "unordered pairs": [1, 2, 3] ==> [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)] *) | 
| 22166 | 92 | (* ==> library *) | 
| 19922 | 93 | fun unordered_pairs [] = [] | 
| 94 | | unordered_pairs (x::xs) = map (pair x) (x::xs) @ unordered_pairs xs | |
| 19564 
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
 krauss parents: diff
changeset | 95 | |
| 19770 
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
 krauss parents: 
19564diff
changeset | 96 | |
| 27792 | 97 | (* Replaces Frees by name. Works with loose Bounds. *) | 
| 22166 | 98 | fun replace_frees assoc = | 
| 99 | map_aterms (fn c as Free (n, _) => the_default c (AList.lookup (op =) assoc n) | |
| 100 | | t => t) | |
| 101 | ||
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 102 | |
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 103 | fun rename_bound n (Q $ Abs(_, T, b)) = (Q $ Abs(n, T, b)) | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 104 | | rename_bound n _ = raise Match | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 105 | |
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 106 | fun mk_forall_rename (n, v) = | 
| 27330 | 107 | rename_bound n o Logic.all v | 
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 108 | |
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 109 | fun forall_intr_rename (n, cv) thm = | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 110 | let | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 111 | val allthm = forall_intr cv thm | 
| 22166 | 112 | val (_ $ abs) = prop_of allthm | 
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 113 | in | 
| 27792 | 114 | Thm.rename_boundvars abs (Abs (n, dummyT, Term.dummy_pattern dummyT)) allthm | 
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 115 | end | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 116 | |
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 117 | |
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 118 | (* Returns the frees in a term in canonical order, excluding the fixes from the context *) | 
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20154diff
changeset | 119 | fun frees_in_term ctxt t = | 
| 21319 
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
 krauss parents: 
21237diff
changeset | 120 | Term.add_frees t [] | 
| 
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
 krauss parents: 
21237diff
changeset | 121 | |> filter_out (Variable.is_fixed ctxt o fst) | 
| 
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
 krauss parents: 
21237diff
changeset | 122 | |> rev | 
| 21051 
c49467a9c1e1
Switched function package to use the new package for inductive predicates.
 krauss parents: 
20876diff
changeset | 123 | |
| 
c49467a9c1e1
Switched function package to use the new package for inductive predicates.
 krauss parents: 
20876diff
changeset | 124 | |
| 27790 
37b4e65d1dbf
FundefLib.try_proof : attempt a proof and see if it works
 krauss parents: 
27330diff
changeset | 125 | datatype proof_attempt = Solved of thm | Stuck of thm | Fail | 
| 
37b4e65d1dbf
FundefLib.try_proof : attempt a proof and see if it works
 krauss parents: 
27330diff
changeset | 126 | |
| 
37b4e65d1dbf
FundefLib.try_proof : attempt a proof and see if it works
 krauss parents: 
27330diff
changeset | 127 | fun try_proof cgoal tac = | 
| 
37b4e65d1dbf
FundefLib.try_proof : attempt a proof and see if it works
 krauss parents: 
27330diff
changeset | 128 | case SINGLE tac (Goal.init cgoal) of | 
| 
37b4e65d1dbf
FundefLib.try_proof : attempt a proof and see if it works
 krauss parents: 
27330diff
changeset | 129 | NONE => Fail | 
| 
37b4e65d1dbf
FundefLib.try_proof : attempt a proof and see if it works
 krauss parents: 
27330diff
changeset | 130 | | SOME st => if Thm.no_prems st then Solved (Goal.finish st) else Stuck st | 
| 
37b4e65d1dbf
FundefLib.try_proof : attempt a proof and see if it works
 krauss parents: 
27330diff
changeset | 131 | |
| 
37b4e65d1dbf
FundefLib.try_proof : attempt a proof and see if it works
 krauss parents: 
27330diff
changeset | 132 | |
| 21858 
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
 wenzelm parents: 
21319diff
changeset | 133 | end |