src/HOL/Tools/Function/function_core.ML
changeset 63011 301e631666a0
parent 63005 d743bb1b6c23
child 63352 4eaf35781b23
equal deleted inserted replaced
63010:8e0378864478 63011:301e631666a0
     7 signature FUNCTION_CORE =
     7 signature FUNCTION_CORE =
     8 sig
     8 sig
     9   val trace: bool Unsynchronized.ref
     9   val trace: bool Unsynchronized.ref
    10   val prepare_function : Function_Common.function_config
    10   val prepare_function : Function_Common.function_config
    11     -> binding (* defname *)
    11     -> binding (* defname *)
    12     -> ((bstring * typ) * mixfix) list (* defined symbol *)
    12     -> ((binding * typ) * mixfix) list (* defined symbol *)
    13     -> ((bstring * typ) list * term list * term * term) list (* specification *)
    13     -> ((string * typ) list * term list * term * term) list (* specification *)
    14     -> local_theory
    14     -> local_theory
    15     -> (term   (* f *)
    15     -> (term   (* f *)
    16         * thm  (* goalstate *)
    16         * thm  (* goalstate *)
    17         * (Proof.context -> thm -> Function_Common.function_result) (* continuation *)
    17         * (Proof.context -> thm -> Function_Common.function_result) (* continuation *)
    18        ) * local_theory
    18        ) * local_theory
   497       Abs ("x", domT, Const (@{const_name Fun_Def.THE_default}, ranT --> (ranT --> boolT) --> ranT)
   497       Abs ("x", domT, Const (@{const_name Fun_Def.THE_default}, ranT --> (ranT --> boolT) --> ranT)
   498         $ (default $ Bound 0) $ Abs ("y", ranT, G $ Bound 1 $ Bound 0))
   498         $ (default $ Bound 0) $ Abs ("y", ranT, G $ Bound 1 $ Bound 0))
   499       |> Syntax.check_term lthy
   499       |> Syntax.check_term lthy
   500   in
   500   in
   501     lthy |> Local_Theory.define
   501     lthy |> Local_Theory.define
   502       ((Binding.name (fname ^ "C") (* FIXME proper binding *), mixfix), ((f_def_binding, []), f_def))
   502       ((Binding.map_name (suffix "C") fname, mixfix), ((f_def_binding, []), f_def))
   503   end
   503   end
   504 
   504 
   505 fun define_recursion_relation (R_binding, R_type) qglrs clauses RCss lthy =
   505 fun define_recursion_relation (R_binding, R_type) qglrs clauses RCss lthy =
   506   let
   506   let
   507     val R = Free (Binding.name_of R_binding, R_type)
   507     val R = Free (Binding.name_of R_binding, R_type)
   824 
   824 
   825 fun prepare_function config defname [((fname, fT), mixfix)] abstract_qglrs lthy =
   825 fun prepare_function config defname [((fname, fT), mixfix)] abstract_qglrs lthy =
   826   let
   826   let
   827     val FunctionConfig {domintros, default=default_opt, ...} = config
   827     val FunctionConfig {domintros, default=default_opt, ...} = config
   828 
   828 
   829     val default_str = the_default "%x. HOL.undefined" default_opt
   829     val default_str = the_default "%x. HOL.undefined" (* FIXME proper term!? *) default_opt
   830     val fvar = Free (fname, fT)
   830     val fvar = (Binding.name_of fname, fT)
   831     val domT = domain_type fT
   831     val domT = domain_type fT
   832     val ranT = range_type fT
   832     val ranT = range_type fT
   833 
   833 
   834     val default = Syntax.parse_term lthy default_str
   834     val default = Syntax.parse_term lthy default_str
   835       |> Type.constraint fT |> Syntax.check_term lthy
   835       |> Type.constraint fT |> Syntax.check_term lthy
   836 
   836 
   837     val (globals, ctxt') = fix_globals domT ranT fvar lthy
   837     val (globals, ctxt') = fix_globals domT ranT (Free fvar) lthy
   838 
   838 
   839     val Globals { x, h, ... } = globals
   839     val Globals { x, h, ... } = globals
   840 
   840 
   841     val clauses = map (mk_clause_context x ctxt') abstract_qglrs
   841     val clauses = map (mk_clause_context x ctxt') abstract_qglrs
   842 
   842 
   843     val n = length abstract_qglrs
   843     val n = length abstract_qglrs
   844 
   844 
   845     fun build_tree (ClauseContext { ctxt, rhs, ...}) =
   845     fun build_tree (ClauseContext { ctxt, rhs, ...}) =
   846        Function_Context_Tree.mk_tree (fname, fT) h ctxt rhs
   846        Function_Context_Tree.mk_tree fvar h ctxt rhs
   847 
   847 
   848     val trees = map build_tree clauses
   848     val trees = map build_tree clauses
   849     val RCss = map find_calls trees
   849     val RCss = map find_calls trees
   850 
   850 
   851     val ((G, GIntro_thms, G_elim, G_induct), lthy) =
   851     val ((G, GIntro_thms, G_elim, G_induct), lthy) =
   852       PROFILE "def_graph"
   852       PROFILE "def_graph"
   853         (define_graph
   853         (define_graph
   854           (derived_name_suffix defname "_graph", domT --> ranT --> boolT) fvar clauses RCss) lthy
   854           (derived_name_suffix defname "_graph", domT --> ranT --> boolT) (Free fvar) clauses RCss)
       
   855         lthy
   855 
   856 
   856     val ((f, (_, f_defthm)), lthy) =
   857     val ((f, (_, f_defthm)), lthy) =
   857       PROFILE "def_fun" (define_function defname (fname, mixfix) domT ranT G default) lthy
   858       PROFILE "def_fun" (define_function defname (fname, mixfix) domT ranT G default) lthy
   858 
   859 
   859     val RCss = map (map (inst_RC lthy fvar f)) RCss
   860     val RCss = map (map (inst_RC lthy (Free fvar) f)) RCss
   860     val trees = map (Function_Context_Tree.inst_tree lthy fvar f) trees
   861     val trees = map (Function_Context_Tree.inst_tree lthy (Free fvar) f) trees
   861 
   862 
   862     val ((R, RIntro_thmss, R_elim), lthy) =
   863     val ((R, RIntro_thmss, R_elim), lthy) =
   863       PROFILE "def_rel"
   864       PROFILE "def_rel"
   864         (define_recursion_relation (derived_name_suffix defname "_rel", domT --> domT --> boolT)
   865         (define_recursion_relation (derived_name_suffix defname "_rel", domT --> domT --> boolT)
   865           abstract_qglrs clauses RCss) lthy
   866           abstract_qglrs clauses RCss) lthy
   866 
   867 
   867     val dom = mk_acc domT R
   868     val dom = mk_acc domT R
   868     val (_, lthy) =
   869     val (_, lthy) =
   869       Local_Theory.abbrev Syntax.mode_default
   870       Local_Theory.abbrev Syntax.mode_default
   870         ((name_suffix defname "_dom", NoSyn), dom) lthy
   871         ((derived_name_suffix defname "_dom", NoSyn), dom) lthy
   871 
   872 
   872     val newthy = Proof_Context.theory_of lthy
   873     val newthy = Proof_Context.theory_of lthy
   873     val clauses = map (transfer_clause_ctx newthy) clauses
   874     val clauses = map (transfer_clause_ctx newthy) clauses
   874 
   875 
   875     val xclauses = PROFILE "xclauses"
   876     val xclauses = PROFILE "xclauses"
   878 
   879 
   879     val complete =
   880     val complete =
   880       mk_completeness globals clauses abstract_qglrs |> Thm.cterm_of lthy |> Thm.assume
   881       mk_completeness globals clauses abstract_qglrs |> Thm.cterm_of lthy |> Thm.assume
   881 
   882 
   882     val compat =
   883     val compat =
   883       mk_compat_proof_obligations domT ranT fvar f abstract_qglrs
   884       mk_compat_proof_obligations domT ranT (Free fvar) f abstract_qglrs
   884       |> map (Thm.cterm_of lthy #> Thm.assume)
   885       |> map (Thm.cterm_of lthy #> Thm.assume)
   885 
   886 
   886     val compat_store = store_compat_thms n compat
   887     val compat_store = store_compat_thms n compat
   887 
   888 
   888     val (goalstate, values) = PROFILE "prove_stuff"
   889     val (goalstate, values) = PROFILE "prove_stuff"
   919       end
   920       end
   920   in
   921   in
   921     ((f, goalstate, mk_partial_rules), lthy)
   922     ((f, goalstate, mk_partial_rules), lthy)
   922   end
   923   end
   923 
   924 
   924 
       
   925 end
   925 end