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