src/HOL/Tools/function_package/pattern_split.ML
changeset 20344 d02b43ea722e
parent 20338 ecdfc96cf4d0
child 20523 36a59e5d0039
equal deleted inserted replaced
20343:e093a54bf25e 20344:d02b43ea722e
     1 (*  Title:      HOL/Tools/function_package/fundef_package.ML
     1 (*  Title:      HOL/Tools/function_package/fundef_package.ML
     2     ID:         $Id$
     2     ID:         $Id$
     3     Author:     Alexander Krauss, TU Muenchen
     3     Author:     Alexander Krauss, TU Muenchen
     4 
     4 
     5 A package for general recursive function definitions. 
     5 A package for general recursive function definitions.
     6 
     6 
     7 Automatic splitting of overlapping constructor patterns. This is a preprocessing step which 
     7 Automatic splitting of overlapping constructor patterns. This is a preprocessing step which
     8 turns a specification with overlaps into an overlap-free specification.
     8 turns a specification with overlaps into an overlap-free specification.
     9 
     9 
    10 *)
    10 *)
    11 
    11 
    12 signature FUNDEF_SPLIT = 
    12 signature FUNDEF_SPLIT =
    13 sig
    13 sig
    14   val split_some_equations :
    14   val split_some_equations :
    15     Proof.context -> (('a * ('b * bool)) * term) list -> (('a * 'b) * term list) list
    15     Proof.context -> (('a * ('b * bool)) * term) list -> (('a * 'b) * term list) list
    16 
    16 
    17 end
    17 end
    18 
    18 
    19 structure FundefSplit : FUNDEF_SPLIT = 
    19 structure FundefSplit : FUNDEF_SPLIT =
    20 struct
    20 struct
    21 
    21 
    22 
    22 
    23 (* We use proof context for the variable management *)
    23 (* We use proof context for the variable management *)
    24 (* FIXME: no __ *)
    24 (* FIXME: no __ *)
    25 
    25 
    26 fun new_var ctx vs T = 
    26 fun new_var ctx vs T =
    27     let 
    27     let
    28       val [v] = Variable.variant_frees ctx vs [("v", T)]
    28       val [v] = Variable.variant_frees ctx vs [("v", T)]
    29     in
    29     in
    30       (Free v :: vs, Free v)
    30       (Free v :: vs, Free v)
    31     end
    31     end
    32 
    32 
    35          (binder_types (fastype_of t)) (vs, t)
    35          (binder_types (fastype_of t)) (vs, t)
    36 
    36 
    37 
    37 
    38 (* This is copied from "fundef_datatype.ML" *)
    38 (* This is copied from "fundef_datatype.ML" *)
    39 fun inst_constrs_of thy (T as Type (name, _)) =
    39 fun inst_constrs_of thy (T as Type (name, _)) =
    40 	map (fn (Cn,CT) => Envir.subst_TVars (Type.typ_match (Sign.tsig_of thy) (body_type CT, T) Vartab.empty) (Const (Cn, CT)))
    40         map (fn (Cn,CT) => Envir.subst_TVars (Sign.typ_match thy (body_type CT, T) Vartab.empty) (Const (Cn, CT)))
    41 	    (the (DatatypePackage.get_datatype_constrs thy name))
    41             (the (DatatypePackage.get_datatype_constrs thy name))
    42   | inst_constrs_of thy t = (print t; sys_error "inst_constrs_of")
    42   | inst_constrs_of thy t = (print t; sys_error "inst_constrs_of")
    43 
    43 
    44 
    44 
    45 
    45 
    46 fun pattern_subtract_subst ctx vs _ (Free v2) = []
    46 fun pattern_subtract_subst ctx vs _ (Free v2) = []
    47   | pattern_subtract_subst ctx vs (v as (Free (_, T))) t' =
    47   | pattern_subtract_subst ctx vs (v as (Free (_, T))) t' =
    48     let 
    48     let
    49       fun foo constr = 
    49       fun foo constr =
    50           let 
    50           let
    51             val (vs', t) = saturate ctx vs constr
    51             val (vs', t) = saturate ctx vs constr
    52             val substs = pattern_subtract_subst ctx vs' t t'
    52             val substs = pattern_subtract_subst ctx vs' t t'
    53           in
    53           in
    54             map (cons (v, t)) substs
    54             map (cons (v, t)) substs
    55           end
    55           end
    93     fold_rev (pattern_subtract_from_many ctx) ps'
    93     fold_rev (pattern_subtract_from_many ctx) ps'
    94 
    94 
    95 
    95 
    96 
    96 
    97 fun split_all_equations ctx eqns =
    97 fun split_all_equations ctx eqns =
    98     let 
    98     let
    99       fun split_aux prev [] = []
    99       fun split_aux prev [] = []
   100         | split_aux prev (e::es) = pattern_subtract_many ctx prev [e] @ split_aux (e::prev) es
   100         | split_aux prev (e::es) = pattern_subtract_many ctx prev [e] @ split_aux (e::prev) es
   101     in
   101     in
   102       split_aux [] eqns
   102       split_aux [] eqns
   103 end
   103 end
   106 fun split_some_equations ctx eqns =
   106 fun split_some_equations ctx eqns =
   107     let
   107     let
   108       fun split_aux prev [] = []
   108       fun split_aux prev [] = []
   109         | split_aux prev (((n, (att, true)), eq) :: es) = ((n, att), pattern_subtract_many ctx prev [eq])
   109         | split_aux prev (((n, (att, true)), eq) :: es) = ((n, att), pattern_subtract_many ctx prev [eq])
   110                                                           :: split_aux (eq :: prev) es
   110                                                           :: split_aux (eq :: prev) es
   111         | split_aux prev (((n, (att, false)), eq) :: es) = ((n, att), [eq]) 
   111         | split_aux prev (((n, (att, false)), eq) :: es) = ((n, att), [eq])
   112                                                            :: split_aux (eq :: prev) es
   112                                                                 :: split_aux (eq :: prev) es
   113     in
   113     in
   114       split_aux [] eqns
   114       split_aux [] eqns
   115     end
   115     end
   116 
   116 
   117 end
   117 end