src/HOL/Tools/function_package/pattern_split.ML
author krauss
Thu, 14 Sep 2006 15:25:23 +0200
changeset 20535 b4b3933ec026
parent 20523 36a59e5d0039
child 20636 ddddf0b7d322
permissions -rw-r--r--
updated makefile
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
     1
(*  Title:      HOL/Tools/function_package/fundef_package.ML
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
     2
    ID:         $Id$
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
     3
    Author:     Alexander Krauss, TU Muenchen
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
     4
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
     5
A package for general recursive function definitions.
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
     6
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
     7
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
     8
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
     9
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    10
*)
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    11
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
    12
signature FUNDEF_SPLIT =
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    13
sig
20289
ba7a7c56bed5 normalized Proof.context/method type aliases;
wenzelm
parents: 20270
diff changeset
    14
  val split_some_equations :
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    15
    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
    16
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    17
  val split_all_equations :
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    18
    Proof.context -> term list -> term list list
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    19
end
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    20
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
    21
structure FundefSplit : FUNDEF_SPLIT =
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    22
struct
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    23
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)
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    38
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    39
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, _)) =
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
    42
        map (fn (Cn,CT) => Envir.subst_TVars (Sign.typ_match thy (body_type CT, T) Vartab.empty) (Const (Cn, CT)))
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
    43
            (the (DatatypePackage.get_datatype_constrs thy name))
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    44
  | inst_constrs_of thy t = (print t; sys_error "inst_constrs_of")
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    45
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    46
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    47
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    48
fun pattern_subtract_subst ctx vs _ (Free v2) = []
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    49
  | pattern_subtract_subst ctx vs (v as (Free (_, T))) t' =
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
    50
    let
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
    51
      fun foo constr =
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
    52
          let
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    53
            val (vs', t) = saturate ctx vs constr
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    54
            val substs = pattern_subtract_subst ctx vs' t t'
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    55
          in
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    56
            map (fn (vs, subst) => (vs, (v,t)::subst)) substs
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    57
          end
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    58
    in
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    59
      flat (map foo (inst_constrs_of (ProofContext.theory_of ctx) T))
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    60
    end
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    61
  | pattern_subtract_subst ctx vs t t' =
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    62
    let
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    63
      val (C, ps) = strip_comb t
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    64
      val (C', qs) = strip_comb t'
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    65
    in
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    66
      if C = C'
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    67
      then flat (map2 (pattern_subtract_subst ctx vs) ps qs)
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    68
      else [(vs, [])]
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    69
    end
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    70
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    71
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    72
(* p - q *)
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    73
fun pattern_subtract ctx eq2 eq1 =
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    74
    let
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    75
      
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    76
      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
    77
      val (_,  _ $ (_ $ lhs2 $ _)) = dest_all_all eq2
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    78
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    79
      val thy = ProofContext.theory_of ctx
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    80
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    81
      val substs = pattern_subtract_subst ctx vs lhs1 lhs2
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    82
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    83
      fun instantiate (vs', sigma) =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    84
          let
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    85
            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
    86
          in
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    87
            fold_rev mk_forall (map Free (frees_in_term ctx t) inter vs') t
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    88
          end
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    89
    in
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    90
      map instantiate substs
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    91
    end
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    92
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    93
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    94
(* ps - p' *)
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    95
fun pattern_subtract_from_many ctx p'=
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    96
    flat o map (pattern_subtract ctx p')
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    97
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    98
(* in reverse order *)
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    99
fun pattern_subtract_many ctx ps' =
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   100
    fold_rev (pattern_subtract_from_many ctx) ps'
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   101
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   102
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   103
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   104
fun split_some_equations ctx eqns =
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   105
    let
20338
ecdfc96cf4d0 Added Keywords: "otherwise" and "sequential", needed for function package's
krauss
parents: 20289
diff changeset
   106
      fun split_aux prev [] = []
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   107
        | 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
   108
                                              :: split_aux (eq :: prev) es
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   109
        | 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
   110
                                               :: split_aux (eq :: prev) es
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   111
    in
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   112
      split_aux [] eqns
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   113
    end
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   114
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   115
fun split_all_equations ctx =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   116
    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
   117
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
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   120
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   121
end