src/Pure/Isar/specification.ML
author berghofe
Tue Aug 28 18:03:16 2007 +0200 (2007-08-28)
changeset 24452 93b113c5ac33
parent 24219 e558fe311376
child 24464 58d24cbe5fa6
permissions -rw-r--r--
- theorem(_i) now also takes "interactive" flag as argument
- added theorem hook
wenzelm@18620
     1
(*  Title:      Pure/Isar/specification.ML
wenzelm@18620
     2
    ID:         $Id$
wenzelm@18620
     3
    Author:     Makarius
wenzelm@18620
     4
wenzelm@21036
     5
Derived local theory specifications --- with type-inference and
wenzelm@18810
     6
toplevel polymorphism.
wenzelm@18620
     7
*)
wenzelm@18620
     8
wenzelm@18620
     9
signature SPECIFICATION =
wenzelm@18620
    10
sig
wenzelm@20890
    11
  val quiet_mode: bool ref
wenzelm@20890
    12
  val print_consts: local_theory -> (string * typ -> bool) -> (string * typ) list -> unit
wenzelm@18771
    13
  val read_specification: (string * string option * mixfix) list ->
wenzelm@18954
    14
    ((string * Attrib.src list) * string list) list -> local_theory ->
wenzelm@18771
    15
    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
wenzelm@18954
    16
    local_theory
wenzelm@18771
    17
  val cert_specification: (string * typ option * mixfix) list ->
wenzelm@18954
    18
    ((string * Attrib.src list) * term list) list -> local_theory ->
wenzelm@18771
    19
    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
wenzelm@18954
    20
    local_theory
wenzelm@18954
    21
  val axiomatization: (string * string option * mixfix) list ->
wenzelm@18954
    22
    ((bstring * Attrib.src list) * string list) list -> local_theory ->
wenzelm@18954
    23
    (term list * (bstring * thm list) list) * local_theory
wenzelm@18954
    24
  val axiomatization_i: (string * typ option * mixfix) list ->
wenzelm@18954
    25
    ((bstring * Attrib.src list) * term list) list -> local_theory ->
wenzelm@18954
    26
    (term list * (bstring * thm list) list) * local_theory
wenzelm@18954
    27
  val definition:
wenzelm@21705
    28
    (string * string option * mixfix) option * ((string * Attrib.src list) * string) ->
wenzelm@21705
    29
    local_theory -> (term * (bstring * thm)) * local_theory
wenzelm@18954
    30
  val definition_i:
wenzelm@21705
    31
    (string * typ option * mixfix) option * ((string * Attrib.src list) * term) ->
wenzelm@21705
    32
    local_theory -> (term * (bstring * thm)) * local_theory
wenzelm@21705
    33
  val abbreviation: Syntax.mode -> (string * string option * mixfix) option * string ->
wenzelm@21532
    34
    local_theory -> local_theory
wenzelm@21705
    35
  val abbreviation_i: Syntax.mode -> (string * typ option * mixfix) option * term ->
wenzelm@21532
    36
    local_theory -> local_theory
wenzelm@21206
    37
  val notation: Syntax.mode -> (string * mixfix) list -> local_theory -> local_theory
wenzelm@21206
    38
  val notation_i: Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory
wenzelm@20914
    39
  val theorems: string -> ((bstring * Attrib.src list) * (thmref * Attrib.src list) list) list
wenzelm@20914
    40
    -> local_theory -> (bstring * thm list) list * local_theory
wenzelm@20914
    41
  val theorems_i: string -> ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list
wenzelm@20914
    42
    -> local_theory -> (bstring * thm list) list * local_theory
wenzelm@21036
    43
  val theorem: string -> Method.text option -> (thm list list -> local_theory -> local_theory) ->
wenzelm@21036
    44
    string * Attrib.src list -> Element.context Locale.element list -> Element.statement ->
berghofe@24452
    45
    bool -> local_theory -> Proof.state
wenzelm@21036
    46
  val theorem_i: string -> Method.text option -> (thm list list -> local_theory -> local_theory) ->
wenzelm@21036
    47
    string * Attrib.src list -> Element.context_i Locale.element list -> Element.statement_i ->
berghofe@24452
    48
    bool -> local_theory -> Proof.state
berghofe@24452
    49
  val add_theorem_hook: (bool -> Proof.state -> Proof.state) -> Context.generic -> Context.generic
wenzelm@18620
    50
end;
wenzelm@18620
    51
wenzelm@18620
    52
structure Specification: SPECIFICATION =
wenzelm@18620
    53
struct
wenzelm@18620
    54
wenzelm@21230
    55
wenzelm@20890
    56
(* diagnostics *)
wenzelm@20890
    57
wenzelm@20890
    58
val quiet_mode = ref false;
wenzelm@20890
    59
wenzelm@20890
    60
fun print_consts _ _ [] = ()
wenzelm@20890
    61
  | print_consts ctxt pred cs =
wenzelm@20890
    62
      if ! quiet_mode then () else Pretty.writeln (ProofDisplay.pretty_consts ctxt pred cs);
wenzelm@20890
    63
wenzelm@21036
    64
fun present_results ctxt res =
wenzelm@21036
    65
  if ! quiet_mode then () else ProofDisplay.present_results ctxt res;
wenzelm@21036
    66
wenzelm@21036
    67
fun present_results' ctxt kind res = present_results ctxt ((kind, ""), res);
wenzelm@20914
    68
wenzelm@19664
    69
wenzelm@18620
    70
(* prepare specification *)
wenzelm@18620
    71
wenzelm@18828
    72
fun prep_specification prep_vars prep_propp prep_att raw_vars raw_specs ctxt =
wenzelm@18620
    73
  let
wenzelm@18670
    74
    val thy = ProofContext.theory_of ctxt;
wenzelm@18620
    75
wenzelm@18670
    76
    val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
wenzelm@18670
    77
    val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
wenzelm@18670
    78
    val ((specs, vs), specs_ctxt) =
wenzelm@19585
    79
      prep_propp (params_ctxt, map (map (rpair []) o snd) raw_specs)
wenzelm@18620
    80
      |> swap |>> map (map fst)
wenzelm@18771
    81
      ||>> fold_map ProofContext.inferred_param xs;
wenzelm@18620
    82
wenzelm@18771
    83
    val params = vs ~~ map #3 vars;
wenzelm@18620
    84
    val names = map (fst o fst) raw_specs;
wenzelm@18670
    85
    val atts = map (map (prep_att thy) o snd o fst) raw_specs;
wenzelm@18620
    86
  in ((params, (names ~~ atts) ~~ specs), specs_ctxt) end;
wenzelm@18620
    87
wenzelm@18620
    88
fun read_specification x =
wenzelm@18771
    89
  prep_specification ProofContext.read_vars ProofContext.read_propp Attrib.intern_src x;
wenzelm@21370
    90
wenzelm@18620
    91
fun cert_specification x =
wenzelm@18670
    92
  prep_specification ProofContext.cert_vars ProofContext.cert_propp (K I) x;
wenzelm@18620
    93
wenzelm@18620
    94
wenzelm@18771
    95
(* axiomatization *)
wenzelm@18620
    96
wenzelm@20890
    97
fun gen_axioms prep raw_vars raw_specs lthy =
wenzelm@18620
    98
  let
wenzelm@20890
    99
    val (vars, specs) = fst (prep raw_vars raw_specs lthy);
wenzelm@18828
   100
    val cs = map fst vars;
wenzelm@18880
   101
    val spec_frees = member (op =) (fold (fold Term.add_frees o snd) specs []);
wenzelm@18786
   102
wenzelm@20890
   103
    val ((consts, axioms), lthy') = lthy
wenzelm@20890
   104
      |> LocalTheory.consts spec_frees vars
wenzelm@21370
   105
      ||> fold (fold Variable.auto_fixes o snd) specs   (* FIXME !? *)
wenzelm@21435
   106
      ||>> LocalTheory.axioms Thm.axiomK specs;
wenzelm@18786
   107
wenzelm@20890
   108
    (* FIXME generic target!? *)
wenzelm@20890
   109
    val hs = map (Term.head_of o #2 o Logic.dest_equals o Thm.prop_of o #2) consts;
wenzelm@20890
   110
    val lthy'' = lthy' |> LocalTheory.theory (Theory.add_finals_i false hs);
wenzelm@20890
   111
wenzelm@20890
   112
    val _ = print_consts lthy' spec_frees cs;
wenzelm@20890
   113
  in ((map #1 consts, axioms), lthy'') end;
wenzelm@18786
   114
wenzelm@18954
   115
val axiomatization = gen_axioms read_specification;
wenzelm@18954
   116
val axiomatization_i = gen_axioms cert_specification;
wenzelm@18620
   117
wenzelm@18786
   118
wenzelm@18786
   119
(* definition *)
wenzelm@18786
   120
wenzelm@21705
   121
fun gen_def prep (raw_var, (raw_a, raw_prop)) lthy =
wenzelm@18786
   122
  let
wenzelm@21705
   123
    val (vars, [((raw_name, atts), [prop])]) =
wenzelm@21705
   124
      fst (prep (the_list raw_var) [(raw_a, [raw_prop])] lthy);
wenzelm@21705
   125
    val (((x, T), rhs), prove) = LocalDefs.derived_def lthy true prop;
wenzelm@21705
   126
    val name = Thm.def_name_optional x raw_name;
wenzelm@21705
   127
    val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
wenzelm@21705
   128
      if x = x' then mx
wenzelm@21705
   129
      else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x'));
wenzelm@21705
   130
    val ((lhs, (_, th)), lthy2) = lthy
berghofe@22923
   131
      |> LocalTheory.def Thm.definitionK ((x, mx), ((name ^ "_raw", []), rhs));
wenzelm@21705
   132
    val ((b, [th']), lthy3) = lthy2
haftmann@22744
   133
      |> LocalTheory.note Thm.definitionK
haftmann@24219
   134
          ((name, Code.add_func_attr false :: atts), [prove lthy2 th]);
wenzelm@18786
   135
wenzelm@21705
   136
    val lhs' = Morphism.term (LocalTheory.target_morphism lthy3) lhs;
wenzelm@21705
   137
    val _ = print_consts lthy3 (member (op =) (Term.add_frees lhs' [])) [(x, T)];
wenzelm@21705
   138
  in ((lhs, (b, th')), lthy3) end;
wenzelm@18786
   139
wenzelm@21705
   140
val definition = gen_def read_specification;
wenzelm@21705
   141
val definition_i = gen_def cert_specification;
wenzelm@18786
   142
wenzelm@19080
   143
wenzelm@19080
   144
(* abbreviation *)
wenzelm@19080
   145
wenzelm@21705
   146
fun gen_abbrev prep mode (raw_var, raw_prop) lthy =
wenzelm@19080
   147
  let
wenzelm@21705
   148
    val ((vars, [(_, [prop])]), _) =
wenzelm@21705
   149
      prep (the_list raw_var) [(("", []), [raw_prop])]
wenzelm@21729
   150
        (lthy |> ProofContext.set_expand_abbrevs false);
wenzelm@21705
   151
    val ((x, T), rhs) = LocalDefs.abs_def (#2 (LocalDefs.cert_def lthy prop));
wenzelm@21705
   152
    val mx = (case vars of [] => NoSyn | [((y, _), mx)] =>
wenzelm@21705
   153
      if x = y then mx
wenzelm@21705
   154
      else error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote y));
wenzelm@21705
   155
    val lthy' = lthy
wenzelm@21705
   156
      |> ProofContext.set_syntax_mode mode    (* FIXME !? *)
wenzelm@21793
   157
      |> LocalTheory.abbrev mode ((x, mx), rhs) |> snd
wenzelm@21705
   158
      |> ProofContext.restore_syntax_mode lthy;
wenzelm@21705
   159
    val _ = print_consts lthy' (K false) [(x, T)]
wenzelm@21532
   160
  in lthy' end;
wenzelm@19080
   161
wenzelm@21705
   162
val abbreviation = gen_abbrev read_specification;
wenzelm@21705
   163
val abbreviation_i = gen_abbrev cert_specification;
wenzelm@19080
   164
wenzelm@19664
   165
wenzelm@21230
   166
(* notation *)
wenzelm@19664
   167
wenzelm@21206
   168
fun gen_notation prep_const mode args lthy =
wenzelm@21746
   169
  lthy |> LocalTheory.notation mode (map (apfst (prep_const lthy)) args);
wenzelm@19664
   170
wenzelm@21206
   171
val notation = gen_notation ProofContext.read_const;
wenzelm@21206
   172
val notation_i = gen_notation (K I);
wenzelm@19664
   173
wenzelm@20914
   174
wenzelm@21036
   175
(* fact statements *)
wenzelm@20914
   176
wenzelm@20914
   177
fun gen_theorems prep_thms prep_att kind raw_facts lthy =
wenzelm@20914
   178
  let
wenzelm@20914
   179
    val attrib = prep_att (ProofContext.theory_of lthy);
wenzelm@20914
   180
    val facts = raw_facts |> map (fn ((name, atts), bs) =>
wenzelm@20914
   181
      ((name, map attrib atts),
wenzelm@21435
   182
        bs |> map (fn (b, more_atts) => (prep_thms lthy b, map attrib more_atts))));
wenzelm@21435
   183
    val (res, lthy') = lthy |> LocalTheory.notes kind facts;
wenzelm@21036
   184
    val _ = present_results' lthy' kind res;
wenzelm@20914
   185
  in (res, lthy') end;
wenzelm@20914
   186
wenzelm@20914
   187
val theorems = gen_theorems ProofContext.get_thms Attrib.intern_src;
wenzelm@20914
   188
val theorems_i = gen_theorems (K I) (K I);
wenzelm@20914
   189
wenzelm@21036
   190
wenzelm@21230
   191
(* complex goal statements *)
wenzelm@21036
   192
wenzelm@21036
   193
local
wenzelm@21036
   194
wenzelm@21450
   195
fun subtract_prems ctxt1 ctxt2 =
wenzelm@21450
   196
  Library.drop (length (Assumption.prems_of ctxt1), Assumption.prems_of ctxt2);
wenzelm@21450
   197
wenzelm@21236
   198
fun prep_statement prep_att prep_stmt elems concl ctxt =
wenzelm@21236
   199
  (case concl of
wenzelm@21236
   200
    Element.Shows shows =>
wenzelm@21230
   201
      let
wenzelm@21450
   202
        val (_, loc_ctxt, elems_ctxt, propp) = prep_stmt elems (map snd shows) ctxt;
wenzelm@21450
   203
        val prems = subtract_prems loc_ctxt elems_ctxt;
wenzelm@21450
   204
        val stmt = Attrib.map_specs prep_att (map fst shows ~~ propp);
wenzelm@21370
   205
        val goal_ctxt = fold (fold (Variable.auto_fixes o fst)) propp elems_ctxt;
wenzelm@21450
   206
      in ((prems, stmt, []), goal_ctxt) end
wenzelm@21236
   207
  | Element.Obtains obtains =>
wenzelm@21230
   208
      let
wenzelm@21236
   209
        val case_names = obtains |> map_index
wenzelm@21236
   210
          (fn (i, ("", _)) => string_of_int (i + 1) | (_, (name, _)) => name);
wenzelm@21236
   211
        val constraints = obtains |> map (fn (_, (vars, _)) =>
wenzelm@21236
   212
          Locale.Elem (Element.Constrains
wenzelm@21236
   213
            (vars |> map_filter (fn (x, SOME T) => SOME (x, T) | _ => NONE))));
wenzelm@21036
   214
wenzelm@21236
   215
        val raw_propp = obtains |> map (fn (_, (_, props)) => map (rpair []) props);
wenzelm@21450
   216
        val (_, loc_ctxt, elems_ctxt, propp) = prep_stmt (elems @ constraints) raw_propp ctxt;
wenzelm@21236
   217
wenzelm@21236
   218
        val thesis = ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) AutoBind.thesisN;
wenzelm@21036
   219
wenzelm@21236
   220
        fun assume_case ((name, (vars, _)), asms) ctxt' =
wenzelm@21236
   221
          let
wenzelm@21236
   222
            val xs = map fst vars;
wenzelm@21236
   223
            val props = map fst asms;
wenzelm@21236
   224
            val (parms, _) = ctxt'
wenzelm@21236
   225
              |> fold Variable.declare_term props
wenzelm@21236
   226
              |> fold_map ProofContext.inferred_param xs;
wenzelm@21236
   227
            val asm = Term.list_all_free (parms, Logic.list_implies (props, thesis));
wenzelm@21236
   228
          in
wenzelm@21236
   229
            ctxt' |> (snd o ProofContext.add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs));
wenzelm@21370
   230
            ctxt' |> Variable.auto_fixes asm
wenzelm@21236
   231
            |> ProofContext.add_assms_i Assumption.assume_export
wenzelm@21236
   232
              [((name, [ContextRules.intro_query NONE]), [(asm, [])])]
wenzelm@21236
   233
            |>> (fn [(_, [th])] => th)
wenzelm@21236
   234
          end;
wenzelm@21236
   235
wenzelm@21658
   236
        val atts = map (Attrib.internal o K)
wenzelm@21236
   237
          [RuleCases.consumes (~ (length obtains)), RuleCases.case_names case_names];
wenzelm@21450
   238
        val prems = subtract_prems loc_ctxt elems_ctxt;
wenzelm@21236
   239
        val stmt = [(("", atts), [(thesis, [])])];
wenzelm@21236
   240
wenzelm@21236
   241
        val (facts, goal_ctxt) = elems_ctxt
wenzelm@21236
   242
          |> (snd o ProofContext.add_fixes_i [(AutoBind.thesisN, NONE, NoSyn)])
wenzelm@21236
   243
          |> fold_map assume_case (obtains ~~ propp)
wenzelm@21435
   244
          |-> (fn ths => ProofContext.note_thmss_i Thm.assumptionK
wenzelm@21435
   245
                [((Obtain.thatN, []), [(ths, [])])] #> #2 #> pair ths);
wenzelm@21450
   246
      in ((prems, stmt, facts), goal_ctxt) end);
wenzelm@21036
   247
berghofe@24452
   248
structure TheoremHook = GenericDataFun
berghofe@24452
   249
(
berghofe@24452
   250
  type T = ((bool -> Proof.state -> Proof.state) * stamp) list;
berghofe@24452
   251
  val empty = [];
berghofe@24452
   252
  val extend = I;
berghofe@24452
   253
  fun merge _ type_checks : T = Library.merge (eq_snd op =) type_checks;
berghofe@24452
   254
);
berghofe@24452
   255
wenzelm@21036
   256
fun gen_theorem prep_att prep_stmt
berghofe@24452
   257
    kind before_qed after_qed (name, raw_atts) raw_elems raw_concl int lthy0 =
wenzelm@21036
   258
  let
wenzelm@21860
   259
    val _ = LocalTheory.affirm lthy0;
wenzelm@21036
   260
    val thy = ProofContext.theory_of lthy0;
wenzelm@21206
   261
wenzelm@21036
   262
    val (loc, ctxt, lthy) =
wenzelm@21036
   263
      (case (TheoryTarget.peek lthy0, exists (fn Locale.Expr _ => true | _ => false) raw_elems) of
wenzelm@21036
   264
        (SOME loc, true) => (* workaround non-modularity of in/includes *)  (* FIXME *)
wenzelm@21275
   265
          (SOME loc, ProofContext.init thy, LocalTheory.restore lthy0)
wenzelm@21036
   266
      | _ => (NONE, lthy0, lthy0));
wenzelm@21036
   267
wenzelm@21435
   268
    val attrib = prep_att thy;
wenzelm@21435
   269
    val atts = map attrib raw_atts;
wenzelm@21435
   270
wenzelm@21532
   271
    val elems = raw_elems |> map (Locale.map_elem (Element.map_ctxt_attrib attrib));
wenzelm@21450
   272
    val ((prems, stmt, facts), goal_ctxt) =
wenzelm@21450
   273
      prep_statement attrib (prep_stmt loc) elems raw_concl ctxt;
wenzelm@21036
   274
wenzelm@21036
   275
    fun after_qed' results goal_ctxt' =
wenzelm@21602
   276
      let val results' =
wenzelm@21602
   277
        burrow (map Goal.norm_result o ProofContext.export goal_ctxt' lthy) results
wenzelm@21602
   278
      in
wenzelm@21036
   279
        lthy
wenzelm@21435
   280
        |> LocalTheory.notes kind (map2 (fn (a, _) => fn ths => (a, [(ths, [])])) stmt results')
wenzelm@21036
   281
        |> (fn (res, lthy') =>
wenzelm@21036
   282
          (present_results lthy' ((kind, name), res);
wenzelm@21435
   283
            if name = "" andalso null atts then lthy'
wenzelm@21435
   284
            else #2 (LocalTheory.notes kind [((name, atts), [(maps #2 res, [])])] lthy')))
wenzelm@21036
   285
        |> after_qed results'
wenzelm@21036
   286
      end;
wenzelm@21036
   287
  in
wenzelm@21036
   288
    goal_ctxt
wenzelm@21450
   289
    |> ProofContext.note_thmss_i Thm.assumptionK [((AutoBind.assmsN, []), [(prems, [])])]
wenzelm@21450
   290
    |> snd
wenzelm@21435
   291
    |> Proof.theorem_i before_qed after_qed' (map snd stmt)
berghofe@24452
   292
    |> Library.apply (map (fn (f, _) => f int)
berghofe@24452
   293
      (TheoremHook.get (Context.Proof goal_ctxt)))
wenzelm@21036
   294
    |> Proof.refine_insert facts
wenzelm@21036
   295
  end;
wenzelm@21036
   296
wenzelm@21230
   297
in
wenzelm@21230
   298
wenzelm@21036
   299
val theorem = gen_theorem Attrib.intern_src Locale.read_context_statement_i;
wenzelm@21036
   300
val theorem_i = gen_theorem (K I) Locale.cert_context_statement;
wenzelm@21036
   301
berghofe@24452
   302
fun add_theorem_hook f = TheoremHook.map (cons (f, stamp ()));
berghofe@24452
   303
wenzelm@18620
   304
end;
wenzelm@21036
   305
wenzelm@21036
   306
end;