src/HOL/Tools/function_package/fundef_common.ML
author wenzelm
Sun, 03 Jun 2007 23:16:43 +0200
changeset 23215 20b5558a5419
parent 23206 209e32e7c91e
child 23766 77e796fe89eb
permissions -rw-r--r--
local open FundefLib;
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_common.ML
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     2
    ID:         $Id$
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     3
    Author:     Alexander Krauss, TU Muenchen
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     4
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     5
A package for general recursive function definitions. 
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
     6
Common definitions and other infrastructure.
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     7
*)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     8
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
     9
structure FundefCommon =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    10
struct
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    11
23215
20b5558a5419 local open FundefLib;
wenzelm
parents: 23206
diff changeset
    12
local open FundefLib in
20b5558a5419 local open FundefLib;
wenzelm
parents: 23206
diff changeset
    13
22498
62cdd4b3e96b made function syntax strict, requiring | to separate equations; cleanup
krauss
parents: 22279
diff changeset
    14
(* Profiling *)
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    15
val profile = ref false;
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    16
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    17
fun PROFILE msg = if !profile then timeap_msg msg else I
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    18
22498
62cdd4b3e96b made function syntax strict, requiring | to separate equations; cleanup
krauss
parents: 22279
diff changeset
    19
62cdd4b3e96b made function syntax strict, requiring | to separate equations; cleanup
krauss
parents: 22279
diff changeset
    20
val acc_const_name = "Accessible_Part.acc"
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
    21
fun mk_acc domT R =
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
    22
    Const (acc_const_name, (domT --> domT --> HOLogic.boolT) --> domT --> HOLogic.boolT) $ R 
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    23
21319
cf814e36f788 replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents: 21255
diff changeset
    24
val function_name = suffix "C"
cf814e36f788 replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents: 21255
diff changeset
    25
val graph_name = suffix "_graph"
cf814e36f788 replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents: 21255
diff changeset
    26
val rel_name = suffix "_rel"
cf814e36f788 replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents: 21255
diff changeset
    27
val dom_name = suffix "_dom"
cf814e36f788 replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents: 21255
diff changeset
    28
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    29
type depgraph = int IntGraph.T
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    30
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    31
datatype ctx_tree 
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    32
  = Leaf of term
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    33
  | Cong of (term * thm * depgraph * ((string * typ) list * thm list * ctx_tree) list)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    34
  | RCall of (term * ctx_tree)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    35
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    36
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    37
19583
c5fa77b03442 function-package: Changed record usage to make sml/nj happy...
krauss
parents: 19564
diff changeset
    38
datatype fundef_result =
c5fa77b03442 function-package: Changed record usage to make sml/nj happy...
krauss
parents: 19564
diff changeset
    39
  FundefResult of
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    40
     {
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
    41
      fs: term list,
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
    42
      G: term,
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
    43
      R: term,
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents: 19617
diff changeset
    44
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
    45
      psimps : thm list, 
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21835
diff changeset
    46
      trsimps : thm list option, 
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21835
diff changeset
    47
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents: 19617
diff changeset
    48
      subset_pinducts : thm list, 
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents: 19617
diff changeset
    49
      simple_pinducts : thm list, 
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents: 19617
diff changeset
    50
      cases : thm,
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents: 19617
diff changeset
    51
      termination : thm,
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21835
diff changeset
    52
      domintros : thm list option
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents: 19617
diff changeset
    53
     }
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents: 19617
diff changeset
    54
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
    55
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    56
datatype fundef_context_data =
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    57
  FundefCtxData of
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    58
     {
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
    59
      defname : string,
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
    60
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21835
diff changeset
    61
      add_simps : string -> Attrib.src list -> thm list -> local_theory -> thm list * local_theory,
19770
be5c23ebe1eb HOL/Tools/function_package: Added support for mutual recursive definitions.
krauss
parents: 19617
diff changeset
    62
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
    63
      fs : term list,
21255
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    64
      R : term,
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    65
      
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    66
      psimps: thm list,
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    67
      pinducts: thm list,
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    68
      termination: thm
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    69
     }
617fdb08abe9 added profiling code, improved handling of proof terms, generation of domain
krauss
parents: 21237
diff changeset
    70
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
    71
fun morph_fundef_data phi (FundefCtxData {add_simps, fs, R, psimps, pinducts, termination, defname}) =
22623
5fcee5b319a2 proper handling of morphisms
krauss
parents: 22498
diff changeset
    72
    let
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
    73
      val term = Morphism.term phi val thm = Morphism.thm phi val fact = Morphism.fact phi
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
    74
      val name = Morphism.name phi
22623
5fcee5b319a2 proper handling of morphisms
krauss
parents: 22498
diff changeset
    75
    in
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
    76
      FundefCtxData { add_simps = add_simps (* contains no logical entities *), 
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
    77
                      fs = map term fs, R = term R, psimps = fact psimps, 
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
    78
                      pinducts = fact pinducts, termination = thm termination,
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
    79
                      defname = name defname }
22623
5fcee5b319a2 proper handling of morphisms
krauss
parents: 22498
diff changeset
    80
    end
5fcee5b319a2 proper handling of morphisms
krauss
parents: 22498
diff changeset
    81
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
    82
structure FundefData = GenericDataFun
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22760
diff changeset
    83
(
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22760
diff changeset
    84
  type T = (term * fundef_context_data) NetRules.T;
22760
6eafeffe801c repaired value restriction problem
haftmann
parents: 22733
diff changeset
    85
  val empty = NetRules.init
6eafeffe801c repaired value restriction problem
haftmann
parents: 22733
diff changeset
    86
    (op aconv o pairself fst : (term * fundef_context_data) * (term * fundef_context_data) -> bool)
6eafeffe801c repaired value restriction problem
haftmann
parents: 22733
diff changeset
    87
    fst;
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    88
  val copy = I;
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    89
  val extend = I;
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
    90
  fun merge _ (tab1, tab2) = NetRules.merge (tab1, tab2)
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22760
diff changeset
    91
);
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    92
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    93
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
    94
structure FundefCongs = GenericDataFun
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22760
diff changeset
    95
(
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22760
diff changeset
    96
  type T = thm list
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22760
diff changeset
    97
  val empty = []
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22760
diff changeset
    98
  val extend = I
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22760
diff changeset
    99
  fun merge _ = Drule.merge_rules
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22760
diff changeset
   100
);
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   101
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   102
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   103
(* Generally useful?? *)
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   104
fun lift_morphism thy f = 
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   105
    let 
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   106
      val term = Drule.term_rule thy f
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   107
    in
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   108
      Morphism.thm_morphism f $> Morphism.term_morphism term $> Morphism.typ_morphism (Logic.type_map term)
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   109
    end
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   110
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   111
fun import_fundef_data t ctxt =
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   112
    let
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   113
      val thy = Context.theory_of ctxt
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   114
      val ct = cterm_of thy t
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   115
      val inst_morph = lift_morphism thy o Thm.instantiate 
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   116
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   117
      fun match data = 
22903
95ad1a91ecef Thm.match;
wenzelm
parents: 22846
diff changeset
   118
          SOME (morph_fundef_data (inst_morph (Thm.match (cterm_of thy (fst data), ct))) (snd data))
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   119
          handle Pattern.MATCH => NONE
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   120
    in 
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   121
      get_first match (NetRules.retrieve (FundefData.get ctxt) t)
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   122
    end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   123
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   124
fun import_last_fundef ctxt =
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   125
    case NetRules.rules (FundefData.get ctxt) of
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   126
      [] => NONE
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   127
    | (t, data) :: _ =>
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   128
      let 
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   129
        val ([t'], ctxt') = Variable.import_terms true [t] (Context.proof_of ctxt)
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   130
      in
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   131
        import_fundef_data t' (Context.Proof ctxt')
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   132
      end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   133
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   134
val all_fundef_data = NetRules.rules o FundefData.get
21319
cf814e36f788 replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents: 21255
diff changeset
   135
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   136
val map_fundef_congs = FundefCongs.map 
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   137
val get_fundef_congs = FundefCongs.get
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   138
20654
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20523
diff changeset
   139
21319
cf814e36f788 replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents: 21255
diff changeset
   140
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   141
structure TerminationRule = GenericDataFun
22846
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22760
diff changeset
   142
(
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22760
diff changeset
   143
  type T = thm list
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22760
diff changeset
   144
  val empty = []
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22760
diff changeset
   145
  val extend = I
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22760
diff changeset
   146
  fun merge _ = Drule.merge_rules
fb79144af9a3 simplified DataFun interfaces;
wenzelm
parents: 22760
diff changeset
   147
);
21319
cf814e36f788 replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents: 21255
diff changeset
   148
22733
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   149
val get_termination_rules = TerminationRule.get
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   150
val store_termination_rule = TerminationRule.map o cons
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   151
val apply_termination_rule = resolve_tac o get_termination_rules o Context.Proof
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   152
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   153
fun add_fundef_data (data as FundefCtxData {fs, termination, ...}) =
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   154
    FundefData.map (fold (fn f => NetRules.insert (f, data)) fs)
0b14bb35be90 definition lookup via terms, not names. Methods "relation" and "lexicographic_order"
krauss
parents: 22623
diff changeset
   155
    #> store_termination_rule termination
21319
cf814e36f788 replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents: 21255
diff changeset
   156
cf814e36f788 replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents: 21255
diff changeset
   157
20654
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20523
diff changeset
   158
(* Configuration management *)
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20523
diff changeset
   159
datatype fundef_opt 
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20523
diff changeset
   160
  = Sequential
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20523
diff changeset
   161
  | Default of string
21051
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20874
diff changeset
   162
  | Target of xstring
21319
cf814e36f788 replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents: 21255
diff changeset
   163
  | DomIntros
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21835
diff changeset
   164
  | Tailrec
20654
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20523
diff changeset
   165
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20523
diff changeset
   166
datatype fundef_config
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20523
diff changeset
   167
  = FundefConfig of
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20523
diff changeset
   168
   {
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20523
diff changeset
   169
    sequential: bool,
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20523
diff changeset
   170
    default: string,
21319
cf814e36f788 replaced "auto_term" by the simpler method "relation", which does not try
krauss
parents: 21255
diff changeset
   171
    target: xstring option,
22166
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21835
diff changeset
   172
    domintros: bool,
0a50d4db234a * Preliminary implementation of tail recursion
krauss
parents: 21835
diff changeset
   173
    tailrec: bool
20654
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20523
diff changeset
   174
   }
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20523
diff changeset
   175
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   176
fun apply_opt Sequential (FundefConfig {sequential, default, target, domintros,tailrec}) = 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   177
    FundefConfig {sequential=true, default=default, target=target, domintros=domintros, tailrec=tailrec}
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   178
  | apply_opt (Default d) (FundefConfig {sequential, default, target, domintros,tailrec}) = 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   179
    FundefConfig {sequential=sequential, default=d, target=target, domintros=domintros, tailrec=tailrec}
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   180
  | apply_opt (Target t) (FundefConfig {sequential, default, target, domintros,tailrec}) =
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   181
    FundefConfig {sequential=sequential, default=default, target=SOME t, domintros=domintros, tailrec=tailrec}
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   182
  | apply_opt DomIntros (FundefConfig {sequential, default, target, domintros,tailrec}) =
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   183
    FundefConfig {sequential=sequential, default=default, target=target, domintros=true,tailrec=tailrec}
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   184
  | apply_opt Tailrec (FundefConfig {sequential, default, target, domintros,tailrec}) =
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   185
    FundefConfig {sequential=sequential, default=default, target=target, domintros=domintros,tailrec=true}
20654
d80502f0d701 1. Function package accepts a parameter (default "some_term"), which specifies the functions
krauss
parents: 20523
diff changeset
   186
22498
62cdd4b3e96b made function syntax strict, requiring | to separate equations; cleanup
krauss
parents: 22279
diff changeset
   187
fun target_of (FundefConfig {target, ...}) = target
21051
c49467a9c1e1 Switched function package to use the new package for inductive predicates.
krauss
parents: 20874
diff changeset
   188
23189
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   189
(* Common operations on equations *)
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   190
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   191
fun open_all_all (Const ("all", _) $ Abs (n, T, b)) = apfst (cons (n, T)) (open_all_all b)
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   192
  | open_all_all t = ([], t)
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   193
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   194
exception MalformedEquation of term
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   195
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   196
fun split_def geq =
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   197
    let
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   198
      val (qs, imp) = open_all_all geq
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   199
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   200
      val gs = Logic.strip_imp_prems imp
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   201
      val eq = Logic.strip_imp_concl imp
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   202
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   203
      val (f_args, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop eq)
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   204
          handle TERM _ => raise MalformedEquation geq
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   205
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   206
      val (head, args) = strip_comb f_args
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   207
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   208
      val fname = fst (dest_Free head)
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   209
          handle TERM _ => raise MalformedEquation geq
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   210
    in
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   211
      (fname, qs, gs, args, rhs)
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   212
    end
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   213
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   214
exception ArgumentCount of string
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   215
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   216
fun mk_arities fqgars =
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   217
    let fun f (fname, _, _, args, _) arities =
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   218
            let val k = length args
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   219
            in
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   220
              case Symtab.lookup arities fname of
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   221
                NONE => Symtab.update (fname, k) arities
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   222
              | SOME i => (if i = k then arities else raise ArgumentCount fname)
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   223
            end
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   224
    in
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   225
      fold f fqgars Symtab.empty
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   226
    end
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   227
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   228
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   229
(* Check for all sorts of errors in the input *)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   230
fun check_defs ctxt fixes eqs =
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   231
    let
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   232
      val fnames = map (fst o fst) fixes
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   233
                                
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   234
      fun check geq = 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   235
          let
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   236
            fun input_error msg = cat_lines [msg, ProofContext.string_of_term ctxt geq]
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   237
                                  
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   238
            val fqgar as (fname, qs, gs, args, rhs) = split_def geq
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   239
                                 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   240
            val _ = fname mem fnames 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   241
                    orelse error (input_error ("Head symbol of left hand side must be " ^ plural "" "one out of " fnames 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   242
                                               ^ commas_quote fnames))
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   243
                                            
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   244
            fun add_bvs t is = add_loose_bnos (t, 0, is)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   245
            val rvs = (add_bvs rhs [] \\ fold add_bvs args [])
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   246
                        |> map (fst o nth (rev qs))
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   247
                      
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   248
            val _ = null rvs orelse error (input_error ("Variable" ^ plural " " "s " rvs ^ commas_quote rvs
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   249
                                                        ^ " occur" ^ plural "s" "" rvs ^ " on right hand side only:"))
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   250
                                    
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   251
            val _ = forall (FundefLib.forall_aterms (fn Free (n, _) => not (n mem fnames) | _ => true)) gs 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   252
                    orelse error (input_error "Recursive Calls not allowed in premises")
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   253
          in
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   254
            fqgar
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   255
          end
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   256
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   257
      val _ = mk_arities (map check eqs)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   258
          handle ArgumentCount fname => 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   259
                 error ("Function " ^ quote fname ^ " has different numbers of arguments in different equations")
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   260
    in
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   261
      ()
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   262
    end
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   263
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   264
(* Preprocessors *)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   265
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   266
type fixes = ((string * typ) * mixfix) list
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   267
type 'a spec = ((bstring * Attrib.src list) * 'a list) list
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   268
type preproc = fundef_config -> bool list -> Proof.context -> fixes -> term spec -> (term list * (thm list -> thm spec))
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   269
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   270
fun empty_preproc check _ _ ctxt fixes spec =
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   271
    let 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   272
      val (nas,tss) = split_list spec
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   273
      val _ = check ctxt fixes (flat tss)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   274
    in
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   275
      (flat tss, curry op ~~ nas o Library.unflat tss)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   276
    end
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   277
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   278
structure Preprocessor = GenericDataFun
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   279
(
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   280
  type T = preproc
23206
209e32e7c91e made SML/NJ happy;
wenzelm
parents: 23203
diff changeset
   281
  val empty : T = empty_preproc check_defs
23203
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   282
  val extend = I
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   283
  fun merge _ (a, _) = a
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   284
);
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   285
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   286
val get_preproc = Preprocessor.get o Context.Proof
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   287
val set_preproc = Preprocessor.map o K
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   288
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   289
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   290
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   291
local 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   292
  structure P = OuterParse and K = OuterKeyword 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   293
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   294
  val opt_sequential = Scan.optional ((P.$$$ "(" |-- P.$$$ "sequential" --| P.$$$ ")") >> K true) false
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   295
                       
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   296
  val option_parser = (P.$$$ "sequential" >> K Sequential)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   297
                   || ((P.reserved "default" |-- P.term) >> Default)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   298
                   || (P.reserved "domintros" >> K DomIntros)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   299
                   || (P.reserved "tailrec" >> K Tailrec)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   300
                   || ((P.$$$ "in" |-- P.xname) >> Target)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   301
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   302
  fun config_parser default = (Scan.optional (P.$$$ "(" |-- P.!!! (P.list1 (P.group "option" option_parser)) --| P.$$$ ")") [])
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   303
                              >> (fn opts => fold apply_opt opts default)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   304
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   305
  val otherwise = P.$$$ "(" |-- P.$$$ "otherwise" --| P.$$$ ")"
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   306
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   307
  fun pipe_error t = P.!!! (Scan.fail_with (K (cat_lines ["Equations must be separated by " ^ quote "|", quote t])))
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   308
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   309
  val statement_ow = SpecParse.opt_thm_name ":" -- (P.prop -- Scan.optional (otherwise >> K true) false)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   310
                     --| Scan.ahead ((P.term :-- pipe_error) || Scan.succeed ("",""))
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   311
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   312
  val statements_ow = P.enum1 "|" statement_ow
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   313
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   314
  val flags_statements = statements_ow
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   315
                         >> (fn sow => (map (snd o snd) sow, map (apsnd fst) sow))
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   316
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   317
  fun basic_apply_flags ((config, fixes), (flags, statements)) = ((config, fixes), statements)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   318
in
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   319
  fun fundef_parser default_cfg = (config_parser default_cfg -- P.fixes --| P.$$$ "where" -- flags_statements)
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   320
                                  
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   321
end
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   322
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   323
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   324
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   325
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   326
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   327
val default_config = FundefConfig { sequential=false, default="%x. arbitrary", 
a5026e73cfcf "function (sequential)" and "fun" now handle incomplete patterns silently by adding "undefined" cases.
krauss
parents: 23189
diff changeset
   328
                                    target=NONE, domintros=false, tailrec=false }
23189
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   329
4574ab8f3b21 simplified interfaces, some restructuring
krauss
parents: 22903
diff changeset
   330
23215
20b5558a5419 local open FundefLib;
wenzelm
parents: 23206
diff changeset
   331
end
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   332
end
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   333
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   334
(* Common Abbreviations *)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   335
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   336
structure FundefAbbrev =
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   337
struct
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   338
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   339
fun implies_elim_swp x y = implies_elim y x
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   340
22498
62cdd4b3e96b made function syntax strict, requiring | to separate equations; cleanup
krauss
parents: 22279
diff changeset
   341
(* HOL abbreviations *)
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   342
val boolT = HOLogic.boolT
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   343
val mk_prod = HOLogic.mk_prod
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   344
val mk_eq = HOLogic.mk_eq
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20289
diff changeset
   345
val eq_const = HOLogic.eq_const
19564
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   346
val Trueprop = HOLogic.mk_Trueprop
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   347
val mk_relT = HOLogic.mk_setT o HOLogic.mk_prodT
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   348
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   349
fun free_to_var (Free (v,T)) = Var ((v,0),T)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   350
  | free_to_var _ = raise Match
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   351
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   352
fun var_to_free (Var ((v,_),T)) = Free (v,T)
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   353
  | var_to_free _ = raise Match
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   354
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   355
d3e2f532459a First usable version of the new function definition package (HOL/function_packake/...).
krauss
parents:
diff changeset
   356
end