src/HOL/Tools/function_package/fundef_package.ML
author wenzelm
Sat, 28 Mar 2009 17:21:11 +0100
changeset 30761 ac7570d80c3d
parent 30528 7173bf123335
child 30787 5b7a5a05c7aa
permissions -rw-r--r--
renamed ProofContext.note_thmss_i to ProofContext.note_thmss, eliminated obsolete external version;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     1
(*  Title:      HOL/Tools/function_package/fundef_package.ML
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     2
    Author:     Alexander Krauss, TU Muenchen
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     3
20363
f34c5dbe74d5 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20338
diff changeset
     4
A package for general recursive function definitions.
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     5
Isar commands.
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     6
*)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     7
20363
f34c5dbe74d5 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20338
diff changeset
     8
signature FUNDEF_PACKAGE =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     9
sig
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
    10
    val add_fundef :  (binding * typ option * mixfix) list
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
    11
                       -> (Attrib.binding * term) list
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
    12
                       -> FundefCommon.fundef_config
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
    13
                       -> bool list
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
    14
                       -> local_theory
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
    15
                       -> Proof.state
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
    16
    val add_fundef_cmd :  (binding * string option * mixfix) list
28084
a05ca48ef263 type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents: 28083
diff changeset
    17
                      -> (Attrib.binding * string) list 
20877
368b997ad67e removed with_local_path -- LocalTheory.note admits qualified names;
wenzelm
parents: 20654
diff changeset
    18
                      -> FundefCommon.fundef_config
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    19
                      -> bool list
20877
368b997ad67e removed with_local_path -- LocalTheory.note admits qualified names;
wenzelm
parents: 20654
diff changeset
    20
                      -> local_theory
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22725
diff changeset
    21
                      -> Proof.state
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20363
diff changeset
    22
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
    23
    val termination_proof : term option -> local_theory -> Proof.state
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
    24
    val termination_proof_cmd : string option -> local_theory -> Proof.state
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
    25
    val termination : term option -> local_theory -> Proof.state
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
    26
    val termination_cmd : string option -> local_theory -> Proof.state
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    27
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    28
    val setup : theory -> theory
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
    29
    val get_congs : Proof.context -> thm list
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    30
end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    31
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    32
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
    33
structure FundefPackage : FUNDEF_PACKAGE =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    34
struct
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    35
21051
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 21000
diff changeset
    36
open FundefLib
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    37
open FundefCommon
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    38
30435
e62d6ecab6ad explicit Binding.qualified_name -- prevents implicitly qualified bstring;
wenzelm
parents: 30364
diff changeset
    39
fun note_theorem ((name, atts), ths) =
e62d6ecab6ad explicit Binding.qualified_name -- prevents implicitly qualified bstring;
wenzelm
parents: 30364
diff changeset
    40
  LocalTheory.note Thm.theoremK ((Binding.qualified_name name, atts), ths)
21435
883337ea2f3b LocalTheory.axioms/notes/defs: proper kind;
wenzelm
parents: 21391
diff changeset
    41
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 22102
diff changeset
    42
fun mk_defname fixes = fixes |> map (fst o fst) |> space_implode "_" 
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 22102
diff changeset
    43
26529
03ad378ed5f0 Function package no longer overwrites theorems.
krauss
parents: 26171
diff changeset
    44
fun add_simps fnames post sort extra_qualify label moreatts simps lthy =
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    45
    let
29866
6e93ae65c678 Added Nitpick_Const_Psimp attribute, dropped the 's' in Nitpick_Const_Simps, and killed the Nitpick_Ind_Intros attribute.
blanchet
parents: 29863
diff changeset
    46
      val atts = Attrib.internal (K Simplifier.simp_add) :: moreatts
23819
2040846d1bbe some interface cleanup
krauss
parents: 23473
diff changeset
    47
      val spec = post simps
2040846d1bbe some interface cleanup
krauss
parents: 23473
diff changeset
    48
                   |> map (apfst (apsnd (append atts)))
26529
03ad378ed5f0 Function package no longer overwrites theorems.
krauss
parents: 26171
diff changeset
    49
                   |> map (apfst (apfst extra_qualify))
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21240
diff changeset
    50
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 22102
diff changeset
    51
      val (saved_spec_simps, lthy) =
23819
2040846d1bbe some interface cleanup
krauss
parents: 23473
diff changeset
    52
        fold_map note_theorem spec lthy
2040846d1bbe some interface cleanup
krauss
parents: 23473
diff changeset
    53
22498
62cdd4b3e96b made function syntax strict, requiring | to separate equations; cleanup
krauss
parents: 22325
diff changeset
    54
      val saved_simps = flat (map snd saved_spec_simps)
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 22102
diff changeset
    55
      val simps_by_f = sort saved_simps
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    56
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 22102
diff changeset
    57
      fun add_for_f fname simps =
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30223
diff changeset
    58
        note_theorem ((Long_Name.qualify fname label, []), simps) #> snd
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    59
    in
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 22102
diff changeset
    60
      (saved_simps,
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 22102
diff changeset
    61
       fold2 add_for_f fnames simps_by_f lthy)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    62
    end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    63
26594
1c676ae50311 fundef_afterqed: removed unused config, added do_print flag;
wenzelm
parents: 26580
diff changeset
    64
fun fundef_afterqed do_print fixes post defname cont sort_cont cnames [[proof]] lthy =
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20363
diff changeset
    65
    let
26529
03ad378ed5f0 Function package no longer overwrites theorems.
krauss
parents: 26171
diff changeset
    66
      val FundefResult {fs, R, psimps, trsimps,  simple_pinducts, termination, domintros, cases, ...} = 
26628
63306cb94313 replaced Drule.close_derivation/Goal.close_result by Thm.close_derivation (removed obsolete compression);
wenzelm
parents: 26594
diff changeset
    67
          cont (Thm.close_derivation proof)
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 22102
diff changeset
    68
23819
2040846d1bbe some interface cleanup
krauss
parents: 23473
diff changeset
    69
      val fnames = map (fst o fst) fixes
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30223
diff changeset
    70
      val qualify = Long_Name.qualify defname
23819
2040846d1bbe some interface cleanup
krauss
parents: 23473
diff changeset
    71
      val addsmps = add_simps fnames post sort_cont
21602
cb13024d0e36 Goal.norm/close_result;
wenzelm
parents: 21511
diff changeset
    72
cb13024d0e36 Goal.norm/close_result;
wenzelm
parents: 21511
diff changeset
    73
      val (((psimps', pinducts'), (_, [termination'])), lthy) =
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21240
diff changeset
    74
          lthy
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30223
diff changeset
    75
            |> addsmps (Long_Name.qualify "partial") "psimps"
29866
6e93ae65c678 Added Nitpick_Const_Psimp attribute, dropped the 's' in Nitpick_Const_Simps, and killed the Nitpick_Ind_Intros attribute.
blanchet
parents: 29863
diff changeset
    76
                       [Attrib.internal (K Nitpick_Const_Psimp_Thms.add)] psimps
6e93ae65c678 Added Nitpick_Const_Psimp attribute, dropped the 's' in Nitpick_Const_Simps, and killed the Nitpick_Ind_Intros attribute.
blanchet
parents: 29863
diff changeset
    77
            ||> fold_option (snd oo addsmps I "simps" [Attrib.internal (K Nitpick_Const_Simp_Thms.add)]) trsimps
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 22102
diff changeset
    78
            ||>> note_theorem ((qualify "pinduct",
25222
78943ac46f6d fun/function: generate case names for induction rules
krauss
parents: 25201
diff changeset
    79
                   [Attrib.internal (K (RuleCases.case_names cnames)),
78943ac46f6d fun/function: generate case names for induction rules
krauss
parents: 25201
diff changeset
    80
                    Attrib.internal (K (RuleCases.consumes 1)),
78943ac46f6d fun/function: generate case names for induction rules
krauss
parents: 25201
diff changeset
    81
                    Attrib.internal (K (Induct.induct_pred ""))]), simple_pinducts)
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 22102
diff changeset
    82
            ||>> note_theorem ((qualify "termination", []), [termination])
25222
78943ac46f6d fun/function: generate case names for induction rules
krauss
parents: 25201
diff changeset
    83
            ||> (snd o note_theorem ((qualify "cases", 
78943ac46f6d fun/function: generate case names for induction rules
krauss
parents: 25201
diff changeset
    84
                   [Attrib.internal (K (RuleCases.case_names cnames))]), [cases]))
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 22102
diff changeset
    85
            ||> fold_option (snd oo curry note_theorem (qualify "domintros", [])) domintros
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21240
diff changeset
    86
25222
78943ac46f6d fun/function: generate case names for induction rules
krauss
parents: 25201
diff changeset
    87
      val cdata = FundefCtxData { add_simps=addsmps, case_names=cnames, psimps=psimps',
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22725
diff changeset
    88
                                  pinducts=snd pinducts', termination=termination', fs=fs, R=R, defname=defname }
26594
1c676ae50311 fundef_afterqed: removed unused config, added do_print flag;
wenzelm
parents: 26580
diff changeset
    89
      val _ =
1c676ae50311 fundef_afterqed: removed unused config, added do_print flag;
wenzelm
parents: 26580
diff changeset
    90
        if not do_print then ()
1c676ae50311 fundef_afterqed: removed unused config, added do_print flag;
wenzelm
parents: 26580
diff changeset
    91
        else Specification.print_consts lthy (K false) (map fst fixes)
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20363
diff changeset
    92
    in
22623
5fcee5b319a2 proper handling of morphisms
krauss
parents: 22498
diff changeset
    93
      lthy 
25201
e6fe58b640ce print the defined constants when finished; tuned
krauss
parents: 25169
diff changeset
    94
        |> LocalTheory.declaration (add_fundef_data o morph_fundef_data cdata)
25222
78943ac46f6d fun/function: generate case names for induction rules
krauss
parents: 25201
diff changeset
    95
    end
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20363
diff changeset
    96
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20363
diff changeset
    97
30486
9cdc7ce0e389 simplified preparation and outer parsing of specification;
wenzelm
parents: 30435
diff changeset
    98
fun gen_add_fundef is_external prep default_constraint fixspec eqns config flags lthy =
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20363
diff changeset
    99
    let
29051
b9c5726e79ab constrain type inference to sort "type"
krauss
parents: 29006
diff changeset
   100
      val constrn_fxs = map (fn (b, T, mx) => (b, SOME (the_default default_constraint T), mx))
30486
9cdc7ce0e389 simplified preparation and outer parsing of specification;
wenzelm
parents: 30435
diff changeset
   101
      val ((fixes0, spec0), ctxt') = prep (constrn_fxs fixspec) eqns lthy
30223
24d975352879 renamed Binding.name_pos to Binding.make, renamed Binding.base_name to Binding.name_of, renamed Binding.map_base to Binding.map_name, added mandatory flag to Binding.qualify;
wenzelm
parents: 29866
diff changeset
   102
      val fixes = map (apfst (apfst Binding.name_of)) fixes0;
30486
9cdc7ce0e389 simplified preparation and outer parsing of specification;
wenzelm
parents: 30435
diff changeset
   103
      val spec = map (fn ((b, atts), prop) => ((Binding.name_of b, atts), [prop])) spec0;
25222
78943ac46f6d fun/function: generate case names for induction rules
krauss
parents: 25201
diff changeset
   104
      val (eqs, post, sort_cont, cnames) = FundefCommon.get_preproc lthy config flags ctxt' fixes spec
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 22102
diff changeset
   105
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 22102
diff changeset
   106
      val defname = mk_defname fixes
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 22102
diff changeset
   107
23819
2040846d1bbe some interface cleanup
krauss
parents: 23473
diff changeset
   108
      val ((goalstate, cont), lthy) =
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   109
          FundefMutual.prepare_fundef_mutual config defname fixes eqs lthy
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20363
diff changeset
   110
26594
1c676ae50311 fundef_afterqed: removed unused config, added do_print flag;
wenzelm
parents: 26580
diff changeset
   111
      val afterqed = fundef_afterqed is_external fixes post defname cont sort_cont cnames
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20363
diff changeset
   112
    in
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22725
diff changeset
   113
      lthy
26594
1c676ae50311 fundef_afterqed: removed unused config, added do_print flag;
wenzelm
parents: 26580
diff changeset
   114
        |> is_external ? LocalTheory.set_group (serial_string ())
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22725
diff changeset
   115
        |> Proof.theorem_i NONE afterqed [[(Logic.unprotect (concl_of goalstate), [])]]
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22725
diff changeset
   116
        |> Proof.refine (Method.primitive_text (fn _ => goalstate)) |> Seq.hd
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20363
diff changeset
   117
    end
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20363
diff changeset
   118
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   119
val add_fundef = gen_add_fundef false Specification.check_spec (TypeInfer.anyT HOLogic.typeS)
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   120
val add_fundef_cmd = gen_add_fundef true Specification.read_spec "_::type"
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   121
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   122
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22725
diff changeset
   123
fun total_termination_afterqed data [[totality]] lthy =
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20363
diff changeset
   124
    let
25222
78943ac46f6d fun/function: generate case names for induction rules
krauss
parents: 25201
diff changeset
   125
      val FundefCtxData { add_simps, case_names, psimps, pinducts, defname, ... } = data
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21240
diff changeset
   126
26628
63306cb94313 replaced Drule.close_derivation/Goal.close_result by Thm.close_derivation (removed obsolete compression);
wenzelm
parents: 26594
diff changeset
   127
      val totality = Thm.close_derivation totality
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   128
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21240
diff changeset
   129
      val remove_domain_condition = full_simplify (HOL_basic_ss addsimps [totality, True_implies_equals])
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   130
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 22102
diff changeset
   131
      val tsimps = map remove_domain_condition psimps
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 22102
diff changeset
   132
      val tinduct = map remove_domain_condition pinducts
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents: 19617
diff changeset
   133
21511
16c62deb1adf The function package declares the [code] attribute automatically again.
krauss
parents: 21491
diff changeset
   134
      val has_guards = exists ((fn (Const ("Trueprop", _) $ _) => false | _ => true) o prop_of) tsimps
29866
6e93ae65c678 Added Nitpick_Const_Psimp attribute, dropped the 's' in Nitpick_Const_Simps, and killed the Nitpick_Ind_Intros attribute.
blanchet
parents: 29863
diff changeset
   135
      val allatts = (not has_guards ? cons Code.add_default_eqn_attrib)
6e93ae65c678 Added Nitpick_Const_Psimp attribute, dropped the 's' in Nitpick_Const_Simps, and killed the Nitpick_Ind_Intros attribute.
blanchet
parents: 29863
diff changeset
   136
        [Attrib.internal (K Nitpick_Const_Simp_Thms.add)]
21602
cb13024d0e36 Goal.norm/close_result;
wenzelm
parents: 21511
diff changeset
   137
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30223
diff changeset
   138
      val qualify = Long_Name.qualify defname;
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   139
    in
21511
16c62deb1adf The function package declares the [code] attribute automatically again.
krauss
parents: 21491
diff changeset
   140
      lthy
26529
03ad378ed5f0 Function package no longer overwrites theorems.
krauss
parents: 26171
diff changeset
   141
        |> add_simps I "simps" allatts tsimps |> snd
25222
78943ac46f6d fun/function: generate case names for induction rules
krauss
parents: 25201
diff changeset
   142
        |> note_theorem ((qualify "induct", [Attrib.internal (K (RuleCases.case_names case_names))]), tinduct) |> snd
20363
f34c5dbe74d5 global goals/qeds: after_qed operates on Proof.context (potentially local_theory);
wenzelm
parents: 20338
diff changeset
   143
    end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   144
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   145
fun gen_termination_proof prep_term raw_term_opt lthy =
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20363
diff changeset
   146
    let
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   147
      val term_opt = Option.map (prep_term lthy) raw_term_opt
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22725
diff changeset
   148
      val data = the (case term_opt of
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   149
                        SOME t => (import_fundef_data t lthy
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   150
                          handle Option.Option =>
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   151
                            error ("Not a function: " ^ quote (Syntax.string_of_term lthy t)))
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   152
                      | NONE => (import_last_fundef lthy handle Option.Option => error "Not a function"))
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20363
diff changeset
   153
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 22102
diff changeset
   154
        val FundefCtxData {termination, R, ...} = data
22325
be61bd159a99 changed termination goal to use object quantifier
krauss
parents: 22170
diff changeset
   155
        val domT = domain_type (fastype_of R)
be61bd159a99 changed termination goal to use object quantifier
krauss
parents: 22170
diff changeset
   156
        val goal = HOLogic.mk_Trueprop (HOLogic.mk_all ("x", domT, mk_acc domT R $ Free ("x", domT)))
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20363
diff changeset
   157
    in
20877
368b997ad67e removed with_local_path -- LocalTheory.note admits qualified names;
wenzelm
parents: 20654
diff changeset
   158
      lthy
30761
ac7570d80c3d renamed ProofContext.note_thmss_i to ProofContext.note_thmss, eliminated obsolete external version;
wenzelm
parents: 30528
diff changeset
   159
        |> ProofContext.note_thmss "" [((Binding.empty, [ContextRules.rule_del]), [([allI], [])])] |> snd
ac7570d80c3d renamed ProofContext.note_thmss_i to ProofContext.note_thmss, eliminated obsolete external version;
wenzelm
parents: 30528
diff changeset
   160
        |> ProofContext.note_thmss "" [((Binding.empty, [ContextRules.intro_bang (SOME 1)]), [([allI], [])])] |> snd
ac7570d80c3d renamed ProofContext.note_thmss_i to ProofContext.note_thmss, eliminated obsolete external version;
wenzelm
parents: 30528
diff changeset
   161
        |> ProofContext.note_thmss ""
28965
1de908189869 cleaned up binding module and related code
haftmann
parents: 28703
diff changeset
   162
          [((Binding.name "termination", [ContextRules.intro_bang (SOME 0)]),
21602
cb13024d0e36 Goal.norm/close_result;
wenzelm
parents: 21511
diff changeset
   163
            [([Goal.norm_result termination], [])])] |> snd
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22725
diff changeset
   164
        |> Proof.theorem_i NONE (total_termination_afterqed data) [[(goal, [])]]
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20363
diff changeset
   165
    end
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20363
diff changeset
   166
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   167
val termination_proof = gen_termination_proof Syntax.check_term;
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   168
val termination_proof_cmd = gen_termination_proof Syntax.read_term;
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   169
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   170
fun termination term_opt lthy =
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   171
  lthy
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   172
  |> LocalTheory.set_group (serial_string ())
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   173
  |> termination_proof term_opt;
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   174
26171
5426a823455c more precise handling of "group" for termination;
wenzelm
parents: 26129
diff changeset
   175
fun termination_cmd term_opt lthy =
5426a823455c more precise handling of "group" for termination;
wenzelm
parents: 26129
diff changeset
   176
  lthy
5426a823455c more precise handling of "group" for termination;
wenzelm
parents: 26129
diff changeset
   177
  |> LocalTheory.set_group (serial_string ())
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   178
  |> termination_proof_cmd term_opt;
19611
da2a0014c461 Function Package: Quick-and-dirty-fixed strange "Proved a different theorem bug"
krauss
parents: 19585
diff changeset
   179
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   180
21235
674e2731b519 Added datatype hook to declare all case_congs as "fundef_cong" automatically.
krauss
parents: 21211
diff changeset
   181
(* Datatype hook to declare datatype congs as "fundef_congs" *)
674e2731b519 Added datatype hook to declare all case_congs as "fundef_cong" automatically.
krauss
parents: 21211
diff changeset
   182
674e2731b519 Added datatype hook to declare all case_congs as "fundef_cong" automatically.
krauss
parents: 21211
diff changeset
   183
21602
cb13024d0e36 Goal.norm/close_result;
wenzelm
parents: 21511
diff changeset
   184
fun add_case_cong n thy =
24168
86a03a092062 simplified internal interfaces; cong rules are now handled directly by "context_tree.ML"
krauss
parents: 24039
diff changeset
   185
    Context.theory_map (FundefCtxTree.map_fundef_congs (Thm.add_thm
21235
674e2731b519 Added datatype hook to declare all case_congs as "fundef_cong" automatically.
krauss
parents: 21211
diff changeset
   186
                          (DatatypePackage.get_datatype thy n |> the
674e2731b519 Added datatype hook to declare all case_congs as "fundef_cong" automatically.
krauss
parents: 21211
diff changeset
   187
                           |> #case_cong
21602
cb13024d0e36 Goal.norm/close_result;
wenzelm
parents: 21511
diff changeset
   188
                           |> safe_mk_meta_eq)))
21235
674e2731b519 Added datatype hook to declare all case_congs as "fundef_cong" automatically.
krauss
parents: 21211
diff changeset
   189
                       thy
674e2731b519 Added datatype hook to declare all case_congs as "fundef_cong" automatically.
krauss
parents: 21211
diff changeset
   190
24626
85eceef2edc7 introduced generic concepts for theory interpretators
haftmann
parents: 24624
diff changeset
   191
val case_cong = fold add_case_cong
21235
674e2731b519 Added datatype hook to declare all case_congs as "fundef_cong" automatically.
krauss
parents: 21211
diff changeset
   192
24711
e8bba7723858 simplified interpretation setup;
wenzelm
parents: 24626
diff changeset
   193
val setup_case_cong = DatatypePackage.interpretation case_cong
24626
85eceef2edc7 introduced generic concepts for theory interpretators
haftmann
parents: 24624
diff changeset
   194
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   195
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   196
(* setup *)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   197
21602
cb13024d0e36 Goal.norm/close_result;
wenzelm
parents: 21511
diff changeset
   198
val setup =
30528
7173bf123335 simplified attribute setup;
wenzelm
parents: 30493
diff changeset
   199
  Attrib.setup @{binding fundef_cong} (Attrib.add_del FundefCtxTree.cong_add FundefCtxTree.cong_del)
7173bf123335 simplified attribute setup;
wenzelm
parents: 30493
diff changeset
   200
      "declaration of congruence rule for function definitions"
24626
85eceef2edc7 introduced generic concepts for theory interpretators
haftmann
parents: 24624
diff changeset
   201
  #> setup_case_cong
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22733
diff changeset
   202
  #> FundefRelation.setup
26749
397a1aeede7d * New attribute "termination_simp": Simp rules for termination proofs
krauss
parents: 26628
diff changeset
   203
  #> FundefCommon.TerminationSimps.setup
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   204
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   205
val get_congs = FundefCtxTree.get_fundef_congs
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents: 19617
diff changeset
   206
21235
674e2731b519 Added datatype hook to declare all case_congs as "fundef_cong" automatically.
krauss
parents: 21211
diff changeset
   207
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   208
(* outer syntax *)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   209
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   210
local structure P = OuterParse and K = OuterKeyword in
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   211
27353
71c4dd53d4cb moved global keywords from OuterSyntax to OuterKeyword, tuned interfaces;
wenzelm
parents: 27187
diff changeset
   212
val _ = OuterKeyword.keyword "otherwise";
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24861
diff changeset
   213
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24861
diff changeset
   214
val _ =
26989
9b2acb536228 uniform treatment of target, not as config;
wenzelm
parents: 26749
diff changeset
   215
  OuterSyntax.local_theory_to_proof "function" "define general recursive functions" K.thy_goal
22498
62cdd4b3e96b made function syntax strict, requiring | to separate equations; cleanup
krauss
parents: 22325
diff changeset
   216
  (fundef_parser default_config
30493
b8570ea1ce25 provide regular ML interfaces for Isar source language elements;
wenzelm
parents: 30486
diff changeset
   217
     >> (fn ((config, fixes), (flags, statements)) => add_fundef_cmd fixes statements config flags));
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20363
diff changeset
   218
24867
e5b55d7be9bb simplified interfaces for outer syntax;
wenzelm
parents: 24861
diff changeset
   219
val _ =
26989
9b2acb536228 uniform treatment of target, not as config;
wenzelm
parents: 26749
diff changeset
   220
  OuterSyntax.local_theory_to_proof "termination" "prove termination of a recursive function" K.thy_goal
9b2acb536228 uniform treatment of target, not as config;
wenzelm
parents: 26749
diff changeset
   221
  (Scan.option P.term >> termination_cmd);
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   222
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   223
end;
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   224
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   225
19585
70a1ce3b23ae removed 'concl is' patterns;
wenzelm
parents: 19583
diff changeset
   226
end