author | krauss |
Sat, 02 Jun 2007 15:28:38 +0200 | |
changeset 23203 | a5026e73cfcf |
parent 22166 | 0a50d4db234a |
child 23216 | 49c78d67b807 |
permissions | -rw-r--r-- |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
1 |
(* Title: HOL/Tools/function_package/lib.ML |
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 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
9 |
|
21051
c49467a9c1e1
Switched function package to use the new package for inductive predicates.
krauss
parents:
20876
diff
changeset
|
10 |
|
c49467a9c1e1
Switched function package to use the new package for inductive predicates.
krauss
parents:
20876
diff
changeset
|
11 |
structure FundefLib = struct |
c49467a9c1e1
Switched function package to use the new package for inductive predicates.
krauss
parents:
20876
diff
changeset
|
12 |
|
22166 | 13 |
fun map_option f NONE = NONE |
14 |
| map_option f (SOME x) = SOME (f x); |
|
15 |
||
16 |
fun fold_option f NONE y = y |
|
17 |
| fold_option f (SOME x) y = f x y; |
|
18 |
||
19 |
fun fold_map_option f NONE y = (NONE, y) |
|
20 |
| fold_map_option f (SOME x) y = apfst SOME (f x y); |
|
21 |
||
22 |
(* ??? *) |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
23 |
fun eq_str (s : string, t) = (s = t) (* monomorphic equality on strings *) |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
24 |
|
22166 | 25 |
(* ==> logic.ML? *) |
21188 | 26 |
fun mk_forall v t = all (fastype_of v) $ lambda v t |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
27 |
|
22166 | 28 |
(* lambda-abstracts over an arbitrarily nested tuple |
29 |
==> hologic.ML? *) |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
30 |
fun tupled_lambda vars t = |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
31 |
case vars of |
21237 | 32 |
(Free v) => lambda (Free v) t |
21188 | 33 |
| (Var v) => lambda (Var v) t |
34 |
| (Const ("Pair", Type ("fun", [Ta, Type ("fun", [Tb, _])]))) $ us $ vs => |
|
21237 | 35 |
(HOLogic.split_const (Ta,Tb, fastype_of t)) $ (tupled_lambda us (tupled_lambda vs t)) |
21188 | 36 |
| _ => raise Match |
37 |
||
38 |
||
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
39 |
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
|
40 |
let |
21237 | 41 |
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
|
42 |
in |
21237 | 43 |
(Free (n, T), body) |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
44 |
end |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
45 |
| dest_all _ = raise Match |
21188 | 46 |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
47 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
48 |
(* 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
|
49 |
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
|
50 |
let |
21319
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
51 |
val (v,b) = dest_all t |
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
52 |
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
|
53 |
in |
21319
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
54 |
(v :: vs, b') |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
55 |
end |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
56 |
| dest_all_all t = ([],t) |
21319
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
57 |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
58 |
|
21319
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
59 |
(* FIXME: similar to Variable.focus *) |
19922 | 60 |
fun dest_all_all_ctx ctx (Const ("all", _) $ Abs (a as (n,T,b))) = |
61 |
let |
|
20154
c709a29f1363
renamed Variable.rename_wrt to Variable.variant_frees;
wenzelm
parents:
19922
diff
changeset
|
62 |
val [(n', _)] = Variable.variant_frees ctx [] [(n,T)] |
19922 | 63 |
val (_, ctx') = ProofContext.add_fixes_i [(n', SOME T, NoSyn)] ctx |
64 |
||
65 |
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:
21319
diff
changeset
|
66 |
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:
21319
diff
changeset
|
67 |
(* Note: We assume that n' does not occur in the body. Otherwise it would be fixed. *) |
19922 | 68 |
|
69 |
val (ctx'', vs, bd) = dest_all_all_ctx ctx' body |
|
70 |
in |
|
21237 | 71 |
(ctx'', (n', T) :: vs, bd) |
19922 | 72 |
end |
73 |
| dest_all_all_ctx ctx t = |
|
74 |
(ctx, [], t) |
|
75 |
||
76 |
||
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
77 |
fun implies_elim_swp a b = implies_elim b a |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
78 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
79 |
fun map3 _ [] [] [] = [] |
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
80 |
| map3 f (x :: xs) (y :: ys) (z :: zs) = f x y z :: map3 f xs ys zs |
19841 | 81 |
| map3 _ _ _ _ = raise Library.UnequalLengths; |
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
82 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
83 |
fun map4 _ [] [] [] [] = [] |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
84 |
| 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:
20154
diff
changeset
|
85 |
| map4 _ _ _ _ _ = raise Library.UnequalLengths; |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
86 |
|
19922 | 87 |
fun map6 _ [] [] [] [] [] [] = [] |
88 |
| 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 |
|
89 |
| map6 _ _ _ _ _ _ _ = raise Library.UnequalLengths; |
|
90 |
||
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
91 |
fun map7 _ [] [] [] [] [] [] [] = [] |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
92 |
| 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:
20154
diff
changeset
|
93 |
| map7 _ _ _ _ _ _ _ _ = raise Library.UnequalLengths; |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
94 |
|
19564
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
95 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
96 |
|
d3e2f532459a
First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff
changeset
|
97 |
(* forms all "unordered pairs": [1, 2, 3] ==> [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)] *) |
22166 | 98 |
(* ==> library *) |
19922 | 99 |
fun unordered_pairs [] = [] |
100 |
| 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
|
101 |
|
19770
be5c23ebe1eb
HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents:
19564
diff
changeset
|
102 |
|
22166 | 103 |
(* term.ML *) |
21319
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
104 |
fun forall_aterms P (t $ u) = forall_aterms P t andalso forall_aterms P u |
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
105 |
| forall_aterms P (Abs (_, _, t)) = forall_aterms P t |
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
106 |
| forall_aterms P a = P a |
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
107 |
|
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
108 |
fun exists_aterm P = not o forall_aterms (not o P) |
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
109 |
|
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
110 |
|
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
111 |
|
22166 | 112 |
(* Replaces Frees by name. Probably quicker than Pattern.rewrite_terms, and works with loose Bounds. *) |
113 |
fun replace_frees assoc = |
|
114 |
map_aterms (fn c as Free (n, _) => the_default c (AList.lookup (op =) assoc n) |
|
115 |
| t => t) |
|
116 |
||
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
117 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
118 |
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:
20154
diff
changeset
|
119 |
| rename_bound n _ = raise Match |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
120 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
121 |
fun mk_forall_rename (n, v) = |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
122 |
rename_bound n o mk_forall v |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
123 |
|
21319
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
124 |
val dummy_term = Free ("", dummyT) |
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
125 |
|
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
126 |
fun forall_intr_rename (n, cv) thm = |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
127 |
let |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
128 |
val allthm = forall_intr cv thm |
22166 | 129 |
val (_ $ abs) = prop_of allthm |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
130 |
in |
21319
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
131 |
Thm.rename_boundvars abs (Abs (n, dummyT, dummy_term)) allthm |
20523
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
132 |
end |
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
133 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
134 |
|
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
krauss
parents:
20154
diff
changeset
|
135 |
(* 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:
20154
diff
changeset
|
136 |
fun frees_in_term ctxt t = |
21319
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
137 |
Term.add_frees t [] |
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
138 |
|> filter_out (Variable.is_fixed ctxt o fst) |
cf814e36f788
replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents:
21237
diff
changeset
|
139 |
|> rev |
21051
c49467a9c1e1
Switched function package to use the new package for inductive predicates.
krauss
parents:
20876
diff
changeset
|
140 |
|
c49467a9c1e1
Switched function package to use the new package for inductive predicates.
krauss
parents:
20876
diff
changeset
|
141 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
21319
diff
changeset
|
142 |
end |