src/HOL/Tools/Function/function.ML
author wenzelm
Sat, 16 Aug 2014 19:20:11 +0200
changeset 57959 1bfed12a7646
parent 56932 11a4001b06c6
child 58112 8081087096ad
permissions -rw-r--r--
updated to named_theorems; modernized module name and setup;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37744
3daaf23b9ab4 tuned titles
haftmann
parents: 37145
diff changeset
     1
(*  Title:      HOL/Tools/Function/function.ML
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
     2
    Author:     Alexander Krauss, TU Muenchen
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
     3
41114
f9ae7c2abf7e tuned headers
krauss
parents: 40076
diff changeset
     4
Main entry points to the function package.
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
     5
*)
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
     6
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
     7
signature FUNCTION =
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
     8
sig
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
     9
  include FUNCTION_DATA
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    10
36520
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
    11
  val add_function: (binding * typ option * mixfix) list ->
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
    12
    (Attrib.binding * term) list -> Function_Common.function_config ->
36522
e80a95279ef6 return info record (relative to auxiliary context!)
krauss
parents: 36520
diff changeset
    13
    (Proof.context -> tactic) -> local_theory -> info * local_theory
36520
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
    14
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
    15
  val add_function_cmd: (binding * string option * mixfix) list ->
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
    16
    (Attrib.binding * string) list -> Function_Common.function_config ->
44239
47ecd30e018d less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
wenzelm
parents: 44192
diff changeset
    17
    (Proof.context -> tactic) -> bool -> local_theory -> info * local_theory
36520
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
    18
36519
46bf776a81e0 ML interface uses plain command names, following conventions from typedef
krauss
parents: 36518
diff changeset
    19
  val function: (binding * typ option * mixfix) list ->
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    20
    (Attrib.binding * term) list -> Function_Common.function_config ->
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    21
    local_theory -> Proof.state
34230
b0d21ae2528e more official data record Function.info
krauss
parents: 33726
diff changeset
    22
36519
46bf776a81e0 ML interface uses plain command names, following conventions from typedef
krauss
parents: 36518
diff changeset
    23
  val function_cmd: (binding * string option * mixfix) list ->
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    24
    (Attrib.binding * string) list -> Function_Common.function_config ->
44239
47ecd30e018d less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
wenzelm
parents: 44192
diff changeset
    25
    bool -> local_theory -> Proof.state
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    26
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
    27
  val prove_termination: term option -> tactic -> local_theory ->
36547
2a9d0ec8c10d return updated info record after termination proof
krauss
parents: 36522
diff changeset
    28
    info * local_theory
2a9d0ec8c10d return updated info record after termination proof
krauss
parents: 36522
diff changeset
    29
  val prove_termination_cmd: string option -> tactic -> local_theory ->
2a9d0ec8c10d return updated info record after termination proof
krauss
parents: 36522
diff changeset
    30
    info * local_theory
36520
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
    31
36519
46bf776a81e0 ML interface uses plain command names, following conventions from typedef
krauss
parents: 36518
diff changeset
    32
  val termination : term option -> local_theory -> Proof.state
46bf776a81e0 ML interface uses plain command names, following conventions from typedef
krauss
parents: 36518
diff changeset
    33
  val termination_cmd : string option -> local_theory -> Proof.state
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    34
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    35
  val setup : theory -> theory
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    36
  val get_congs : Proof.context -> thm list
34230
b0d21ae2528e more official data record Function.info
krauss
parents: 33726
diff changeset
    37
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    38
  val get_info : Proof.context -> term -> info
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    39
end
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    40
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    41
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    42
structure Function : FUNCTION =
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    43
struct
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    44
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    45
open Function_Lib
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    46
open Function_Common
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    47
45592
8baa0b7f3f66 added ML antiquotation @{attributes};
wenzelm
parents: 45291
diff changeset
    48
val simp_attribs =
8baa0b7f3f66 added ML antiquotation @{attributes};
wenzelm
parents: 45291
diff changeset
    49
  @{attributes [simp, nitpick_simp]} @ [Attrib.internal (K Code.add_default_eqn_attribute)]
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    50
45592
8baa0b7f3f66 added ML antiquotation @{attributes};
wenzelm
parents: 45291
diff changeset
    51
val psimp_attribs =
8baa0b7f3f66 added ML antiquotation @{attributes};
wenzelm
parents: 45291
diff changeset
    52
  @{attributes [nitpick_psimp]}
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    53
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    54
fun mk_defname fixes = fixes |> map (fst o fst) |> space_implode "_"
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    55
53604
c1db98d7c66f clarified
krauss
parents: 53603
diff changeset
    56
fun note_qualified suffix attrs (fname, thms) =
c1db98d7c66f clarified
krauss
parents: 53603
diff changeset
    57
  Local_Theory.note ((Binding.qualify true fname (Binding.name suffix),
c1db98d7c66f clarified
krauss
parents: 53603
diff changeset
    58
    map (Attrib.internal o K) attrs), thms)
c1db98d7c66f clarified
krauss
parents: 53603
diff changeset
    59
  #> apfst snd
c1db98d7c66f clarified
krauss
parents: 53603
diff changeset
    60
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    61
fun add_simps fnames post sort extra_qualify label mod_binding moreatts
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    62
  simps lthy =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    63
  let
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    64
    val spec = post simps
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    65
      |> map (apfst (apsnd (fn ats => moreatts @ ats)))
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    66
      |> map (apfst (apfst extra_qualify))
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    67
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    68
    val (saved_spec_simps, lthy) =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    69
      fold_map Local_Theory.note spec lthy
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    70
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    71
    val saved_simps = maps snd saved_spec_simps
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    72
    val simps_by_f = sort saved_simps
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    73
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    74
    fun add_for_f fname simps =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    75
      Local_Theory.note
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    76
        ((mod_binding (Binding.qualify true fname (Binding.name label)), []), simps)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    77
      #> snd
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    78
  in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    79
    (saved_simps, fold2 add_for_f fnames simps_by_f lthy)
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    80
  end
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    81
44239
47ecd30e018d less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
wenzelm
parents: 44192
diff changeset
    82
fun prepare_function do_print prep default_constraint fixspec eqns config lthy =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    83
  let
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    84
    val constrn_fxs = map (fn (b, T, mx) => (b, SOME (the_default default_constraint T), mx))
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    85
    val ((fixes0, spec0), ctxt') = prep (constrn_fxs fixspec) eqns lthy
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    86
    val fixes = map (apfst (apfst Binding.name_of)) fixes0;
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    87
    val spec = map (fn (bnd, prop) => (bnd, [prop])) spec0;
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    88
    val (eqs, post, sort_cont, cnames) = get_preproc lthy config ctxt' fixes spec
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    89
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    90
    val defname = mk_defname fixes
41846
b368a7aee46a removed support for tail-recursion from function package (now implemented by partial_function)
krauss
parents: 41417
diff changeset
    91
    val FunctionConfig {partials, default, ...} = config
41417
211dbd42f95d function (default) is legacy feature
krauss
parents: 41405
diff changeset
    92
    val _ =
44052
00f0c8782a51 slightly more uniform messages;
wenzelm
parents: 43277
diff changeset
    93
      if is_some default
00f0c8782a51 slightly more uniform messages;
wenzelm
parents: 43277
diff changeset
    94
      then legacy_feature "\"function (default)\" -- use 'partial_function' instead"
41417
211dbd42f95d function (default) is legacy feature
krauss
parents: 41405
diff changeset
    95
      else ()
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    96
36519
46bf776a81e0 ML interface uses plain command names, following conventions from typedef
krauss
parents: 36518
diff changeset
    97
    val ((goal_state, cont), lthy') =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
    98
      Function_Mutual.prepare_function_mutual config defname fixes eqs lthy
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
    99
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   100
    fun afterqed [[proof]] lthy =
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   101
      let
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   102
        val result = cont (Thm.close_derivation proof)
52384
80c00a851de5 export dom predicate in the info record
krauss
parents: 52383
diff changeset
   103
        val FunctionResult {fs, R, dom, psimps, simple_pinducts,
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   104
                termination, domintros, cases, ...} = result
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   105
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   106
        val pelims = Function_Elims.mk_partial_elim_rules lthy result
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   107
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   108
        val fnames = map (fst o fst) fixes
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   109
        fun qualify n = Binding.name n
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   110
          |> Binding.qualify true defname
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   111
        val conceal_partial = if partials then I else Binding.conceal
33394
9c6980f2eb39 conceal "termination" rule, used only by special tools
krauss
parents: 33369
diff changeset
   112
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   113
        val addsmps = add_simps fnames post sort_cont
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   114
53604
c1db98d7c66f clarified
krauss
parents: 53603
diff changeset
   115
        val (((((psimps', [pinducts']), [termination']), cases'), pelims'), lthy) =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   116
          lthy
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   117
          |> addsmps (conceal_partial o Binding.qualify false "partial")
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   118
               "psimps" conceal_partial psimp_attribs psimps
50771
2852f997bfb5 prefer negative "consumes", relative to the total number of prems, which is stable under more morphisms, notably those from nested context with assumes (cf. existing treatment of 'obtains');
wenzelm
parents: 49967
diff changeset
   119
          ||>> Local_Theory.notes [((conceal_partial (qualify "pinduct"), []),
2852f997bfb5 prefer negative "consumes", relative to the total number of prems, which is stable under more morphisms, notably those from nested context with assumes (cf. existing treatment of 'obtains');
wenzelm
parents: 49967
diff changeset
   120
                simple_pinducts |> map (fn th => ([th],
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   121
                 [Attrib.internal (K (Rule_Cases.case_names cnames)),
50771
2852f997bfb5 prefer negative "consumes", relative to the total number of prems, which is stable under more morphisms, notably those from nested context with assumes (cf. existing treatment of 'obtains');
wenzelm
parents: 49967
diff changeset
   122
                  Attrib.internal (K (Rule_Cases.consumes (1 - Thm.nprems_of th))),
2852f997bfb5 prefer negative "consumes", relative to the total number of prems, which is stable under more morphisms, notably those from nested context with assumes (cf. existing treatment of 'obtains');
wenzelm
parents: 49967
diff changeset
   123
                  Attrib.internal (K (Induct.induct_pred ""))])))]
52383
71df93ff010d export cases rule in the info record
krauss
parents: 51717
diff changeset
   124
          ||>> (apfst snd o Local_Theory.note ((Binding.conceal (qualify "termination"), []), [termination]))
53604
c1db98d7c66f clarified
krauss
parents: 53603
diff changeset
   125
          ||>> fold_map (note_qualified "cases" [Rule_Cases.case_names cnames]) (fnames ~~ map single cases) (* TODO: case names *)
c1db98d7c66f clarified
krauss
parents: 53603
diff changeset
   126
          ||>> fold_map (note_qualified "pelims" [Rule_Cases.consumes 1, Rule_Cases.constraints 1]) (fnames ~~ pelims)
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   127
          ||> (case domintros of NONE => I | SOME thms =>
40076
6f012a209dac some cleanup in Function_Lib
krauss
parents: 39754
diff changeset
   128
                   Local_Theory.note ((qualify "domintros", []), thms) #> snd)
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   129
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   130
        val info = { add_simps=addsmps, fnames=fnames, case_names=cnames, psimps=psimps',
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   131
          pinducts=snd pinducts', simps=NONE, inducts=NONE, termination=termination',
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   132
          fs=fs, R=R, dom=dom, defname=defname, is_partial=true, cases=flat cases',
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   133
          pelims=pelims',elims=NONE}
34230
b0d21ae2528e more official data record Function.info
krauss
parents: 33726
diff changeset
   134
56932
11a4001b06c6 more position markup to help locating the query context, e.g. from "Info" dockable;
wenzelm
parents: 56254
diff changeset
   135
        val _ =
11a4001b06c6 more position markup to help locating the query context, e.g. from "Info" dockable;
wenzelm
parents: 56254
diff changeset
   136
          Proof_Display.print_consts do_print (Position.thread_data ()) lthy
11a4001b06c6 more position markup to help locating the query context, e.g. from "Info" dockable;
wenzelm
parents: 56254
diff changeset
   137
            (K false) (map fst fixes)
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   138
      in
44239
47ecd30e018d less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
wenzelm
parents: 44192
diff changeset
   139
        (info,
45291
57cd50f98fdc uniform Local_Theory.declaration with explicit params;
wenzelm
parents: 45290
diff changeset
   140
         lthy |> Local_Theory.declaration {syntax = false, pervasive = false}
57cd50f98fdc uniform Local_Theory.declaration with explicit params;
wenzelm
parents: 45290
diff changeset
   141
          (add_function_data o transform_function_data info))
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   142
      end
36520
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   143
  in
36519
46bf776a81e0 ML interface uses plain command names, following conventions from typedef
krauss
parents: 36518
diff changeset
   144
    ((goal_state, afterqed), lthy')
36518
a33b986f2e22 function: better separate Isar integration from actual functionality
krauss
parents: 36323
diff changeset
   145
  end
a33b986f2e22 function: better separate Isar integration from actual functionality
krauss
parents: 36323
diff changeset
   146
44239
47ecd30e018d less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
wenzelm
parents: 44192
diff changeset
   147
fun gen_add_function do_print prep default_constraint fixspec eqns config tac lthy =
36520
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   148
  let
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   149
    val ((goal_state, afterqed), lthy') =
44239
47ecd30e018d less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
wenzelm
parents: 44192
diff changeset
   150
      prepare_function do_print prep default_constraint fixspec eqns config lthy
36520
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   151
    val pattern_thm =
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   152
      case SINGLE (tac lthy') goal_state of
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   153
        NONE => error "pattern completeness and compatibility proof failed"
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   154
      | SOME st => Goal.finish lthy' st
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   155
  in
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   156
    lthy'
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   157
    |> afterqed [[pattern_thm]]
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   158
  end
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   159
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   160
val add_function =
56254
a2dd9200854d more antiquotations;
wenzelm
parents: 55404
diff changeset
   161
  gen_add_function false Specification.check_spec (Type_Infer.anyT @{sort type})
44239
47ecd30e018d less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
wenzelm
parents: 44192
diff changeset
   162
fun add_function_cmd a b c d int = gen_add_function int Specification.read_spec "_::type" a b c d
36520
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   163
44239
47ecd30e018d less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
wenzelm
parents: 44192
diff changeset
   164
fun gen_function do_print prep default_constraint fixspec eqns config lthy =
36518
a33b986f2e22 function: better separate Isar integration from actual functionality
krauss
parents: 36323
diff changeset
   165
  let
36520
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   166
    val ((goal_state, afterqed), lthy') =
44239
47ecd30e018d less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
wenzelm
parents: 44192
diff changeset
   167
      prepare_function do_print prep default_constraint fixspec eqns config lthy
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   168
  in
36518
a33b986f2e22 function: better separate Isar integration from actual functionality
krauss
parents: 36323
diff changeset
   169
    lthy'
36522
e80a95279ef6 return info record (relative to auxiliary context!)
krauss
parents: 36520
diff changeset
   170
    |> Proof.theorem NONE (snd oo afterqed) [[(Logic.unprotect (concl_of goal_state), [])]]
54883
dd04a8b654fc proper context for norm_hhf and derived operations;
wenzelm
parents: 53614
diff changeset
   171
    |> Proof.refine (Method.primitive_text (K (K goal_state))) |> Seq.hd
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   172
  end
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   173
36519
46bf776a81e0 ML interface uses plain command names, following conventions from typedef
krauss
parents: 36518
diff changeset
   174
val function =
56254
a2dd9200854d more antiquotations;
wenzelm
parents: 55404
diff changeset
   175
  gen_function false Specification.check_spec (Type_Infer.anyT @{sort type})
44239
47ecd30e018d less verbosity for 'function' and 'fun': observe "int" flag more carefully (cf. a32ca9165928);
wenzelm
parents: 44192
diff changeset
   176
fun function_cmd a b c int = gen_function int Specification.read_spec "_::type" a b c
36520
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   177
36518
a33b986f2e22 function: better separate Isar integration from actual functionality
krauss
parents: 36323
diff changeset
   178
fun prepare_termination_proof prep_term raw_term_opt lthy =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   179
  let
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   180
    val term_opt = Option.map (prep_term lthy) raw_term_opt
48995
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   181
    val info =
49967
69774b4f5b8a recovered explicit error message, which was lost in b8570ea1ce25;
wenzelm
parents: 48995
diff changeset
   182
      (case term_opt of
69774b4f5b8a recovered explicit error message, which was lost in b8570ea1ce25;
wenzelm
parents: 48995
diff changeset
   183
        SOME t =>
69774b4f5b8a recovered explicit error message, which was lost in b8570ea1ce25;
wenzelm
parents: 48995
diff changeset
   184
          (case import_function_data t lthy of
69774b4f5b8a recovered explicit error message, which was lost in b8570ea1ce25;
wenzelm
parents: 48995
diff changeset
   185
            SOME info => info
69774b4f5b8a recovered explicit error message, which was lost in b8570ea1ce25;
wenzelm
parents: 48995
diff changeset
   186
          | NONE => error ("Not a function: " ^ quote (Syntax.string_of_term lthy t)))
69774b4f5b8a recovered explicit error message, which was lost in b8570ea1ce25;
wenzelm
parents: 48995
diff changeset
   187
      | NONE =>
69774b4f5b8a recovered explicit error message, which was lost in b8570ea1ce25;
wenzelm
parents: 48995
diff changeset
   188
          (case import_last_function lthy of
69774b4f5b8a recovered explicit error message, which was lost in b8570ea1ce25;
wenzelm
parents: 48995
diff changeset
   189
            SOME info => info
69774b4f5b8a recovered explicit error message, which was lost in b8570ea1ce25;
wenzelm
parents: 48995
diff changeset
   190
          | NONE => error "Not a function"))
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   191
48995
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   192
    val { termination, fs, R, add_simps, case_names, psimps,
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   193
      pinducts, defname, fnames, cases, dom, pelims, ...} = info
48995
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   194
    val domT = domain_type (fastype_of R)
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   195
    val goal = HOLogic.mk_Trueprop (HOLogic.mk_all ("x", domT, mk_acc domT R $ Free ("x", domT)))
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   196
    fun afterqed [[totality]] lthy =
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   197
      let
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   198
        val totality = Thm.close_derivation totality
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   199
        val remove_domain_condition =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50771
diff changeset
   200
          full_simplify (put_simpset HOL_basic_ss lthy
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 50771
diff changeset
   201
            addsimps [totality, @{thm True_implies_equals}])
48995
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   202
        val tsimps = map remove_domain_condition psimps
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   203
        val tinduct = map remove_domain_condition pinducts
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   204
        val telims = map (map remove_domain_condition) pelims
34230
b0d21ae2528e more official data record Function.info
krauss
parents: 33726
diff changeset
   205
48995
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   206
        fun qualify n = Binding.name n
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   207
          |> Binding.qualify true defname
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   208
48995
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   209
      in
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   210
        lthy
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   211
        |> add_simps I "simps" I simp_attribs tsimps
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   212
        ||>> Local_Theory.note
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   213
           ((qualify "induct",
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   214
             [Attrib.internal (K (Rule_Cases.case_names case_names))]),
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   215
            tinduct)
53614
8c51fc24d83c omit automatic Induct.cases_pred declaration, which breaks many existing proofs
krauss
parents: 53604
diff changeset
   216
        ||>> fold_map (note_qualified "elims" [Rule_Cases.consumes 1, Rule_Cases.constraints 1]) (fnames ~~ telims)
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   217
        |-> (fn ((simps,(_,inducts)), elims) => fn lthy =>
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   218
          let val info' = { is_partial=false, defname=defname, fnames=fnames, add_simps=add_simps,
52384
80c00a851de5 export dom predicate in the info record
krauss
parents: 52383
diff changeset
   219
            case_names=case_names, fs=fs, R=R, dom=dom, psimps=psimps, pinducts=pinducts,
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   220
            simps=SOME simps, inducts=SOME inducts, termination=termination, cases=cases, pelims=pelims, elims=SOME elims}
48995
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   221
          in
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   222
            (info',
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   223
             lthy
48995
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   224
             |> Local_Theory.declaration {syntax = false, pervasive = false}
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   225
               (add_function_data o transform_function_data info')
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   226
             |> Spec_Rules.add Spec_Rules.Equational (fs, tsimps))
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   227
          end)
0e1cab4a334e more precise indentation;
wenzelm
parents: 47701
diff changeset
   228
      end
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   229
  in
36518
a33b986f2e22 function: better separate Isar integration from actual functionality
krauss
parents: 36323
diff changeset
   230
    (goal, afterqed, termination)
a33b986f2e22 function: better separate Isar integration from actual functionality
krauss
parents: 36323
diff changeset
   231
  end
a33b986f2e22 function: better separate Isar integration from actual functionality
krauss
parents: 36323
diff changeset
   232
36520
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   233
fun gen_prove_termination prep_term raw_term_opt tac lthy =
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   234
  let
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   235
    val (goal, afterqed, termination) =
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   236
      prepare_termination_proof prep_term raw_term_opt lthy
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   237
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   238
    val totality = Goal.prove lthy [] [] goal (K tac)
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   239
  in
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   240
    afterqed [[totality]] lthy
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   241
end
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   242
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   243
val prove_termination = gen_prove_termination Syntax.check_term
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   244
val prove_termination_cmd = gen_prove_termination Syntax.read_term
772ed73e1d61 function: sane interface for programmatic use
krauss
parents: 36519
diff changeset
   245
36519
46bf776a81e0 ML interface uses plain command names, following conventions from typedef
krauss
parents: 36518
diff changeset
   246
fun gen_termination prep_term raw_term_opt lthy =
36518
a33b986f2e22 function: better separate Isar integration from actual functionality
krauss
parents: 36323
diff changeset
   247
  let
a33b986f2e22 function: better separate Isar integration from actual functionality
krauss
parents: 36323
diff changeset
   248
    val (goal, afterqed, termination) = prepare_termination_proof prep_term raw_term_opt lthy
a33b986f2e22 function: better separate Isar integration from actual functionality
krauss
parents: 36323
diff changeset
   249
  in
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   250
    lthy
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 41846
diff changeset
   251
    |> Proof_Context.note_thmss ""
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   252
       [((Binding.empty, [Context_Rules.rule_del]), [([allI], [])])] |> snd
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 41846
diff changeset
   253
    |> Proof_Context.note_thmss ""
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   254
       [((Binding.empty, [Context_Rules.intro_bang (SOME 1)]), [([allI], [])])] |> snd
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 41846
diff changeset
   255
    |> Proof_Context.note_thmss ""
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   256
       [((Binding.name "termination", [Context_Rules.intro_bang (SOME 0)]),
54883
dd04a8b654fc proper context for norm_hhf and derived operations;
wenzelm
parents: 53614
diff changeset
   257
         [([Goal.norm_result lthy termination], [])])] |> snd
36547
2a9d0ec8c10d return updated info record after termination proof
krauss
parents: 36522
diff changeset
   258
    |> Proof.theorem NONE (snd oo afterqed) [[(goal, [])]]
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   259
  end
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   260
36519
46bf776a81e0 ML interface uses plain command names, following conventions from typedef
krauss
parents: 36518
diff changeset
   261
val termination = gen_termination Syntax.check_term
46bf776a81e0 ML interface uses plain command names, following conventions from typedef
krauss
parents: 36518
diff changeset
   262
val termination_cmd = gen_termination Syntax.read_term
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   263
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   264
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   265
(* Datatype hook to declare datatype congs as "function_congs" *)
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   266
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   267
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   268
fun add_case_cong n thy =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   269
  let
55404
5cb95b79a51f transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents: 55085
diff changeset
   270
    val cong = #case_cong (Datatype_Data.the_info thy n)
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   271
      |> safe_mk_meta_eq
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   272
  in
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   273
    Context.theory_map
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   274
      (Function_Ctx_Tree.map_function_congs (Thm.add_thm cong)) thy
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   275
  end
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   276
55404
5cb95b79a51f transformed 'option' and 'list' into new-style datatypes (but register them as old-style as well)
blanchet
parents: 55085
diff changeset
   277
val setup_case_cong = Datatype_Data.interpretation (K (fold add_case_cong))
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   278
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   279
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   280
(* setup *)
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   281
57959
1bfed12a7646 updated to named_theorems;
wenzelm
parents: 56932
diff changeset
   282
val setup = setup_case_cong
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   283
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   284
val get_congs = Function_Ctx_Tree.get_function_congs
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   285
34230
b0d21ae2528e more official data record Function.info
krauss
parents: 33726
diff changeset
   286
fun get_info ctxt t = Item_Net.retrieve (get_function ctxt) t
b0d21ae2528e more official data record Function.info
krauss
parents: 33726
diff changeset
   287
  |> the_single |> snd
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   288
36960
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36547
diff changeset
   289
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   290
(* outer syntax *)
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   291
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   292
val _ =
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 45592
diff changeset
   293
  Outer_Syntax.local_theory_to_proof' @{command_spec "function"}
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 45592
diff changeset
   294
    "define general recursive functions"
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 45592
diff changeset
   295
    (function_parser default_config
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 45592
diff changeset
   296
      >> (fn ((config, fixes), statements) => function_cmd fixes statements config))
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   297
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   298
val _ =
46961
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 45592
diff changeset
   299
  Outer_Syntax.local_theory_to_proof @{command_spec "termination"}
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 45592
diff changeset
   300
    "prove termination of a recursive function"
5c6955f487e5 outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents: 45592
diff changeset
   301
    (Scan.option Parse.term >> termination_cmd)
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   302
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   303
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   304
end