Nominal/nominal_function_common.ML
author Christian Urban <urbanc@in.tum.de>
Sun, 05 Jun 2011 21:14:23 +0100
changeset 2819 4bd584ff4fab
child 2821 c7d4bd9e89e0
permissions -rw-r--r--
added an option for an invariant (at the moment only a stub)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2819
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
(*  Nominal Function Common
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
    Author: Christian Urban
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
Common definitions and other infrastructure for the function package.
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
*)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
signature FUNCTION_DATA =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
sig
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
type info =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
 {is_partial : bool,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
  defname : string,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
    (* contains no logical entities: invariant under morphisms: *)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
  add_simps : (binding -> binding) -> string -> (binding -> binding) ->
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
    Attrib.src list -> thm list -> local_theory -> thm list * local_theory,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  case_names : string list,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  fs : term list,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
  R : term,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
  psimps: thm list,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  pinducts: thm list,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
  simps : thm list option,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  inducts : thm list option,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  termination: thm}
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
end
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
structure Function_Data : FUNCTION_DATA =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
struct
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
type info =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
 {is_partial : bool,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  defname : string,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
    (* contains no logical entities: invariant under morphisms: *)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
  add_simps : (binding -> binding) -> string -> (binding -> binding) ->
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
    Attrib.src list -> thm list -> local_theory -> thm list * local_theory,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
  case_names : string list,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  fs : term list,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
  R : term,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
  psimps: thm list,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
  pinducts: thm list,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  simps : thm list option,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
  inducts : thm list option,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  termination: thm}
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
end
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
structure Nominal_Function_Common =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
struct
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
open Function_Data
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
local open Function_Lib in
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
(* Profiling *)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
val profile = Unsynchronized.ref false;
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
fun PROFILE msg = if !profile then timeap_msg msg else I
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
val acc_const_name = @{const_name accp}
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
fun mk_acc domT R =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
  Const (acc_const_name, (domT --> domT --> HOLogic.boolT) --> domT --> HOLogic.boolT) $ R 
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
val function_name = suffix "C"
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
val graph_name = suffix "_graph"
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
val rel_name = suffix "_rel"
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
val dom_name = suffix "_dom"
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
(* Termination rules *)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
structure TerminationRule = Generic_Data
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
(
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  type T = thm list
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  val empty = []
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
  val extend = I
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
  val merge = Thm.merge_thms
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
);
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
val get_termination_rules = TerminationRule.get
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
val store_termination_rule = TerminationRule.map o cons
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
val apply_termination_rule = resolve_tac o get_termination_rules o Context.Proof
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
(* Function definition result data *)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
datatype function_result = FunctionResult of
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
 {fs: term list,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
  G: term,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  R: term,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
  psimps : thm list,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
  simple_pinducts : thm list,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
  cases : thm,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
  termination : thm,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
  domintros : thm list option}
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
fun morph_function_data ({add_simps, case_names, fs, R, psimps, pinducts,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
  simps, inducts, termination, defname, is_partial} : info) phi =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
    let
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
      val term = Morphism.term phi val thm = Morphism.thm phi val fact = Morphism.fact phi
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
      val name = Binding.name_of o Morphism.binding phi o Binding.name
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
    in
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
      { add_simps = add_simps, case_names = case_names,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
        fs = map term fs, R = term R, psimps = fact psimps,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
        pinducts = fact pinducts, simps = Option.map fact simps,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
        inducts = Option.map fact inducts, termination = thm termination,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
        defname = name defname, is_partial=is_partial }
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
    end
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
structure FunctionData = Generic_Data
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
(
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
  type T = (term * info) Item_Net.T;
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
  val empty : T = Item_Net.init (op aconv o pairself fst) (single o fst);
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
  val extend = I;
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
  fun merge tabs : T = Item_Net.merge tabs;
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
val get_function = FunctionData.get o Context.Proof;
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
fun lift_morphism thy f =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
  let
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
    val term = Drule.term_rule thy f
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
  in
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
    Morphism.thm_morphism f $> Morphism.term_morphism term
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
    $> Morphism.typ_morphism (Logic.type_map term)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
  end
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
fun import_function_data t ctxt =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
  let
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
    val thy = Proof_Context.theory_of ctxt
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
    val ct = cterm_of thy t
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
    val inst_morph = lift_morphism thy o Thm.instantiate
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
    fun match (trm, data) =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
      SOME (morph_function_data data (inst_morph (Thm.match (cterm_of thy trm, ct))))
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
      handle Pattern.MATCH => NONE
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
  in
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
    get_first match (Item_Net.retrieve (get_function ctxt) t)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
  end
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
fun import_last_function ctxt =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
  case Item_Net.content (get_function ctxt) of
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
    [] => NONE
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
  | (t, data) :: _ =>
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
    let
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
      val ([t'], ctxt') = Variable.import_terms true [t] ctxt
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
    in
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
      import_function_data t' ctxt'
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
    end
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
val all_function_data = Item_Net.content o get_function
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
fun add_function_data (data : info as {fs, termination, ...}) =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
  FunctionData.map (fold (fn f => Item_Net.update (f, data)) fs)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
  #> store_termination_rule termination
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
(* Simp rules for termination proofs *)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
structure Termination_Simps = Named_Thms
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
(
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
  val name = "termination_simp"
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
  val description = "simplification rules for termination proofs"
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
(* Default Termination Prover *)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
structure TerminationProver = Generic_Data
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
(
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
  type T = Proof.context -> tactic
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
  val empty = (fn _ => error "Termination prover not configured")
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
  val extend = I
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
  fun merge (a, _) = a
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
val set_termination_prover = TerminationProver.put
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
val get_termination_prover = TerminationProver.get o Context.Proof
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
(* Configuration management *)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
datatype nominal_function_opt
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
  = Sequential
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
  | Default of string
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
  | DomIntros
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
  | No_Partials
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
  | Invariant of string
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
datatype nominal_function_config = NominalFunctionConfig of
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
 {sequential: bool,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
  default: string option,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
  domintros: bool,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
  partials: bool,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
  inv: string option}
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   195
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
fun apply_opt Sequential (NominalFunctionConfig {sequential, default, domintros, partials, inv}) =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
    NominalFunctionConfig 
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
      {sequential=true, default=default, domintros=domintros, partials=partials, inv=inv}
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
  | apply_opt (Default d) (NominalFunctionConfig {sequential, default, domintros, partials, inv}) =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
    NominalFunctionConfig 
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
      {sequential=sequential, default=SOME d, domintros=domintros, partials=partials, inv=inv}
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
  | apply_opt DomIntros (NominalFunctionConfig {sequential, default, domintros, partials, inv}) =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
    NominalFunctionConfig 
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
      {sequential=sequential, default=default, domintros=true, partials=partials, inv=inv}
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   205
  | apply_opt No_Partials (NominalFunctionConfig {sequential, default, domintros, partials, inv}) =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
    NominalFunctionConfig 
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   207
      {sequential=sequential, default=default, domintros=domintros, partials=false, inv=inv}
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
  | apply_opt (Invariant s) (NominalFunctionConfig {sequential, default, domintros, partials, inv}) =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
    NominalFunctionConfig 
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   210
      {sequential=sequential, default=default, domintros=domintros, partials=partials, inv = SOME s}
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   212
val nominal_default_config =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
  NominalFunctionConfig { sequential=false, default=NONE,
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
    domintros=false, partials=true, inv=NONE}
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   215
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   216
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   217
(* Analyzing function equations *)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   219
fun split_def ctxt check_head geq =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   220
  let
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   221
    fun input_error msg = cat_lines [msg, Syntax.string_of_term ctxt geq]
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222
    val qs = Term.strip_qnt_vars "all" geq
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   223
    val imp = Term.strip_qnt_body "all" geq
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   224
    val (gs, eq) = Logic.strip_horn imp
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   225
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
    val (f_args, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop eq)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   227
      handle TERM _ => error (input_error "Not an equation")
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   228
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   229
    val (head, args) = strip_comb f_args
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   230
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   231
    val fname = fst (dest_Free head) handle TERM _ => ""
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   232
    val _ = check_head fname
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
  in
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
    (fname, qs, gs, args, rhs)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
  end
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
(* Check for all sorts of errors in the input *)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
fun check_defs ctxt fixes eqs =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
  let
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
    val fnames = map (fst o fst) fixes
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   241
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   242
    fun check geq =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   243
      let
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   244
        fun input_error msg = error (cat_lines [msg, Syntax.string_of_term ctxt geq])
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   245
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   246
        fun check_head fname =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   247
          member (op =) fnames fname orelse
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   248
          input_error ("Illegal equation head. Expected " ^ commas_quote fnames)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   249
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   250
        val (fname, qs, gs, args, rhs) = split_def ctxt check_head geq
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   251
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   252
        val _ = length args > 0 orelse input_error "Function has no arguments:"
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   253
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   254
        fun add_bvs t is = add_loose_bnos (t, 0, is)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   255
        val rvs = (subtract (op =) (fold add_bvs args []) (add_bvs rhs []))
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   256
                    |> map (fst o nth (rev qs))
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   257
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   258
        val _ = null rvs orelse input_error
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   259
          ("Variable" ^ plural " " "s " rvs ^ commas_quote rvs ^
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   260
           " occur" ^ plural "s" "" rvs ^ " on right hand side only:")
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   261
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   262
        val _ = forall (not o Term.exists_subterm
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   263
          (fn Free (n, _) => member (op =) fnames n | _ => false)) (gs @ args)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   264
          orelse input_error "Defined function may not occur in premises or arguments"
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   265
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   266
        val freeargs = map (fn t => subst_bounds (rev (map Free qs), t)) args
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   267
        val funvars = filter (fn q => exists (exists_subterm (fn (Free q') $ _ => q = q' | _ => false)) freeargs) qs
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   268
        val _ = null funvars orelse (warning (cat_lines
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   269
          ["Bound variable" ^ plural " " "s " funvars ^
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   270
          commas_quote (map fst funvars) ^ " occur" ^ plural "s" "" funvars ^
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   271
          " in function position.", "Misspelled constructor???"]); true)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   272
      in
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   273
        (fname, length args)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   274
      end
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   275
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   276
    val grouped_args = AList.group (op =) (map check eqs)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   277
    val _ = grouped_args
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   278
      |> map (fn (fname, ars) =>
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   279
        length (distinct (op =) ars) = 1
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   280
        orelse error ("Function " ^ quote fname ^
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   281
          " has different numbers of arguments in different equations"))
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   282
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   283
    val not_defined = subtract (op =) (map fst grouped_args) fnames
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   284
    val _ = null not_defined
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   285
      orelse error ("No defining equations for function" ^
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   286
        plural " " "s " not_defined ^ commas_quote not_defined)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   287
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   288
    fun check_sorts ((fname, fT), _) =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   289
      Sorts.of_sort (Sign.classes_of (Proof_Context.theory_of ctxt)) (fT, HOLogic.typeS)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   290
      orelse error (cat_lines
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   291
      ["Type of " ^ quote fname ^ " is not of sort " ^ quote "type" ^ ":",
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   292
       Syntax.string_of_typ (Config.put show_sorts true ctxt) fT])
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   293
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   294
    val _ = map check_sorts fixes
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   295
  in
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   296
    ()
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   297
  end
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   298
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   299
(* Preprocessors *)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   300
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   301
type fixes = ((string * typ) * mixfix) list
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   302
type 'a spec = (Attrib.binding * 'a list) list
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   303
type preproc = nominal_function_config -> Proof.context -> fixes -> term spec ->
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
  (term list * (thm list -> thm spec) * (thm list -> thm list list) * string list)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   305
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   306
val fname_of = fst o dest_Free o fst o strip_comb o fst o HOLogic.dest_eq o
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   307
  HOLogic.dest_Trueprop o Logic.strip_imp_concl o snd o dest_all_all
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   308
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   309
fun mk_case_names i "" k = mk_case_names i (string_of_int (i + 1)) k
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   310
  | mk_case_names _ n 0 = []
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   311
  | mk_case_names _ n 1 = [n]
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   312
  | mk_case_names _ n k = map (fn i => n ^ "_" ^ string_of_int i) (1 upto k)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   313
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   314
fun empty_preproc check _ ctxt fixes spec =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   315
  let
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   316
    val (bnds, tss) = split_list spec
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   317
    val ts = flat tss
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   318
    val _ = check ctxt fixes ts
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   319
    val fnames = map (fst o fst) fixes
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   320
    val indices = map (fn eq => find_index (curry op = (fname_of eq)) fnames) ts
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   321
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   322
    fun sort xs = partition_list (fn i => fn (j,_) => i = j) 0 (length fnames - 1) 
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   323
      (indices ~~ xs) |> map (map snd)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   324
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   325
    (* using theorem names for case name currently disabled *)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   326
    val cnames = map_index (fn (i, _) => mk_case_names i "" 1) bnds |> flat
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   327
  in
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   328
    (ts, curry op ~~ bnds o Library.unflat tss, sort, cnames)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   329
  end
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   330
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   331
structure Preprocessor = Generic_Data
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   332
(
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   333
  type T = preproc
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   334
  val empty : T = empty_preproc check_defs
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   335
  val extend = I
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   336
  fun merge (a, _) = a
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   337
)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   338
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   339
val get_preproc = Preprocessor.get o Context.Proof
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   340
val set_preproc = Preprocessor.map o K
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   341
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   342
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   343
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   344
local
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   345
  val option_parser = Parse.group "option"
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   346
    ((Parse.reserved "sequential" >> K Sequential)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   347
     || ((Parse.reserved "default" |-- Parse.term) >> Default)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   348
     || (Parse.reserved "domintros" >> K DomIntros)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   349
     || (Parse.reserved "no_partials" >> K No_Partials))
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   350
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   351
  fun config_parser default =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   352
    (Scan.optional (Parse.$$$ "(" |-- Parse.!!! (Parse.list1 option_parser) --| Parse.$$$ ")") [])
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   353
     >> (fn opts => fold apply_opt opts default)
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   354
in
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   355
  fun function_parser default_cfg =
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   356
      config_parser default_cfg -- Parse.fixes -- Parse_Spec.where_alt_specs
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   357
end
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   358
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   359
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   360
end
4bd584ff4fab added an option for an invariant (at the moment only a stub)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   361
end