src/HOL/Tools/function_package/pattern_split.ML
author krauss
Thu, 21 Sep 2006 12:22:05 +0200
changeset 20654 d80502f0d701
parent 20636 ddddf0b7d322
child 21051 c49467a9c1e1
permissions -rw-r--r--
1. Function package accepts a parameter (default "some_term"), which specifies the functions behaviour outside its domain. 2. Bugfix: An exception occured when a function in a mutual definition was declared but no equation was given.
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
20636
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    48
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    49
fun join ((vs1,sub1), (vs2,sub2)) = (merge (op aconv) (vs1,vs2), sub1 @ sub2)
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    50
fun join_product (xs, ys) = map join (product xs ys)
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    51
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    52
fun join_list [] = []
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    53
  | join_list xs = foldr1 (join_product) xs
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    54
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
exception DISJ
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    57
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    58
fun pattern_subtract_subst ctx vs t t' =
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
    59
    let
20636
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    60
      exception DISJ
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    61
      fun pattern_subtract_subst_aux vs _ (Free v2) = []
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    62
        | pattern_subtract_subst_aux vs (v as (Free (_, T))) t' =
20344
d02b43ea722e avoid low-level tsig;
wenzelm
parents: 20338
diff changeset
    63
          let
20636
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    64
            fun foo constr =
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    65
                let
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    66
                  val (vs', t) = saturate ctx vs constr
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    67
                  val substs = pattern_subtract_subst ctx vs' t t'
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    68
                in
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    69
                  map (fn (vs, subst) => (vs, (v,t)::subst)) substs
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    70
                end
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    71
          in
20636
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    72
            flat (map foo (inst_constrs_of (ProofContext.theory_of ctx) T))
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    73
          end
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    74
        | pattern_subtract_subst_aux vs t t' =
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    75
          let
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    76
            val (C, ps) = strip_comb t
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    77
            val (C', qs) = strip_comb t'
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    78
          in
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    79
            if C = C'
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    80
            then flat (map2 (pattern_subtract_subst_aux vs) ps qs)
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    81
            else raise DISJ
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    82
          end
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    83
    in
20636
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    84
      pattern_subtract_subst_aux vs t t'
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    85
      handle DISJ => [(vs, [])]
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    86
    end
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    87
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    88
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    89
(* p - q *)
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    90
fun pattern_subtract ctx eq2 eq1 =
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    91
    let
20636
ddddf0b7d322 Fixed error in pattern splitting algorithm
krauss
parents: 20523
diff changeset
    92
      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
    93
      
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    94
      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
    95
      val (_,  _ $ (_ $ lhs2 $ _)) = dest_all_all eq2
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
    96
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    97
      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
    98
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
    99
      fun instantiate (vs', sigma) =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   100
          let
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   101
            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
   102
          in
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   103
            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
   104
          end
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   105
    in
20654
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20636
diff changeset
   106
      map instantiate substs
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   107
    end
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   108
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   109
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   110
(* ps - p' *)
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   111
fun pattern_subtract_from_many ctx p'=
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   112
    flat o map (pattern_subtract ctx p')
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   113
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   114
(* in reverse order *)
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   115
fun pattern_subtract_many ctx ps' =
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   116
    fold_rev (pattern_subtract_from_many ctx) ps'
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   117
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
fun split_some_equations ctx eqns =
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   121
    let
20338
ecdfc96cf4d0 Added Keywords: "otherwise" and "sequential", needed for function package's
krauss
parents: 20289
diff changeset
   122
      fun split_aux prev [] = []
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   123
        | 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
   124
                                              :: split_aux (eq :: prev) es
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   125
        | 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
   126
                                               :: split_aux (eq :: prev) es
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   127
    in
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   128
      split_aux [] eqns
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   129
    end
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   130
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   131
fun split_all_equations ctx =
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20344
diff changeset
   132
    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
   133
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
20270
3abe7dae681e Function package can now do automatic splits of overlapping datatype patterns
krauss
parents:
diff changeset
   137
end