src/HOL/Tools/Function/function_common.ML
author wenzelm
Fri, 06 Mar 2015 15:58:56 +0100
changeset 59621 291934bac95e
parent 59618 e6939796717e
child 60682 5a6cd2560549
permissions -rw-r--r--
Thm.cterm_of and Thm.ctyp_of operate on local context;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
     1
(*  Title:      HOL/Tools/Function/function_common.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: 39276
diff changeset
     4
Common definitions and other infrastructure for 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
34230
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
     7
signature FUNCTION_DATA =
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
     8
sig
59159
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
     9
  type info =
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    10
   {is_partial : bool,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    11
    defname : string,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    12
      (*contains no logical entities: invariant under morphisms:*)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    13
    add_simps : (binding -> binding) -> string -> (binding -> binding) ->
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    14
      Token.src list -> thm list -> local_theory -> thm list * local_theory,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    15
    fnames : string list,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    16
    case_names : string list,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    17
    fs : term list,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    18
    R : term,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    19
    dom: term,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    20
    psimps: thm list,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    21
    pinducts: thm list,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    22
    simps : thm list option,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    23
    inducts : thm list option,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    24
    termination : thm,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    25
    cases : thm list,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    26
    pelims: thm list list,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    27
    elims: thm list list option}
34230
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
    28
end
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
    29
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
    30
structure Function_Data : FUNCTION_DATA =
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
    31
struct
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
    32
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
    33
type info =
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
    34
 {is_partial : bool,
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
    35
  defname : string,
59159
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    36
    (*contains no logical entities: invariant under morphisms:*)
34230
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
    37
  add_simps : (binding -> binding) -> string -> (binding -> binding) ->
58011
bc6bced136e5 tuned signature -- moved type src to Token, without aliases;
wenzelm
parents: 57959
diff changeset
    38
    Token.src list -> thm list -> local_theory -> thm list * local_theory,
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
    39
  fnames : string list,
34230
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
    40
  case_names : string list,
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
    41
  fs : term list,
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
    42
  R : term,
52384
80c00a851de5 export dom predicate in the info record
krauss
parents: 52383
diff changeset
    43
  dom: term,
34230
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
    44
  psimps: thm list,
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
    45
  pinducts: thm list,
34231
da4d7d40f2f9 provide simp and induct rules in Function.info
krauss
parents: 34230
diff changeset
    46
  simps : thm list option,
da4d7d40f2f9 provide simp and induct rules in Function.info
krauss
parents: 34230
diff changeset
    47
  inducts : thm list option,
52383
71df93ff010d export cases rule in the info record
krauss
parents: 49979
diff changeset
    48
  termination : thm,
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
    49
  cases : thm list,
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
    50
  pelims : thm list list,
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
    51
  elims : thm list list option}
34230
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
    52
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
    53
end
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
    54
49965
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
    55
signature FUNCTION_COMMON =
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
    56
sig
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
    57
  include FUNCTION_DATA
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
    58
  val profile : bool Unsynchronized.ref
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
    59
  val PROFILE : string -> ('a -> 'b) -> 'a -> 'b
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
    60
  val mk_acc : typ -> term -> term
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
    61
  val function_name : string -> string
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
    62
  val graph_name : string -> string
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
    63
  val rel_name : string -> string
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
    64
  val dom_name : string -> string
59159
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    65
  val split_def : Proof.context -> (string -> 'a) -> term ->
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    66
    string * (string * typ) list * term list * term list * term
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    67
  val check_defs : Proof.context -> ((string * typ) * 'a) list -> term list -> unit
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    68
  type fixes = ((string * typ) * mixfix) list
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    69
  type 'a spec = (Attrib.binding * 'a list) list
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    70
  datatype function_config = FunctionConfig of
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    71
   {sequential: bool,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    72
    default: string option,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    73
    domintros: bool,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    74
    partials: bool}
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    75
  type preproc = function_config -> Proof.context -> fixes -> term spec ->
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    76
    (term list * (thm list -> thm spec) * (thm list -> thm list list) * string list)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    77
  val fname_of : term -> string
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    78
  val mk_case_names : int -> string -> int -> string list
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    79
  val empty_preproc : (Proof.context -> ((string * typ) * mixfix) list -> term list -> 'c) ->
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    80
    preproc
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    81
  val termination_rule_tac : Proof.context -> int -> tactic
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    82
  val store_termination_rule : thm -> Context.generic -> Context.generic
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    83
  val get_functions : Proof.context -> (term * info) Item_Net.T
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    84
  val add_function_data : info -> Context.generic -> Context.generic
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    85
  val termination_prover_tac : Proof.context -> tactic
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    86
  val set_termination_prover : (Proof.context -> tactic) -> Context.generic -> Context.generic
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    87
  val get_preproc: Proof.context -> preproc
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
    88
  val set_preproc: preproc -> Context.generic -> Context.generic
49965
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
    89
  datatype function_result = FunctionResult of
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
    90
   {fs: term list,
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
    91
    G: term,
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
    92
    R: term,
52384
80c00a851de5 export dom predicate in the info record
krauss
parents: 52383
diff changeset
    93
    dom: term,
49965
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
    94
    psimps : thm list,
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
    95
    simple_pinducts : thm list,
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
    96
    cases : thm list,
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
    97
    pelims : thm list list,
49965
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
    98
    termination : thm,
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
    99
    domintros : thm list option}
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
   100
  val transform_function_data : info -> morphism -> info
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
   101
  val import_function_data : term -> Proof.context -> info option
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
   102
  val import_last_function : Proof.context -> info option
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
   103
  val default_config : function_config
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
   104
  val function_parser : function_config ->
59159
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   105
    ((function_config * (binding * string option * mixfix) list) *
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   106
      (Attrib.binding * string) list) parser
49965
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
   107
end
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
   108
ee25a04fa06a proper signatures;
wenzelm
parents: 46949
diff changeset
   109
structure Function_Common : FUNCTION_COMMON =
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   110
struct
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   111
34230
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
   112
open Function_Data
b0d21ae2528e more official data record Function.info
krauss
parents: 33751
diff changeset
   113
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   114
local open Function_Lib in
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   115
59159
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   116
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   117
(* profiling *)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   118
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   119
val profile = Unsynchronized.ref false
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   120
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   121
fun PROFILE msg = if !profile then timeap_msg msg else I
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   122
54295
45a5523d4a63 qualifed popular user space names
haftmann
parents: 53603
diff changeset
   123
val acc_const_name = @{const_name Wellfounded.accp}
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   124
fun mk_acc domT R =
59159
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   125
  Const (acc_const_name, (domT --> domT --> HOLogic.boolT) --> domT --> HOLogic.boolT) $ R
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   126
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   127
val function_name = suffix "C"
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   128
val graph_name = suffix "_graph"
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   129
val rel_name = suffix "_rel"
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   130
val dom_name = suffix "_dom"
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   131
59159
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   132
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   133
(* analyzing function equations *)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   134
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   135
fun split_def ctxt check_head geq =
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   136
  let
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   137
    fun input_error msg = cat_lines [msg, Syntax.string_of_term ctxt geq]
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   138
    val qs = Term.strip_qnt_vars @{const_name Pure.all} geq
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   139
    val imp = Term.strip_qnt_body @{const_name Pure.all} geq
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   140
    val (gs, eq) = Logic.strip_horn imp
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   141
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   142
    val (f_args, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop eq)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   143
      handle TERM _ => error (input_error "Not an equation")
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   144
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   145
    val (head, args) = strip_comb f_args
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   146
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   147
    val fname = fst (dest_Free head) handle TERM _ => ""
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   148
    val _ = check_head fname
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   149
  in
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   150
    (fname, qs, gs, args, rhs)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   151
  end
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   152
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   153
(*check for all sorts of errors in the input*)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   154
fun check_defs ctxt fixes eqs =
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   155
  let
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   156
    val fnames = map (fst o fst) fixes
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   157
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   158
    fun check geq =
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   159
      let
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   160
        fun input_error msg = error (cat_lines [msg, Syntax.string_of_term ctxt geq])
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   161
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   162
        fun check_head fname =
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   163
          member (op =) fnames fname orelse
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   164
          input_error ("Illegal equation head. Expected " ^ commas_quote fnames)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   165
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   166
        val (fname, qs, gs, args, rhs) = split_def ctxt check_head geq
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   167
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   168
        val _ = length args > 0 orelse input_error "Function has no arguments:"
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   169
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   170
        fun add_bvs t is = add_loose_bnos (t, 0, is)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   171
        val rvs = (subtract (op =) (fold add_bvs args []) (add_bvs rhs []))
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   172
                    |> map (fst o nth (rev qs))
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   173
59159
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   174
        val _ = null rvs orelse input_error
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   175
          ("Variable" ^ plural " " "s " rvs ^ commas_quote rvs ^
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   176
           " occur" ^ plural "s" "" rvs ^ " on right hand side only:")
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   177
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   178
        val _ = forall (not o Term.exists_subterm
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   179
          (fn Free (n, _) => member (op =) fnames n | _ => false)) (gs @ args)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   180
          orelse input_error "Defined function may not occur in premises or arguments"
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   181
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   182
        val freeargs = map (fn t => subst_bounds (rev (map Free qs), t)) args
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   183
        val funvars =
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   184
          filter
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   185
            (fn q => exists (exists_subterm (fn (Free q') $ _ => q = q' | _ => false)) freeargs) qs
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   186
        val _ = null funvars orelse (warning (cat_lines
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   187
          ["Bound variable" ^ plural " " "s " funvars ^
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   188
          commas_quote (map fst funvars) ^ " occur" ^ plural "s" "" funvars ^
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   189
          " in function position.", "Misspelled constructor???"]); true)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   190
      in
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   191
        (fname, length args)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   192
      end
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   193
59159
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   194
    val grouped_args = AList.group (op =) (map check eqs)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   195
    val _ = grouped_args
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   196
      |> map (fn (fname, ars) =>
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   197
        length (distinct (op =) ars) = 1 orelse
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   198
          error ("Function " ^ quote fname ^
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   199
            " has different numbers of arguments in different equations"))
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   200
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   201
    val not_defined = subtract (op =) (map fst grouped_args) fnames
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   202
    val _ = null not_defined
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   203
      orelse error ("No defining equations for function" ^
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   204
        plural " " "s " not_defined ^ commas_quote not_defined)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   205
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   206
    fun check_sorts ((fname, fT), _) =
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   207
      Sorts.of_sort (Sign.classes_of (Proof_Context.theory_of ctxt)) (fT, @{sort type})
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   208
      orelse error (cat_lines
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   209
      ["Type of " ^ quote fname ^ " is not of sort " ^ quote "type" ^ ":",
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   210
       Syntax.string_of_typ (Config.put show_sorts true ctxt) fT])
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   211
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   212
    val _ = map check_sorts fixes
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   213
  in
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   214
    ()
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   215
  end
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   216
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   217
59159
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   218
(* preprocessors *)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   219
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   220
type fixes = ((string * typ) * mixfix) list
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   221
type 'a spec = (Attrib.binding * 'a list) list
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   222
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   223
datatype function_config = FunctionConfig of
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   224
 {sequential: bool,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   225
  default: string option,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   226
  domintros: bool,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   227
  partials: bool}
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   228
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   229
type preproc = function_config -> Proof.context -> fixes -> term spec ->
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   230
  (term list * (thm list -> thm spec) * (thm list -> thm list list) * string list)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   231
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   232
val fname_of = fst o dest_Free o fst o strip_comb o fst o HOLogic.dest_eq o
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   233
  HOLogic.dest_Trueprop o Logic.strip_imp_concl o snd o dest_all_all
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   234
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   235
fun mk_case_names i "" k = mk_case_names i (string_of_int (i + 1)) k
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   236
  | mk_case_names _ _ 0 = []
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   237
  | mk_case_names _ n 1 = [n]
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   238
  | mk_case_names _ n k = map (fn i => n ^ "_" ^ string_of_int i) (1 upto k)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   239
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   240
fun empty_preproc check (_: function_config) (ctxt: Proof.context) (fixes: fixes) spec =
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   241
  let
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   242
    val (bnds, tss) = split_list spec
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   243
    val ts = flat tss
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   244
    val _ = check ctxt fixes ts
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   245
    val fnames = map (fst o fst) fixes
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   246
    val indices = map (fn eq => find_index (curry op = (fname_of eq)) fnames) ts
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   247
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   248
    fun sort xs = partition_list (fn i => fn (j,_) => i = j) 0 (length fnames - 1)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   249
      (indices ~~ xs) |> map (map snd)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   250
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   251
    (* using theorem names for case name currently disabled *)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   252
    val cnames = map_index (fn (i, _) => mk_case_names i "" 1) bnds |> flat
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   253
  in
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   254
    (ts, curry op ~~ bnds o Library.unflat tss, sort, cnames)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   255
  end
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   256
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   257
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   258
(* context data *)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   259
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   260
structure Data = Generic_Data
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   261
(
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   262
  type T =
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   263
    thm list *
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   264
    (term * info) Item_Net.T *
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   265
    (Proof.context -> tactic) *
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   266
    preproc
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   267
  val empty: T =
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   268
   ([],
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   269
    Item_Net.init (op aconv o apply2 fst) (single o fst),
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   270
    fn _ => error "Termination prover not configured",
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   271
    empty_preproc check_defs)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   272
  val extend = I
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   273
  fun merge
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   274
   ((termination_rules1, functions1, termination_prover1, preproc1),
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   275
    (termination_rules2, functions2, _, _)) : T =
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   276
   (Thm.merge_thms (termination_rules1, termination_rules2),
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   277
    Item_Net.merge (functions1, functions2),
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   278
    termination_prover1,
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   279
    preproc1)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   280
)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   281
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59159
diff changeset
   282
fun termination_rule_tac ctxt = resolve_tac ctxt (#1 (Data.get (Context.Proof ctxt)))
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 59159
diff changeset
   283
59159
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   284
val store_termination_rule = Data.map o @{apply 4(1)} o cons
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   285
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   286
val get_functions = #2 o Data.get o Context.Proof
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   287
fun add_function_data (info : info as {fs, termination, ...}) =
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   288
  (Data.map o @{apply 4(2)}) (fold (fn f => Item_Net.update (f, info)) fs)
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   289
  #> store_termination_rule termination
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   290
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   291
fun termination_prover_tac ctxt = #3 (Data.get (Context.Proof ctxt)) ctxt
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   292
val set_termination_prover = Data.map o @{apply 4(3)} o K
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   293
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   294
val get_preproc = #4 o Data.get o Context.Proof
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   295
val set_preproc = Data.map o @{apply 4(4)} o K
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   296
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   297
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   298
(* function definition result data *)
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   299
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   300
datatype function_result = FunctionResult of
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   301
 {fs: term list,
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   302
  G: term,
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   303
  R: term,
52384
80c00a851de5 export dom predicate in the info record
krauss
parents: 52383
diff changeset
   304
  dom: term,
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   305
  psimps : thm list,
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   306
  simple_pinducts : thm list,
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   307
  cases : thm list,
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   308
  pelims : thm list list,
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   309
  termination : thm,
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   310
  domintros : thm list option}
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   311
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   312
fun transform_function_data ({add_simps, case_names, fnames, fs, R, dom, psimps, pinducts,
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   313
  simps, inducts, termination, defname, is_partial, cases, pelims, elims} : info) phi =
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   314
    let
45290
f599ac41e7f5 tuned signature -- refined terminology;
wenzelm
parents: 44357
diff changeset
   315
      val term = Morphism.term phi
f599ac41e7f5 tuned signature -- refined terminology;
wenzelm
parents: 44357
diff changeset
   316
      val thm = Morphism.thm phi
f599ac41e7f5 tuned signature -- refined terminology;
wenzelm
parents: 44357
diff changeset
   317
      val fact = Morphism.fact phi
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   318
      val name = Binding.name_of o Morphism.binding phi o Binding.name
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   319
    in
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   320
      { add_simps = add_simps, case_names = case_names, fnames = fnames,
52384
80c00a851de5 export dom predicate in the info record
krauss
parents: 52383
diff changeset
   321
        fs = map term fs, R = term R, dom = term dom, psimps = fact psimps,
34231
da4d7d40f2f9 provide simp and induct rules in Function.info
krauss
parents: 34230
diff changeset
   322
        pinducts = fact pinducts, simps = Option.map fact simps,
da4d7d40f2f9 provide simp and induct rules in Function.info
krauss
parents: 34230
diff changeset
   323
        inducts = Option.map fact inducts, termination = thm termination,
53603
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   324
        defname = name defname, is_partial=is_partial, cases = fact cases,
59ef06cda7b9 generate elim rules for elimination of function equalities;
Manuel Eberl
parents: 52384
diff changeset
   325
        elims = Option.map (map fact) elims, pelims = map fact pelims }
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   326
    end
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   327
59618
e6939796717e clarified context;
wenzelm
parents: 59582
diff changeset
   328
fun lift_morphism ctxt f =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   329
  let
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59618
diff changeset
   330
    fun term t = Thm.term_of (Drule.cterm_rule f (Thm.cterm_of ctxt t))
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   331
  in
54740
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 54295
diff changeset
   332
    Morphism.morphism "lift_morphism"
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 54295
diff changeset
   333
      {binding = [],
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 54295
diff changeset
   334
       typ = [Logic.type_map term],
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 54295
diff changeset
   335
       term = [term],
91f54d386680 maintain morphism names for diagnostic purposes;
wenzelm
parents: 54295
diff changeset
   336
       fact = [map f]}
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   337
  end
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   338
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   339
fun import_function_data t ctxt =
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   340
  let
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59618
diff changeset
   341
    val ct = Thm.cterm_of ctxt t
59618
e6939796717e clarified context;
wenzelm
parents: 59582
diff changeset
   342
    val inst_morph = lift_morphism ctxt o Thm.instantiate
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   343
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   344
    fun match (trm, data) =
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59618
diff changeset
   345
      SOME (transform_function_data data (inst_morph (Thm.match (Thm.cterm_of ctxt trm, ct))))
59618
e6939796717e clarified context;
wenzelm
parents: 59582
diff changeset
   346
        handle Pattern.MATCH => NONE
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   347
  in
59159
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   348
    get_first match (Item_Net.retrieve (get_functions ctxt) t)
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   349
  end
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   350
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   351
fun import_last_function ctxt =
59159
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   352
  (case Item_Net.content (get_functions ctxt) of
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   353
    [] => NONE
49966
cf4c03c019e5 removed dead code;
wenzelm
parents: 49965
diff changeset
   354
  | (t, _) :: _ =>
59159
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   355
      let val ([t'], ctxt') = Variable.import_terms true [t] ctxt
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   356
      in import_function_data t' ctxt' end)
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   357
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   358
59159
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   359
(* configuration management *)
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   360
59159
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   361
datatype function_opt =
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   362
    Sequential
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   363
  | Default of string
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   364
  | DomIntros
33101
8846318b52d0 configuration flag "partials"
krauss
parents: 33099
diff changeset
   365
  | No_Partials
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   366
59159
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   367
fun apply_opt Sequential (FunctionConfig {sequential = _, default, domintros, partials}) =
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   368
      FunctionConfig
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   369
        {sequential = true, default = default, domintros = domintros, partials = partials}
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   370
  | apply_opt (Default d) (FunctionConfig {sequential, default = _, domintros, partials}) =
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   371
      FunctionConfig
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   372
        {sequential = sequential, default = SOME d, domintros = domintros, partials = partials}
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   373
  | apply_opt DomIntros (FunctionConfig {sequential, default, domintros = _, partials}) =
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   374
      FunctionConfig
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   375
        {sequential = sequential, default = default, domintros = true, partials = partials}
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   376
  | apply_opt No_Partials (FunctionConfig {sequential, default, domintros, partials = _}) =
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   377
      FunctionConfig
9312710451f5 just one data slot per program unit;
wenzelm
parents: 59058
diff changeset
   378
        {sequential = sequential, default = default, domintros = domintros, partials = false}
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   379
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   380
val default_config =
41417
211dbd42f95d function (default) is legacy feature
krauss
parents: 41114
diff changeset
   381
  FunctionConfig { sequential=false, default=NONE,
41846
b368a7aee46a removed support for tail-recursion from function package (now implemented by partial_function)
krauss
parents: 41417
diff changeset
   382
    domintros=false, partials=true}
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   383
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   384
local
44357
5f5649ac8235 tuned Parse.group: delayed failure message;
wenzelm
parents: 42361
diff changeset
   385
  val option_parser = Parse.group (fn () => "option")
36960
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36954
diff changeset
   386
    ((Parse.reserved "sequential" >> K Sequential)
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36954
diff changeset
   387
     || ((Parse.reserved "default" |-- Parse.term) >> Default)
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36954
diff changeset
   388
     || (Parse.reserved "domintros" >> K DomIntros)
41846
b368a7aee46a removed support for tail-recursion from function package (now implemented by partial_function)
krauss
parents: 41417
diff changeset
   389
     || (Parse.reserved "no_partials" >> K No_Partials))
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   390
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   391
  fun config_parser default =
46949
94aa7b81bcf6 prefer formally checked @{keyword} parser;
wenzelm
parents: 46496
diff changeset
   392
    (Scan.optional (@{keyword "("} |-- Parse.!!! (Parse.list1 option_parser) --| @{keyword ")"}) [])
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   393
     >> (fn opts => fold apply_opt opts default)
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   394
in
34232
36a2a3029fd3 new year's resolution: reindented code in function package
krauss
parents: 34231
diff changeset
   395
  fun function_parser default_cfg =
36960
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 36954
diff changeset
   396
      config_parser default_cfg -- Parse.fixes -- Parse_Spec.where_alt_specs
33099
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   397
end
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   398
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   399
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   400
end
b8cdd3d73022 function package: more standard names for structures and files
krauss
parents:
diff changeset
   401
end