src/HOL/Tools/Function/pattern_split.ML
author haftmann
Tue, 20 Oct 2009 16:13:01 +0200
changeset 33037 b22e44496dc2
parent 32952 aeb1e44fbc19
child 33038 8f9594c31de4
permissions -rw-r--r--
replaced old_style infixes eq_set, subset, union, inter and variants by generic versions
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
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
     4
A package for general recursive function definitions.
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
     5
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
     6
Automatic splitting of overlapping constructor patterns. This is a preprocessing step which
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
     7
turns a specification with overlaps into an overlap-free specification.
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
     8
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
     9
*)
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    10
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
    11
signature FUNDEF_SPLIT =
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    12
sig
20289
ba7a7c56bed5 normalized Proof.context/method type aliases;
wenzelm
parents: 20270
diff changeset
    13
  val split_some_equations :
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    14
      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
    15
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    16
  val split_all_equations :
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    17
      Proof.context -> term list -> term list list
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    18
end
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    19
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
    20
structure FundefSplit : FUNDEF_SPLIT =
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    21
struct
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    22
21051
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20654
diff changeset
    23
open FundefLib
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    24
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    25
(* We use proof context for the variable management *)
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    26
(* FIXME: no __ *)
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    27
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
    28
fun new_var ctx vs T =
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
    29
    let
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    30
      val [v] = Variable.variant_frees ctx vs [("v", T)]
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    31
    in
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    32
      (Free v :: vs, Free v)
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    33
    end
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    34
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    35
fun saturate ctx vs t =
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    36
    fold (fn T => fn (vs, t) => new_var ctx vs T |> apsnd (curry op $ t))
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    37
         (binder_types (fastype_of t)) (vs, t)
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    38
         
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    39
         
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    40
(* This is copied from "fundef_datatype.ML" *)
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    41
fun inst_constrs_of thy (T as Type (name, _)) =
32035
8e77b6a250d5 tuned/modernized Envir.subst_XXX;
wenzelm
parents: 31784
diff changeset
    42
    map (fn (Cn,CT) =>
8e77b6a250d5 tuned/modernized Envir.subst_XXX;
wenzelm
parents: 31784
diff changeset
    43
          Envir.subst_term_types (Sign.typ_match thy (body_type CT, T) Vartab.empty) (Const (Cn, CT)))
31784
bd3486c57ba3 tuned interfaces of datatype module
haftmann
parents: 31775
diff changeset
    44
        (the (Datatype.get_constrs thy name))
25402
0a1005435e11 avoid ML print in production code;
wenzelm
parents: 24584
diff changeset
    45
  | inst_constrs_of thy T = raise TYPE ("inst_constrs_of", [T], [])
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    46
                            
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    47
                            
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    48
                            
20636
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    49
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    50
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
    51
fun join_product (xs, ys) = map_product (curry join) xs ys
20636
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    52
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    53
fun join_list [] = []
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    54
  | join_list xs = foldr1 (join_product) xs
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    55
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    56
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    57
exception DISJ
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    58
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    59
fun pattern_subtract_subst ctx vs t t' =
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
    60
    let
20636
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    61
      exception DISJ
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    62
      fun pattern_subtract_subst_aux vs _ (Free v2) = []
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    63
        | pattern_subtract_subst_aux vs (v as (Free (_, T))) t' =
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
    64
          let
20636
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    65
            fun foo constr =
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    66
                let
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    67
                  val (vs', t) = saturate ctx vs constr
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    68
                  val substs = pattern_subtract_subst ctx vs' t t'
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    69
                in
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    70
                  map (fn (vs, subst) => (vs, (v,t)::subst)) substs
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    71
                end
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    72
          in
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32035
diff changeset
    73
            maps foo (inst_constrs_of (ProofContext.theory_of ctx) T)
20636
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    74
          end
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    75
        | pattern_subtract_subst_aux vs t t' =
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    76
          let
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    77
            val (C, ps) = strip_comb t
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    78
            val (C', qs) = strip_comb t'
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    79
          in
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    80
            if C = C'
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    81
            then flat (map2 (pattern_subtract_subst_aux vs) ps qs)
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    82
            else raise DISJ
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    83
          end
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    84
    in
20636
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    85
      pattern_subtract_subst_aux vs t t'
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    86
      handle DISJ => [(vs, [])]
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    87
    end
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    88
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    89
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    90
(* p - q *)
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    91
fun pattern_subtract ctx eq2 eq1 =
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    92
    let
20636
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    93
      val thy = ProofContext.theory_of ctx
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    94
                
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    95
      val (vs, feq1 as (_ $ (_ $ lhs1 $ _))) = dest_all_all eq1
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    96
      val (_,  _ $ (_ $ lhs2 $ _)) = dest_all_all eq2
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    97
                                     
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    98
      val substs = pattern_subtract_subst ctx vs lhs1 lhs2
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
    99
                   
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   100
      fun instantiate (vs', sigma) =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   101
          let
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   102
            val t = Pattern.rewrite_term thy sigma [] feq1
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   103
          in
33037
b22e44496dc2 replaced old_style infixes eq_set, subset, union, inter and variants by generic versions
haftmann
parents: 32952
diff changeset
   104
            fold_rev Logic.all (gen_inter (op =) (map Free (frees_in_term ctx t), vs')) t
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   105
          end
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   106
    in
20654
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20636
diff changeset
   107
      map instantiate substs
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   108
    end
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   109
      
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   110
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   111
(* ps - p' *)
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   112
fun pattern_subtract_from_many ctx p'=
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32035
diff changeset
   113
    maps (pattern_subtract ctx p')
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   114
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   115
(* in reverse order *)
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   116
fun pattern_subtract_many ctx ps' =
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   117
    fold_rev (pattern_subtract_from_many ctx) ps'
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   118
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   119
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   120
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   121
fun split_some_equations ctx eqns =
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   122
    let
20338
ecdfc96cf4d0 Added Keywords: "otherwise" and "sequential", needed for function package's
krauss
parents: 20289
diff changeset
   123
      fun split_aux prev [] = []
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   124
        | split_aux prev ((true, eq) :: es) = pattern_subtract_many ctx prev [eq]
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   125
                                              :: split_aux (eq :: prev) es
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   126
        | split_aux prev ((false, eq) :: es) = [eq]
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   127
                                               :: split_aux (eq :: prev) es
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   128
    in
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   129
      split_aux [] eqns
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   130
    end
21237
b803f9870e97 untabified
krauss
parents: 21051
diff changeset
   131
    
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   132
fun split_all_equations ctx =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   133
    split_some_equations ctx o map (pair true)
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   134
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   135
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   136
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   137
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   138
end