src/HOL/Tools/function_package/fundef_common.ML
changeset 23819 2040846d1bbe
parent 23766 77e796fe89eb
child 24039 273698405054
equal deleted inserted replaced
23818:cfe8d4bf749a 23819:2040846d1bbe
    23 
    23 
    24 val function_name = suffix "C"
    24 val function_name = suffix "C"
    25 val graph_name = suffix "_graph"
    25 val graph_name = suffix "_graph"
    26 val rel_name = suffix "_rel"
    26 val rel_name = suffix "_rel"
    27 val dom_name = suffix "_dom"
    27 val dom_name = suffix "_dom"
    28 
       
    29 type depgraph = int IntGraph.T
       
    30 
       
    31 datatype ctx_tree 
       
    32   = Leaf of term
       
    33   | Cong of (term * thm * depgraph * ((string * typ) list * thm list * ctx_tree) list)
       
    34   | RCall of (term * ctx_tree)
       
    35 
       
    36 
    28 
    37 
    29 
    38 datatype fundef_result =
    30 datatype fundef_result =
    39   FundefResult of
    31   FundefResult of
    40      {
    32      {
    56 datatype fundef_context_data =
    48 datatype fundef_context_data =
    57   FundefCtxData of
    49   FundefCtxData of
    58      {
    50      {
    59       defname : string,
    51       defname : string,
    60 
    52 
       
    53       (* contains no logical entities: invariant under morphisms *)
    61       add_simps : string -> Attrib.src list -> thm list -> local_theory -> thm list * local_theory,
    54       add_simps : string -> Attrib.src list -> thm list -> local_theory -> thm list * local_theory,
    62 
    55 
    63       fs : term list,
    56       fs : term list,
    64       R : term,
    57       R : term,
    65       
    58       
    71 fun morph_fundef_data phi (FundefCtxData {add_simps, fs, R, psimps, pinducts, termination, defname}) =
    64 fun morph_fundef_data phi (FundefCtxData {add_simps, fs, R, psimps, pinducts, termination, defname}) =
    72     let
    65     let
    73       val term = Morphism.term phi val thm = Morphism.thm phi val fact = Morphism.fact phi
    66       val term = Morphism.term phi val thm = Morphism.thm phi val fact = Morphism.fact phi
    74       val name = Morphism.name phi
    67       val name = Morphism.name phi
    75     in
    68     in
    76       FundefCtxData { add_simps = add_simps (* contains no logical entities *), 
    69       FundefCtxData { add_simps = add_simps,
    77                       fs = map term fs, R = term R, psimps = fact psimps, 
    70                       fs = map term fs, R = term R, psimps = fact psimps, 
    78                       pinducts = fact pinducts, termination = thm termination,
    71                       pinducts = fact pinducts, termination = thm termination,
    79                       defname = name defname }
    72                       defname = name defname }
    80     end
    73     end
    81 
    74 
   152 
   145 
   153 fun add_fundef_data (data as FundefCtxData {fs, termination, ...}) =
   146 fun add_fundef_data (data as FundefCtxData {fs, termination, ...}) =
   154     FundefData.map (fold (fn f => NetRules.insert (f, data)) fs)
   147     FundefData.map (fold (fn f => NetRules.insert (f, data)) fs)
   155     #> store_termination_rule termination
   148     #> store_termination_rule termination
   156 
   149 
   157 
       
   158 (* Configuration management *)
   150 (* Configuration management *)
   159 datatype fundef_opt 
   151 datatype fundef_opt 
   160   = Sequential
   152   = Sequential
   161   | Default of string
   153   | Default of string
   162   | Target of xstring
   154   | Target of xstring
   183     FundefConfig {sequential=sequential, default=default, target=target, domintros=true,tailrec=tailrec}
   175     FundefConfig {sequential=sequential, default=default, target=target, domintros=true,tailrec=tailrec}
   184   | apply_opt Tailrec (FundefConfig {sequential, default, target, domintros,tailrec}) =
   176   | apply_opt Tailrec (FundefConfig {sequential, default, target, domintros,tailrec}) =
   185     FundefConfig {sequential=sequential, default=default, target=target, domintros=domintros,tailrec=true}
   177     FundefConfig {sequential=sequential, default=default, target=target, domintros=domintros,tailrec=true}
   186 
   178 
   187 fun target_of (FundefConfig {target, ...}) = target
   179 fun target_of (FundefConfig {target, ...}) = target
       
   180 
       
   181 val default_config = FundefConfig { sequential=false, default="%x. arbitrary", 
       
   182                                     target=NONE, domintros=false, tailrec=false }
       
   183 
   188 
   184 
   189 (* Common operations on equations *)
   185 (* Common operations on equations *)
   190 
   186 
   191 fun open_all_all (Const ("all", _) $ Abs (n, T, b)) = apfst (cons (n, T)) (open_all_all b)
   187 fun open_all_all (Const ("all", _) $ Abs (n, T, b)) = apfst (cons (n, T)) (open_all_all b)
   192   | open_all_all t = ([], t)
   188   | open_all_all t = ([], t)
   246                         |> map (fst o nth (rev qs))
   242                         |> map (fst o nth (rev qs))
   247                       
   243                       
   248             val _ = null rvs orelse error (input_error ("Variable" ^ plural " " "s " rvs ^ commas_quote rvs
   244             val _ = null rvs orelse error (input_error ("Variable" ^ plural " " "s " rvs ^ commas_quote rvs
   249                                                         ^ " occur" ^ plural "s" "" rvs ^ " on right hand side only:"))
   245                                                         ^ " occur" ^ plural "s" "" rvs ^ " on right hand side only:"))
   250                                     
   246                                     
   251             val _ = forall (FundefLib.forall_aterms (fn Free (n, _) => not (n mem fnames) | _ => true)) gs 
   247             val _ = forall (not o Term.exists_subterm (fn Free (n, _) => n mem fnames | _ => false)) gs 
   252                     orelse error (input_error "Recursive Calls not allowed in premises")
   248                     orelse error (input_error "Recursive Calls not allowed in premises")
   253           in
   249           in
   254             fqgar
   250             fqgar
   255           end
   251           end
   256 
   252 
   263 
   259 
   264 (* Preprocessors *)
   260 (* Preprocessors *)
   265 
   261 
   266 type fixes = ((string * typ) * mixfix) list
   262 type fixes = ((string * typ) * mixfix) list
   267 type 'a spec = ((bstring * Attrib.src list) * 'a list) list
   263 type 'a spec = ((bstring * Attrib.src list) * 'a list) list
   268 type preproc = fundef_config -> bool list -> Proof.context -> fixes -> term spec -> (term list * (thm list -> thm spec))
   264 type preproc = fundef_config -> bool list -> Proof.context -> fixes -> term spec 
       
   265                -> (term list * (thm list -> thm spec) * (thm list -> thm list list))
       
   266 
       
   267 val fname_of = fst o dest_Free o fst o strip_comb o fst o HOLogic.dest_eq o HOLogic.dest_Trueprop o Logic.strip_imp_concl o snd o dest_all_all
   269 
   268 
   270 fun empty_preproc check _ _ ctxt fixes spec =
   269 fun empty_preproc check _ _ ctxt fixes spec =
   271     let 
   270     let 
   272       val (nas,tss) = split_list spec
   271       val (nas,tss) = split_list spec
   273       val _ = check ctxt fixes (flat tss)
   272       val _ = check ctxt fixes (flat tss)
   274     in
   273       val ts = flat tss
   275       (flat tss, curry op ~~ nas o Library.unflat tss)
   274       val fnames = map (fst o fst) fixes
       
   275       val indices = map (fn eq => find_index (curry op = (fname_of eq)) fnames) ts
       
   276 
       
   277       fun sort xs = partition_list (fn i => fn (j,_) => i = j) 0 (length fnames - 1) (indices ~~ xs)
       
   278                         |> map (map snd)
       
   279     in
       
   280       (ts, curry op ~~ nas o Library.unflat tss, sort)
   276     end
   281     end
   277 
   282 
   278 structure Preprocessor = GenericDataFun
   283 structure Preprocessor = GenericDataFun
   279 (
   284 (
   280   type T = preproc
   285   type T = preproc
   311 
   316 
   312   val statements_ow = P.enum1 "|" statement_ow
   317   val statements_ow = P.enum1 "|" statement_ow
   313 
   318 
   314   val flags_statements = statements_ow
   319   val flags_statements = statements_ow
   315                          >> (fn sow => (map (snd o snd) sow, map (apsnd fst) sow))
   320                          >> (fn sow => (map (snd o snd) sow, map (apsnd fst) sow))
   316 
       
   317   fun basic_apply_flags ((config, fixes), (flags, statements)) = ((config, fixes), statements)
       
   318 in
   321 in
   319   fun fundef_parser default_cfg = (config_parser default_cfg -- P.fixes --| P.$$$ "where" -- flags_statements)
   322   fun fundef_parser default_cfg = (config_parser default_cfg -- P.fixes --| P.$$$ "where" -- flags_statements)
   320                                   
       
   321 end
   323 end
   322 
       
   323 
       
   324 
       
   325 
       
   326 
       
   327 val default_config = FundefConfig { sequential=false, default="%x. arbitrary", 
       
   328                                     target=NONE, domintros=false, tailrec=false }
       
   329 
   324 
   330 
   325 
   331 end
   326 end
   332 end
   327 end
   333 
   328 
   334 (* Common Abbreviations *)
       
   335 
       
   336 structure FundefAbbrev =
       
   337 struct
       
   338 
       
   339 fun implies_elim_swp x y = implies_elim y x
       
   340 
       
   341 (* HOL abbreviations *)
       
   342 val boolT = HOLogic.boolT
       
   343 val mk_prod = HOLogic.mk_prod
       
   344 val mk_eq = HOLogic.mk_eq
       
   345 val eq_const = HOLogic.eq_const
       
   346 val Trueprop = HOLogic.mk_Trueprop
       
   347 val mk_relT = HOLogic.mk_setT o HOLogic.mk_prodT
       
   348 
       
   349 fun free_to_var (Free (v,T)) = Var ((v,0),T)
       
   350   | free_to_var _ = raise Match
       
   351 
       
   352 fun var_to_free (Var ((v,_),T)) = Free (v,T)
       
   353   | var_to_free _ = raise Match
       
   354 
       
   355 
       
   356 end