author | wenzelm |
Mon, 18 Apr 2011 13:52:23 +0200 | |
changeset 42388 | a44b0fdaa6c2 |
parent 41117 | d6379876ec4c |
child 42483 | 39eefaef816a |
permissions | -rw-r--r-- |
37744 | 1 |
(* Title: HOL/Tools/Function/function_lib.ML |
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
2 |
Author: Alexander Krauss, TU Muenchen |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
3 |
|
41113 | 4 |
Ad-hoc collection of function waiting to be eliminated, generalized, |
5 |
moved elsewhere or otherwise cleaned up. |
|
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
6 |
*) |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
7 |
|
41113 | 8 |
signature FUNCTION_LIB = |
9 |
sig |
|
10 |
val plural: string -> string -> 'a list -> string |
|
11 |
||
41116 | 12 |
val focus_term: term -> Proof.context -> ((string * typ) list * term) * Proof.context |
41113 | 13 |
val dest_all_all: term -> term list * term |
14 |
||
15 |
val map4: ('a -> 'b -> 'c -> 'd -> 'e) -> 'a list -> 'b list -> 'c list -> 'd list -> 'e list |
|
16 |
val map7: ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h) -> 'a list -> 'b list -> 'c list -> |
|
17 |
'd list -> 'e list -> 'f list -> 'g list -> 'h list |
|
18 |
||
19 |
val unordered_pairs: 'a list -> ('a * 'a) list |
|
20 |
||
21 |
val replace_frees: (string * term) list -> term -> term |
|
22 |
val rename_bound: string -> term -> term |
|
23 |
val mk_forall_rename: (string * term) -> term -> term |
|
24 |
val forall_intr_rename: (string * cterm) -> thm -> thm |
|
25 |
||
26 |
val frees_in_term: Proof.context -> term -> (string * typ) list |
|
27 |
||
28 |
datatype proof_attempt = Solved of thm | Stuck of thm | Fail |
|
29 |
val try_proof: cterm -> tactic -> proof_attempt |
|
30 |
||
31 |
val dest_binop_list: string -> term -> term list |
|
32 |
val regroup_conv: string -> string -> thm list -> int list -> conv |
|
33 |
val regroup_union_conv: int list -> conv |
|
34 |
end |
|
35 |
||
36 |
structure Function_Lib: FUNCTION_LIB = |
|
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
37 |
struct |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
38 |
|
40076 | 39 |
(* "The variable" ^ plural " is" "s are" vs *) |
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
40 |
fun plural sg pl [x] = sg |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
41 |
| plural sg pl _ = pl |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
42 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
43 |
|
41116 | 44 |
(*term variant of Variable.focus*) |
45 |
fun focus_term t ctxt = |
|
46 |
let |
|
47 |
val ps = Term.variant_frees t (Term.strip_all_vars t); (*as they are printed :-*) |
|
48 |
val (xs, Ts) = split_list ps; |
|
49 |
val (xs', ctxt') = Variable.variant_fixes xs ctxt; |
|
50 |
val ps' = xs' ~~ Ts; |
|
51 |
val inst = map Free ps' |
|
52 |
val t' = Term.subst_bounds (rev inst, Term.strip_all_body t); |
|
53 |
val ctxt'' = ctxt' |> fold Variable.declare_constraints inst; |
|
54 |
in ((ps', t'), ctxt'') end; |
|
55 |
||
56 |
||
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
57 |
(* Removes all quantifiers from a term, replacing bound variables by frees. *) |
34232
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
58 |
fun dest_all_all (t as (Const ("all",_) $ _)) = |
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
59 |
let |
40076 | 60 |
val (v,b) = Logic.dest_all t |> apfst Free |
34232
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
61 |
val (vs, b') = dest_all_all b |
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
62 |
in |
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
63 |
(v :: vs, b') |
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
64 |
end |
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
65 |
| dest_all_all t = ([],t) |
33611 | 66 |
|
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
67 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
68 |
fun map4 _ [] [] [] [] = [] |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
69 |
| map4 f (x :: xs) (y :: ys) (z :: zs) (u :: us) = f x y z u :: map4 f xs ys zs us |
40722
441260986b63
make two copies (!) of Library.UnequalLengths coincide with ListPair.UnequalLengths;
wenzelm
parents:
40076
diff
changeset
|
70 |
| map4 _ _ _ _ _ = raise ListPair.UnequalLengths; |
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
71 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
72 |
fun map7 _ [] [] [] [] [] [] [] = [] |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
73 |
| 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 |
40722
441260986b63
make two copies (!) of Library.UnequalLengths coincide with ListPair.UnequalLengths;
wenzelm
parents:
40076
diff
changeset
|
74 |
| map7 _ _ _ _ _ _ _ _ = raise ListPair.UnequalLengths; |
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
75 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
76 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
77 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
78 |
(* forms all "unordered pairs": [1, 2, 3] ==> [(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)] *) |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
79 |
fun unordered_pairs [] = [] |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
80 |
| unordered_pairs (x::xs) = map (pair x) (x::xs) @ unordered_pairs xs |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
81 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
82 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
83 |
(* Replaces Frees by name. Works with loose Bounds. *) |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
84 |
fun replace_frees assoc = |
34232
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
85 |
map_aterms (fn c as Free (n, _) => the_default c (AList.lookup (op =) assoc n) |
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
86 |
| t => t) |
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
87 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
88 |
|
34232
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
89 |
fun rename_bound n (Q $ Abs (_, T, b)) = (Q $ Abs (n, T, b)) |
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
90 |
| rename_bound n _ = raise Match |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
91 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
92 |
fun mk_forall_rename (n, v) = |
34232
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
93 |
rename_bound n o Logic.all v |
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
94 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
95 |
fun forall_intr_rename (n, cv) thm = |
34232
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
96 |
let |
36945 | 97 |
val allthm = Thm.forall_intr cv thm |
34232
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
98 |
val (_ $ abs) = prop_of allthm |
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
99 |
in |
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
100 |
Thm.rename_boundvars abs (Abs (n, dummyT, Term.dummy_pattern dummyT)) allthm |
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
101 |
end |
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
102 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
103 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
104 |
(* Returns the frees in a term in canonical order, excluding the fixes from the context *) |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
105 |
fun frees_in_term ctxt t = |
34232
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
106 |
Term.add_frees t [] |
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
107 |
|> filter_out (Variable.is_fixed ctxt o fst) |
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
108 |
|> rev |
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
109 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
110 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
111 |
datatype proof_attempt = Solved of thm | Stuck of thm | Fail |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
112 |
|
34232
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
113 |
fun try_proof cgoal tac = |
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
114 |
case SINGLE tac (Goal.init cgoal) of |
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
115 |
NONE => Fail |
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
116 |
| SOME st => |
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
117 |
if Thm.no_prems st |
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
118 |
then Solved (Goal.finish (Syntax.init_pretty_global (Thm.theory_of_cterm cgoal)) st) |
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
119 |
else Stuck st |
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
120 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
121 |
|
34232
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
122 |
fun dest_binop_list cn (t as (Const (n, _) $ a $ b)) = |
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
123 |
if cn = n then dest_binop_list cn a @ dest_binop_list cn b else [ t ] |
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
124 |
| dest_binop_list _ t = [ t ] |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
125 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
126 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
127 |
(* separate two parts in a +-expression: |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
128 |
"a + b + c + d + e" --> "(a + b + d) + (c + e)" |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
129 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
130 |
Here, + can be any binary operation that is AC. |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
131 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
132 |
cn - The name of the binop-constructor (e.g. @{const_name Un}) |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
133 |
ac - the AC rewrite rules for cn |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
134 |
is - the list of indices of the expressions that should become the first part |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
135 |
(e.g. [0,1,3] in the above example) |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
136 |
*) |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
137 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
138 |
fun regroup_conv neu cn ac is ct = |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
139 |
let |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
140 |
val mk = HOLogic.mk_binop cn |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
141 |
val t = term_of ct |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
142 |
val xs = dest_binop_list cn t |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
143 |
val js = subtract (op =) is (0 upto (length xs) - 1) |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
144 |
val ty = fastype_of t |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
145 |
val thy = theory_of_cterm ct |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
146 |
in |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
147 |
Goal.prove_internal [] |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
148 |
(cterm_of thy |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
149 |
(Logic.mk_equals (t, |
33611 | 150 |
if null is |
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
151 |
then mk (Const (neu, ty), foldr1 mk (map (nth xs) js)) |
33611 | 152 |
else if null js |
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
153 |
then mk (foldr1 mk (map (nth xs) is), Const (neu, ty)) |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
154 |
else mk (foldr1 mk (map (nth xs) is), foldr1 mk (map (nth xs) js))))) |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
155 |
(K (rewrite_goals_tac ac |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
156 |
THEN rtac Drule.reflexive_thm 1)) |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
157 |
end |
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
158 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
159 |
(* instance for unions *) |
34232
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
160 |
val regroup_union_conv = |
35402 | 161 |
regroup_conv @{const_abbrev Set.empty} @{const_name Lattices.sup} |
34232
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
162 |
(map (fn t => t RS eq_reflection) |
36a2a3029fd3
new year's resolution: reindented code in function package
krauss
parents:
33855
diff
changeset
|
163 |
(@{thms Un_ac} @ @{thms Un_empty_right} @ @{thms Un_empty_left})) |
33099
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
164 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
165 |
|
b8cdd3d73022
function package: more standard names for structures and files
krauss
parents:
diff
changeset
|
166 |
end |