| author | haftmann | 
| Tue, 04 May 2010 08:55:39 +0200 | |
| changeset 36634 | f9b43d197d16 | 
| parent 34232 | 36a2a3029fd3 | 
| child 41114 | f9ae7c2abf7e | 
| permissions | -rw-r--r-- | 
| 31775 | 1 | (* Title: HOL/Tools/Function/pattern_split.ML | 
| 20270 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 2 | Author: Alexander Krauss, TU Muenchen | 
| 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 3 | |
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 4 | Fairly ad-hoc pattern splitting. | 
| 20270 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 5 | |
| 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 6 | *) | 
| 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 7 | |
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: 
33049diff
changeset | 8 | signature FUNCTION_SPLIT = | 
| 20270 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 9 | sig | 
| 20289 | 10 | val split_some_equations : | 
| 21237 | 11 | Proof.context -> (bool * term) list -> term list list | 
| 20270 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 12 | |
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20344diff
changeset | 13 | val split_all_equations : | 
| 21237 | 14 | Proof.context -> term list -> term list list | 
| 20270 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 15 | end | 
| 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 16 | |
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: 
33049diff
changeset | 17 | structure Function_Split : FUNCTION_SPLIT = | 
| 20270 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 18 | struct | 
| 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 19 | |
| 33099 
b8cdd3d73022
function package: more standard names for structures and files
 krauss parents: 
33049diff
changeset | 20 | open Function_Lib | 
| 20270 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 21 | |
| 20344 | 22 | fun new_var ctx vs T = | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 23 | let | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 24 |     val [v] = Variable.variant_frees ctx vs [("v", T)]
 | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 25 | in | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 26 | (Free v :: vs, Free v) | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 27 | end | 
| 20270 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 28 | |
| 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 29 | fun saturate ctx vs t = | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 30 | fold (fn T => fn (vs, t) => new_var ctx vs T |> apsnd (curry op $ t)) | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 31 | (binder_types (fastype_of t)) (vs, t) | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 32 | |
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 33 | |
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 34 | (* This is copied from "pat_completeness.ML" *) | 
| 20270 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 35 | fun inst_constrs_of thy (T as Type (name, _)) = | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 36 | map (fn (Cn,CT) => | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 37 | Envir.subst_term_types (Sign.typ_match thy (body_type CT, T) Vartab.empty) (Const (Cn, CT))) | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 38 | (the (Datatype.get_constrs thy name)) | 
| 25402 | 39 |   | inst_constrs_of thy T = raise TYPE ("inst_constrs_of", [T], [])
 | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 40 | |
| 20636 | 41 | |
| 42 | fun join ((vs1,sub1), (vs2,sub2)) = (merge (op aconv) (vs1,vs2), sub1 @ sub2) | |
| 25538 | 43 | fun join_product (xs, ys) = map_product (curry join) xs ys | 
| 20636 | 44 | |
| 45 | exception DISJ | |
| 46 | ||
| 47 | fun pattern_subtract_subst ctx vs t t' = | |
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 48 | let | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 49 | exception DISJ | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 50 | fun pattern_subtract_subst_aux vs _ (Free v2) = [] | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 51 | | pattern_subtract_subst_aux vs (v as (Free (_, T))) t' = | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 52 | let | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 53 | fun foo constr = | 
| 20344 | 54 | let | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 55 | val (vs', t) = saturate ctx vs constr | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 56 | val substs = pattern_subtract_subst ctx vs' t t' | 
| 20270 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 57 | in | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 58 | map (fn (vs, subst) => (vs, (v,t)::subst)) substs | 
| 20636 | 59 | end | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 60 | in | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 61 | maps foo (inst_constrs_of (ProofContext.theory_of ctx) T) | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 62 | end | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 63 | | pattern_subtract_subst_aux vs t t' = | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 64 | let | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 65 | val (C, ps) = strip_comb t | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 66 | val (C', qs) = strip_comb t' | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 67 | in | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 68 | if C = C' | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 69 | then flat (map2 (pattern_subtract_subst_aux vs) ps qs) | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 70 | else raise DISJ | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 71 | end | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 72 | in | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 73 | pattern_subtract_subst_aux vs t t' | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 74 | handle DISJ => [(vs, [])] | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 75 | end | 
| 20270 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 76 | |
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20344diff
changeset | 77 | (* p - q *) | 
| 20270 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 78 | fun pattern_subtract ctx eq2 eq1 = | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 79 | let | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 80 | val thy = ProofContext.theory_of ctx | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 81 | |
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 82 | val (vs, feq1 as (_ $ (_ $ lhs1 $ _))) = dest_all_all eq1 | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 83 | val (_, _ $ (_ $ lhs2 $ _)) = dest_all_all eq2 | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 84 | |
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 85 | val substs = pattern_subtract_subst ctx vs lhs1 lhs2 | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 86 | |
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 87 | fun instantiate (vs', sigma) = | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 88 | let | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 89 | val t = Pattern.rewrite_term thy sigma [] feq1 | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 90 | in | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 91 | fold_rev Logic.all (inter (op =) vs' (map Free (frees_in_term ctx t))) t | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 92 | end | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 93 | in | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 94 | map instantiate substs | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 95 | end | 
| 20270 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 96 | |
| 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 97 | (* ps - p' *) | 
| 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 98 | fun pattern_subtract_from_many ctx p'= | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 99 | maps (pattern_subtract ctx p') | 
| 20270 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 100 | |
| 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 101 | (* in reverse order *) | 
| 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 102 | fun pattern_subtract_many ctx ps' = | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 103 | fold_rev (pattern_subtract_from_many ctx) ps' | 
| 20270 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 104 | |
| 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 105 | fun split_some_equations ctx eqns = | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 106 | let | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 107 | fun split_aux prev [] = [] | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 108 | | split_aux prev ((true, eq) :: es) = | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 109 | pattern_subtract_many ctx prev [eq] :: split_aux (eq :: prev) es | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 110 | | split_aux prev ((false, eq) :: es) = | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 111 | [eq] :: split_aux (eq :: prev) es | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 112 | in | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 113 | split_aux [] eqns | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 114 | end | 
| 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 115 | |
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20344diff
changeset | 116 | fun split_all_equations ctx = | 
| 34232 
36a2a3029fd3
new year's resolution: reindented code in function package
 krauss parents: 
33855diff
changeset | 117 | split_some_equations ctx o map (pair true) | 
| 20523 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20344diff
changeset | 118 | |
| 
36a59e5d0039
Major update to function package, including new syntax and the (only theoretical)
 krauss parents: 
20344diff
changeset | 119 | |
| 20270 
3abe7dae681e
Function package can now do automatic splits of overlapping datatype patterns
 krauss parents: diff
changeset | 120 | end |