src/HOL/Tools/function_package/pattern_split.ML
author krauss
Thu Sep 21 12:22:05 2006 +0200 (2006-09-21)
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.
krauss@20270
     1
(*  Title:      HOL/Tools/function_package/fundef_package.ML
krauss@20270
     2
    ID:         $Id$
krauss@20270
     3
    Author:     Alexander Krauss, TU Muenchen
krauss@20270
     4
wenzelm@20344
     5
A package for general recursive function definitions.
krauss@20270
     6
wenzelm@20344
     7
Automatic splitting of overlapping constructor patterns. This is a preprocessing step which
krauss@20270
     8
turns a specification with overlaps into an overlap-free specification.
krauss@20270
     9
krauss@20270
    10
*)
krauss@20270
    11
wenzelm@20344
    12
signature FUNDEF_SPLIT =
krauss@20270
    13
sig
wenzelm@20289
    14
  val split_some_equations :
krauss@20523
    15
    Proof.context -> (bool * term) list -> term list list
krauss@20270
    16
krauss@20523
    17
  val split_all_equations :
krauss@20523
    18
    Proof.context -> term list -> term list list
krauss@20270
    19
end
krauss@20270
    20
wenzelm@20344
    21
structure FundefSplit : FUNDEF_SPLIT =
krauss@20270
    22
struct
krauss@20270
    23
krauss@20270
    24
krauss@20270
    25
(* We use proof context for the variable management *)
krauss@20270
    26
(* FIXME: no __ *)
krauss@20270
    27
wenzelm@20344
    28
fun new_var ctx vs T =
wenzelm@20344
    29
    let
krauss@20270
    30
      val [v] = Variable.variant_frees ctx vs [("v", T)]
krauss@20270
    31
    in
krauss@20270
    32
      (Free v :: vs, Free v)
krauss@20270
    33
    end
krauss@20270
    34
krauss@20270
    35
fun saturate ctx vs t =
krauss@20270
    36
    fold (fn T => fn (vs, t) => new_var ctx vs T |> apsnd (curry op $ t))
krauss@20270
    37
         (binder_types (fastype_of t)) (vs, t)
krauss@20270
    38
krauss@20270
    39
krauss@20270
    40
(* This is copied from "fundef_datatype.ML" *)
krauss@20270
    41
fun inst_constrs_of thy (T as Type (name, _)) =
wenzelm@20344
    42
        map (fn (Cn,CT) => Envir.subst_TVars (Sign.typ_match thy (body_type CT, T) Vartab.empty) (Const (Cn, CT)))
wenzelm@20344
    43
            (the (DatatypePackage.get_datatype_constrs thy name))
krauss@20270
    44
  | inst_constrs_of thy t = (print t; sys_error "inst_constrs_of")
krauss@20270
    45
krauss@20270
    46
krauss@20270
    47
krauss@20636
    48
krauss@20636
    49
fun join ((vs1,sub1), (vs2,sub2)) = (merge (op aconv) (vs1,vs2), sub1 @ sub2)
krauss@20636
    50
fun join_product (xs, ys) = map join (product xs ys)
krauss@20636
    51
krauss@20636
    52
fun join_list [] = []
krauss@20636
    53
  | join_list xs = foldr1 (join_product) xs
krauss@20636
    54
krauss@20636
    55
krauss@20636
    56
exception DISJ
krauss@20636
    57
krauss@20636
    58
fun pattern_subtract_subst ctx vs t t' =
wenzelm@20344
    59
    let
krauss@20636
    60
      exception DISJ
krauss@20636
    61
      fun pattern_subtract_subst_aux vs _ (Free v2) = []
krauss@20636
    62
        | pattern_subtract_subst_aux vs (v as (Free (_, T))) t' =
wenzelm@20344
    63
          let
krauss@20636
    64
            fun foo constr =
krauss@20636
    65
                let
krauss@20636
    66
                  val (vs', t) = saturate ctx vs constr
krauss@20636
    67
                  val substs = pattern_subtract_subst ctx vs' t t'
krauss@20636
    68
                in
krauss@20636
    69
                  map (fn (vs, subst) => (vs, (v,t)::subst)) substs
krauss@20636
    70
                end
krauss@20270
    71
          in
krauss@20636
    72
            flat (map foo (inst_constrs_of (ProofContext.theory_of ctx) T))
krauss@20636
    73
          end
krauss@20636
    74
        | pattern_subtract_subst_aux vs t t' =
krauss@20636
    75
          let
krauss@20636
    76
            val (C, ps) = strip_comb t
krauss@20636
    77
            val (C', qs) = strip_comb t'
krauss@20636
    78
          in
krauss@20636
    79
            if C = C'
krauss@20636
    80
            then flat (map2 (pattern_subtract_subst_aux vs) ps qs)
krauss@20636
    81
            else raise DISJ
krauss@20270
    82
          end
krauss@20270
    83
    in
krauss@20636
    84
      pattern_subtract_subst_aux vs t t'
krauss@20636
    85
      handle DISJ => [(vs, [])]
krauss@20270
    86
    end
krauss@20270
    87
krauss@20270
    88
krauss@20523
    89
(* p - q *)
krauss@20270
    90
fun pattern_subtract ctx eq2 eq1 =
krauss@20270
    91
    let
krauss@20636
    92
      val thy = ProofContext.theory_of ctx
krauss@20523
    93
      
krauss@20523
    94
      val (vs, feq1 as (_ $ (_ $ lhs1 $ _))) = dest_all_all eq1
krauss@20523
    95
      val (_,  _ $ (_ $ lhs2 $ _)) = dest_all_all eq2
krauss@20270
    96
krauss@20523
    97
      val substs = pattern_subtract_subst ctx vs lhs1 lhs2
krauss@20523
    98
krauss@20523
    99
      fun instantiate (vs', sigma) =
krauss@20523
   100
          let
krauss@20523
   101
            val t = Pattern.rewrite_term thy sigma [] feq1
krauss@20523
   102
          in
krauss@20523
   103
            fold_rev mk_forall (map Free (frees_in_term ctx t) inter vs') t
krauss@20523
   104
          end
krauss@20270
   105
    in
krauss@20654
   106
      map instantiate substs
krauss@20270
   107
    end
krauss@20270
   108
krauss@20270
   109
krauss@20270
   110
(* ps - p' *)
krauss@20270
   111
fun pattern_subtract_from_many ctx p'=
krauss@20270
   112
    flat o map (pattern_subtract ctx p')
krauss@20270
   113
krauss@20270
   114
(* in reverse order *)
krauss@20270
   115
fun pattern_subtract_many ctx ps' =
krauss@20270
   116
    fold_rev (pattern_subtract_from_many ctx) ps'
krauss@20270
   117
krauss@20270
   118
krauss@20270
   119
krauss@20270
   120
fun split_some_equations ctx eqns =
krauss@20270
   121
    let
krauss@20338
   122
      fun split_aux prev [] = []
krauss@20523
   123
        | split_aux prev ((true, eq) :: es) = pattern_subtract_many ctx prev [eq]
krauss@20523
   124
                                              :: split_aux (eq :: prev) es
krauss@20523
   125
        | split_aux prev ((false, eq) :: es) = [eq]
krauss@20523
   126
                                               :: split_aux (eq :: prev) es
krauss@20270
   127
    in
krauss@20270
   128
      split_aux [] eqns
krauss@20270
   129
    end
krauss@20270
   130
krauss@20523
   131
fun split_all_equations ctx =
krauss@20523
   132
    split_some_equations ctx o map (pair true)
krauss@20523
   133
krauss@20523
   134
krauss@20523
   135
krauss@20523
   136
krauss@20270
   137
end