1 (* Title: HOL/Tools/function_package/fundef_package.ML |
1 (* Title: HOL/Tools/function_package/fundef_package.ML |
2 ID: $Id$ |
2 ID: $Id$ |
3 Author: Alexander Krauss, TU Muenchen |
3 Author: Alexander Krauss, TU Muenchen |
4 |
4 |
5 A package for general recursive function definitions. |
5 A package for general recursive function definitions. |
6 |
6 |
7 Automatic splitting of overlapping constructor patterns. This is a preprocessing step which |
7 Automatic splitting of overlapping constructor patterns. This is a preprocessing step which |
8 turns a specification with overlaps into an overlap-free specification. |
8 turns a specification with overlaps into an overlap-free specification. |
9 |
9 |
10 *) |
10 *) |
11 |
11 |
12 signature FUNDEF_SPLIT = |
12 signature FUNDEF_SPLIT = |
13 sig |
13 sig |
14 val split_some_equations : |
14 val split_some_equations : |
15 Proof.context -> (('a * ('b * bool)) * term) list -> (('a * 'b) * term list) list |
15 Proof.context -> (('a * ('b * bool)) * term) list -> (('a * 'b) * term list) list |
16 |
16 |
17 end |
17 end |
18 |
18 |
19 structure FundefSplit : FUNDEF_SPLIT = |
19 structure FundefSplit : FUNDEF_SPLIT = |
20 struct |
20 struct |
21 |
21 |
22 |
22 |
23 (* We use proof context for the variable management *) |
23 (* We use proof context for the variable management *) |
24 (* FIXME: no __ *) |
24 (* FIXME: no __ *) |
25 |
25 |
26 fun new_var ctx vs T = |
26 fun new_var ctx vs T = |
27 let |
27 let |
28 val [v] = Variable.variant_frees ctx vs [("v", T)] |
28 val [v] = Variable.variant_frees ctx vs [("v", T)] |
29 in |
29 in |
30 (Free v :: vs, Free v) |
30 (Free v :: vs, Free v) |
31 end |
31 end |
32 |
32 |
35 (binder_types (fastype_of t)) (vs, t) |
35 (binder_types (fastype_of t)) (vs, t) |
36 |
36 |
37 |
37 |
38 (* This is copied from "fundef_datatype.ML" *) |
38 (* This is copied from "fundef_datatype.ML" *) |
39 fun inst_constrs_of thy (T as Type (name, _)) = |
39 fun inst_constrs_of thy (T as Type (name, _)) = |
40 map (fn (Cn,CT) => Envir.subst_TVars (Type.typ_match (Sign.tsig_of thy) (body_type CT, T) Vartab.empty) (Const (Cn, CT))) |
40 map (fn (Cn,CT) => Envir.subst_TVars (Sign.typ_match thy (body_type CT, T) Vartab.empty) (Const (Cn, CT))) |
41 (the (DatatypePackage.get_datatype_constrs thy name)) |
41 (the (DatatypePackage.get_datatype_constrs thy name)) |
42 | inst_constrs_of thy t = (print t; sys_error "inst_constrs_of") |
42 | inst_constrs_of thy t = (print t; sys_error "inst_constrs_of") |
43 |
43 |
44 |
44 |
45 |
45 |
46 fun pattern_subtract_subst ctx vs _ (Free v2) = [] |
46 fun pattern_subtract_subst ctx vs _ (Free v2) = [] |
47 | pattern_subtract_subst ctx vs (v as (Free (_, T))) t' = |
47 | pattern_subtract_subst ctx vs (v as (Free (_, T))) t' = |
48 let |
48 let |
49 fun foo constr = |
49 fun foo constr = |
50 let |
50 let |
51 val (vs', t) = saturate ctx vs constr |
51 val (vs', t) = saturate ctx vs constr |
52 val substs = pattern_subtract_subst ctx vs' t t' |
52 val substs = pattern_subtract_subst ctx vs' t t' |
53 in |
53 in |
54 map (cons (v, t)) substs |
54 map (cons (v, t)) substs |
55 end |
55 end |
106 fun split_some_equations ctx eqns = |
106 fun split_some_equations ctx eqns = |
107 let |
107 let |
108 fun split_aux prev [] = [] |
108 fun split_aux prev [] = [] |
109 | split_aux prev (((n, (att, true)), eq) :: es) = ((n, att), pattern_subtract_many ctx prev [eq]) |
109 | split_aux prev (((n, (att, true)), eq) :: es) = ((n, att), pattern_subtract_many ctx prev [eq]) |
110 :: split_aux (eq :: prev) es |
110 :: split_aux (eq :: prev) es |
111 | split_aux prev (((n, (att, false)), eq) :: es) = ((n, att), [eq]) |
111 | split_aux prev (((n, (att, false)), eq) :: es) = ((n, att), [eq]) |
112 :: split_aux (eq :: prev) es |
112 :: split_aux (eq :: prev) es |
113 in |
113 in |
114 split_aux [] eqns |
114 split_aux [] eqns |
115 end |
115 end |
116 |
116 |
117 end |
117 end |