src/HOL/Tools/Function/pattern_split.ML
author haftmann
Tue, 08 Jun 2010 16:37:19 +0200
changeset 37387 3581483cca6c
parent 34232 36a2a3029fd3
child 41114 f9ae7c2abf7e
permissions -rw-r--r--
qualified types "+" and nat; qualified constants Ball, Bex, Suc, curry; modernized some specifications
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31775
2b04504fcb69 uniformly capitialized names for subdirectories
haftmann
parents: 31723
diff changeset
     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: 33855
diff 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: 33049
diff 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
ba7a7c56bed5 normalized Proof.context/method type aliases;
wenzelm
parents: 20270
diff changeset
    10
  val split_some_equations :
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    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: 20344
diff changeset
    13
  val split_all_equations :
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    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: 33049
diff 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: 33049
diff changeset
    20
open Function_Lib
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    21
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
    22
fun new_var ctx vs T =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    23
  let
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    24
    val [v] = Variable.variant_frees ctx vs [("v", T)]
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    25
  in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    26
    (Free v :: vs, Free v)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff 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: 33855
diff 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: 33855
diff changeset
    31
    (binder_types (fastype_of t)) (vs, t)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    32
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    33
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff 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: 33855
diff changeset
    36
  map (fn (Cn,CT) =>
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff 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: 33855
diff changeset
    38
    (the (Datatype.get_constrs thy name))
25402
0a1005435e11 avoid ML print in production code;
wenzelm
parents: 24584
diff changeset
    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: 33855
diff changeset
    40
20636
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    41
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    42
fun join ((vs1,sub1), (vs2,sub2)) = (merge (op aconv) (vs1,vs2), sub1 @ sub2)
25538
58e8ba3b792b map_product and fold_product
haftmann
parents: 25402
diff changeset
    43
fun join_product (xs, ys) = map_product (curry join) xs ys
20636
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    44
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    45
exception DISJ
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    46
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    47
fun pattern_subtract_subst ctx vs t t' =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    48
  let
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    49
    exception DISJ
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    50
    fun pattern_subtract_subst_aux vs _ (Free v2) = []
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    51
      | pattern_subtract_subst_aux vs (v as (Free (_, T))) t' =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    52
      let
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    53
        fun foo constr =
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
    54
          let
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    55
            val (vs', t) = saturate ctx vs constr
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff 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: 33855
diff changeset
    58
            map (fn (vs, subst) => (vs, (v,t)::subst)) substs
20636
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    59
          end
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    60
      in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    61
        maps foo (inst_constrs_of (ProofContext.theory_of ctx) T)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    62
      end
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    63
     | pattern_subtract_subst_aux vs t t' =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    64
     let
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    65
       val (C, ps) = strip_comb t
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    66
       val (C', qs) = strip_comb t'
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    67
     in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    68
       if C = C'
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    69
       then flat (map2 (pattern_subtract_subst_aux vs) ps qs)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    70
       else raise DISJ
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    71
     end
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    72
  in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    73
    pattern_subtract_subst_aux vs t t'
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    74
    handle DISJ => [(vs, [])]
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff 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: 20344
diff 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: 33855
diff changeset
    79
  let
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    80
    val thy = ProofContext.theory_of ctx
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    81
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    82
    val (vs, feq1 as (_ $ (_ $ lhs1 $ _))) = dest_all_all eq1
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    83
    val (_,  _ $ (_ $ lhs2 $ _)) = dest_all_all eq2
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    84
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    85
    val substs = pattern_subtract_subst ctx vs lhs1 lhs2
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    86
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    87
    fun instantiate (vs', sigma) =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    88
      let
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    89
        val t = Pattern.rewrite_term thy sigma [] feq1
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    90
      in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff 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: 33855
diff changeset
    92
      end
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    93
  in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
    94
    map instantiate substs
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff 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: 33855
diff 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: 33855
diff 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: 33855
diff changeset
   106
  let
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
   107
    fun split_aux prev [] = []
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
   108
      | split_aux prev ((true, eq) :: es) =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff 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: 33855
diff changeset
   110
      | split_aux prev ((false, eq) :: es) =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
   111
        [eq] :: split_aux (eq :: prev) es
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
   112
  in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
   113
    split_aux [] eqns
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
   114
  end
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff changeset
   115
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   116
fun split_all_equations ctx =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 33855
diff 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: 20344
diff changeset
   118
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   119
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   120
end