src/Pure/Isar/specification.ML
author wenzelm
Tue Nov 21 20:48:11 2006 +0100 (2006-11-21)
changeset 21450 f16c9e6401d1
parent 21435 883337ea2f3b
child 21532 8c162b766cef
permissions -rw-r--r--
theorem(_i): note assms of statement;
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@18786
    28
    ((string * string option * mixfix) option * ((string * Attrib.src list) * string)) list ->
wenzelm@18954
    29
    local_theory -> (term * (bstring * thm)) list * local_theory
wenzelm@18954
    30
  val definition_i:
wenzelm@18786
    31
    ((string * typ option * mixfix) option * ((string * Attrib.src list) * term)) list ->
wenzelm@18954
    32
    local_theory -> (term * (bstring * thm)) list * local_theory
wenzelm@20784
    33
  val abbreviation: Syntax.mode -> ((string * string option * mixfix) option * string) list ->
wenzelm@21275
    34
    local_theory -> (term * term) list * local_theory
wenzelm@20784
    35
  val abbreviation_i: Syntax.mode -> ((string * typ option * mixfix) option * term) list ->
wenzelm@21275
    36
    local_theory -> (term * term) list * 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 ->
wenzelm@21036
    45
    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 ->
wenzelm@21036
    48
    local_theory -> Proof.state
wenzelm@18620
    49
end;
wenzelm@18620
    50
wenzelm@18620
    51
structure Specification: SPECIFICATION =
wenzelm@18620
    52
struct
wenzelm@18620
    53
wenzelm@21230
    54
wenzelm@20890
    55
(* diagnostics *)
wenzelm@20890
    56
wenzelm@20890
    57
val quiet_mode = ref false;
wenzelm@20890
    58
wenzelm@20890
    59
fun print_consts _ _ [] = ()
wenzelm@20890
    60
  | print_consts ctxt pred cs =
wenzelm@20890
    61
      if ! quiet_mode then () else Pretty.writeln (ProofDisplay.pretty_consts ctxt pred cs);
wenzelm@20890
    62
wenzelm@21036
    63
fun present_results ctxt res =
wenzelm@21036
    64
  if ! quiet_mode then () else ProofDisplay.present_results ctxt res;
wenzelm@21036
    65
wenzelm@21036
    66
fun present_results' ctxt kind res = present_results ctxt ((kind, ""), res);
wenzelm@20914
    67
wenzelm@19664
    68
wenzelm@18620
    69
(* prepare specification *)
wenzelm@18620
    70
wenzelm@18828
    71
fun prep_specification prep_vars prep_propp prep_att raw_vars raw_specs ctxt =
wenzelm@18620
    72
  let
wenzelm@18670
    73
    val thy = ProofContext.theory_of ctxt;
wenzelm@18620
    74
wenzelm@18670
    75
    val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
wenzelm@18670
    76
    val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
wenzelm@18670
    77
    val ((specs, vs), specs_ctxt) =
wenzelm@19585
    78
      prep_propp (params_ctxt, map (map (rpair []) o snd) raw_specs)
wenzelm@18620
    79
      |> swap |>> map (map fst)
wenzelm@18771
    80
      ||>> fold_map ProofContext.inferred_param xs;
wenzelm@18620
    81
wenzelm@18771
    82
    val params = vs ~~ map #3 vars;
wenzelm@18620
    83
    val names = map (fst o fst) raw_specs;
wenzelm@18670
    84
    val atts = map (map (prep_att thy) o snd o fst) raw_specs;
wenzelm@18620
    85
  in ((params, (names ~~ atts) ~~ specs), specs_ctxt) end;
wenzelm@18620
    86
wenzelm@18620
    87
fun read_specification x =
wenzelm@18771
    88
  prep_specification ProofContext.read_vars ProofContext.read_propp Attrib.intern_src x;
wenzelm@21370
    89
wenzelm@18620
    90
fun cert_specification x =
wenzelm@18670
    91
  prep_specification ProofContext.cert_vars ProofContext.cert_propp (K I) x;
wenzelm@18620
    92
wenzelm@18620
    93
wenzelm@18771
    94
(* axiomatization *)
wenzelm@18620
    95
wenzelm@20890
    96
fun gen_axioms prep raw_vars raw_specs lthy =
wenzelm@18620
    97
  let
wenzelm@20890
    98
    val (vars, specs) = fst (prep raw_vars raw_specs lthy);
wenzelm@18828
    99
    val cs = map fst vars;
wenzelm@18880
   100
    val spec_frees = member (op =) (fold (fold Term.add_frees o snd) specs []);
wenzelm@18786
   101
wenzelm@20890
   102
    val ((consts, axioms), lthy') = lthy
wenzelm@20890
   103
      |> LocalTheory.consts spec_frees vars
wenzelm@21370
   104
      ||> fold (fold Variable.auto_fixes o snd) specs   (* FIXME !? *)
wenzelm@21435
   105
      ||>> LocalTheory.axioms Thm.axiomK specs;
wenzelm@18786
   106
wenzelm@20890
   107
    (* FIXME generic target!? *)
wenzelm@20890
   108
    val hs = map (Term.head_of o #2 o Logic.dest_equals o Thm.prop_of o #2) consts;
wenzelm@20890
   109
    val lthy'' = lthy' |> LocalTheory.theory (Theory.add_finals_i false hs);
wenzelm@20890
   110
wenzelm@20890
   111
    val _ = print_consts lthy' spec_frees cs;
wenzelm@20890
   112
  in ((map #1 consts, axioms), lthy'') end;
wenzelm@18786
   113
wenzelm@18954
   114
val axiomatization = gen_axioms read_specification;
wenzelm@18954
   115
val axiomatization_i = gen_axioms cert_specification;
wenzelm@18620
   116
wenzelm@18786
   117
wenzelm@18786
   118
(* definition *)
wenzelm@18786
   119
wenzelm@20890
   120
fun gen_defs prep args lthy =
wenzelm@18786
   121
  let
wenzelm@20890
   122
    fun define (raw_var, (raw_a, raw_prop)) lthy1 =
wenzelm@18786
   123
      let
wenzelm@20890
   124
        val (vars, [((raw_name, atts), [prop])]) =
wenzelm@20890
   125
          fst (prep (the_list raw_var) [(raw_a, [raw_prop])] lthy1);
wenzelm@20890
   126
        val (((x, T), rhs), prove) = LocalDefs.derived_def lthy1 true prop;
wenzelm@20890
   127
        val name = Thm.def_name_optional x raw_name;
wenzelm@18786
   128
        val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
wenzelm@18786
   129
          if x = x' then mx
wenzelm@18786
   130
          else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x'));
wenzelm@20890
   131
        val ((lhs, (_, th)), lthy2) = lthy1
wenzelm@20890
   132
(*          |> LocalTheory.def ((x, mx), ((name ^ "_raw", []), rhs));  FIXME *)
wenzelm@21435
   133
          |> LocalTheory.def Thm.definitionK ((x, mx), ((name, []), rhs));
wenzelm@20890
   134
        val ((b, [th']), lthy3) = lthy2
wenzelm@21435
   135
          |> LocalTheory.note Thm.definitionK ((name, atts), [prove lthy2 th]);
wenzelm@21275
   136
      in (((x, T), (lhs, (b, th'))), LocalTheory.restore lthy3) end;
wenzelm@18786
   137
wenzelm@20890
   138
    val ((cs, defs), lthy') = lthy |> fold_map define args |>> split_list;
wenzelm@18880
   139
    val def_frees = member (op =) (fold (Term.add_frees o fst) defs []);
wenzelm@20890
   140
    val _ = print_consts lthy' def_frees cs;
wenzelm@20890
   141
  in (defs, lthy') end;
wenzelm@18786
   142
wenzelm@18954
   143
val definition = gen_defs read_specification;
wenzelm@18954
   144
val definition_i = gen_defs cert_specification;
wenzelm@18786
   145
wenzelm@19080
   146
wenzelm@19080
   147
(* abbreviation *)
wenzelm@19080
   148
wenzelm@20890
   149
fun gen_abbrevs prep mode args lthy =
wenzelm@19080
   150
  let
wenzelm@20890
   151
    fun abbrev (raw_var, raw_prop) lthy1 =
wenzelm@19080
   152
      let
wenzelm@19372
   153
        val ((vars, [(_, [prop])]), _) =
wenzelm@19372
   154
          prep (the_list raw_var) [(("", []), [raw_prop])]
wenzelm@20890
   155
            (lthy1 |> ProofContext.expand_abbrevs false);
wenzelm@20890
   156
        val ((x, T), rhs) = LocalDefs.abs_def (#2 (LocalDefs.cert_def lthy1 prop));
wenzelm@21275
   157
        val mx = (case vars of [] => NoSyn | [((y, _), mx)] =>
wenzelm@21275
   158
          if x = y then mx
wenzelm@21275
   159
          else error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote y));
wenzelm@21275
   160
        val ([abbr], lthy2) = lthy1
wenzelm@21275
   161
          |> LocalTheory.abbrevs mode [((x, mx), rhs)];
wenzelm@21275
   162
      in (((x, T), abbr), LocalTheory.restore lthy2) end;
wenzelm@19080
   163
wenzelm@21275
   164
    val (abbrs, lthy1) = lthy
wenzelm@19544
   165
      |> ProofContext.set_syntax_mode mode
wenzelm@19544
   166
      |> fold_map abbrev args
wenzelm@20890
   167
      ||> ProofContext.restore_syntax_mode lthy;
wenzelm@21275
   168
    val _ = print_consts lthy1 (K false) (map fst abbrs);
wenzelm@21275
   169
  in (map snd abbrs, lthy1) end;
wenzelm@19080
   170
wenzelm@19080
   171
val abbreviation = gen_abbrevs read_specification;
wenzelm@19080
   172
val abbreviation_i = gen_abbrevs cert_specification;
wenzelm@19080
   173
wenzelm@19664
   174
wenzelm@21230
   175
(* notation *)
wenzelm@19664
   176
wenzelm@21206
   177
fun gen_notation prep_const mode args lthy =
wenzelm@21206
   178
  lthy |> LocalTheory.notation mode (map (apfst (prep_const lthy)) args);
wenzelm@19664
   179
wenzelm@21206
   180
val notation = gen_notation ProofContext.read_const;
wenzelm@21206
   181
val notation_i = gen_notation (K I);
wenzelm@19664
   182
wenzelm@20914
   183
wenzelm@21036
   184
(* fact statements *)
wenzelm@20914
   185
wenzelm@20914
   186
fun gen_theorems prep_thms prep_att kind raw_facts lthy =
wenzelm@20914
   187
  let
wenzelm@20914
   188
    val attrib = prep_att (ProofContext.theory_of lthy);
wenzelm@20914
   189
    val facts = raw_facts |> map (fn ((name, atts), bs) =>
wenzelm@20914
   190
      ((name, map attrib atts),
wenzelm@21435
   191
        bs |> map (fn (b, more_atts) => (prep_thms lthy b, map attrib more_atts))));
wenzelm@21435
   192
    val (res, lthy') = lthy |> LocalTheory.notes kind facts;
wenzelm@21036
   193
    val _ = present_results' lthy' kind res;
wenzelm@20914
   194
  in (res, lthy') end;
wenzelm@20914
   195
wenzelm@20914
   196
val theorems = gen_theorems ProofContext.get_thms Attrib.intern_src;
wenzelm@20914
   197
val theorems_i = gen_theorems (K I) (K I);
wenzelm@20914
   198
wenzelm@21036
   199
wenzelm@21230
   200
(* complex goal statements *)
wenzelm@21036
   201
wenzelm@21036
   202
local
wenzelm@21036
   203
wenzelm@21450
   204
fun subtract_prems ctxt1 ctxt2 =
wenzelm@21450
   205
  Library.drop (length (Assumption.prems_of ctxt1), Assumption.prems_of ctxt2);
wenzelm@21450
   206
wenzelm@21236
   207
fun prep_statement prep_att prep_stmt elems concl ctxt =
wenzelm@21236
   208
  (case concl of
wenzelm@21236
   209
    Element.Shows shows =>
wenzelm@21230
   210
      let
wenzelm@21450
   211
        val (_, loc_ctxt, elems_ctxt, propp) = prep_stmt elems (map snd shows) ctxt;
wenzelm@21450
   212
        val prems = subtract_prems loc_ctxt elems_ctxt;
wenzelm@21450
   213
        val stmt = Attrib.map_specs prep_att (map fst shows ~~ propp);
wenzelm@21370
   214
        val goal_ctxt = fold (fold (Variable.auto_fixes o fst)) propp elems_ctxt;
wenzelm@21450
   215
      in ((prems, stmt, []), goal_ctxt) end
wenzelm@21236
   216
  | Element.Obtains obtains =>
wenzelm@21230
   217
      let
wenzelm@21236
   218
        val case_names = obtains |> map_index
wenzelm@21236
   219
          (fn (i, ("", _)) => string_of_int (i + 1) | (_, (name, _)) => name);
wenzelm@21236
   220
        val constraints = obtains |> map (fn (_, (vars, _)) =>
wenzelm@21236
   221
          Locale.Elem (Element.Constrains
wenzelm@21236
   222
            (vars |> map_filter (fn (x, SOME T) => SOME (x, T) | _ => NONE))));
wenzelm@21036
   223
wenzelm@21236
   224
        val raw_propp = obtains |> map (fn (_, (_, props)) => map (rpair []) props);
wenzelm@21450
   225
        val (_, loc_ctxt, elems_ctxt, propp) = prep_stmt (elems @ constraints) raw_propp ctxt;
wenzelm@21236
   226
wenzelm@21236
   227
        val thesis = ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) AutoBind.thesisN;
wenzelm@21036
   228
wenzelm@21236
   229
        fun assume_case ((name, (vars, _)), asms) ctxt' =
wenzelm@21236
   230
          let
wenzelm@21236
   231
            val xs = map fst vars;
wenzelm@21236
   232
            val props = map fst asms;
wenzelm@21236
   233
            val (parms, _) = ctxt'
wenzelm@21236
   234
              |> fold Variable.declare_term props
wenzelm@21236
   235
              |> fold_map ProofContext.inferred_param xs;
wenzelm@21236
   236
            val asm = Term.list_all_free (parms, Logic.list_implies (props, thesis));
wenzelm@21236
   237
          in
wenzelm@21236
   238
            ctxt' |> (snd o ProofContext.add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs));
wenzelm@21370
   239
            ctxt' |> Variable.auto_fixes asm
wenzelm@21236
   240
            |> ProofContext.add_assms_i Assumption.assume_export
wenzelm@21236
   241
              [((name, [ContextRules.intro_query NONE]), [(asm, [])])]
wenzelm@21236
   242
            |>> (fn [(_, [th])] => th)
wenzelm@21236
   243
          end;
wenzelm@21236
   244
wenzelm@21236
   245
        val atts = map Attrib.internal
wenzelm@21236
   246
          [RuleCases.consumes (~ (length obtains)), RuleCases.case_names case_names];
wenzelm@21450
   247
        val prems = subtract_prems loc_ctxt elems_ctxt;
wenzelm@21236
   248
        val stmt = [(("", atts), [(thesis, [])])];
wenzelm@21236
   249
wenzelm@21236
   250
        val (facts, goal_ctxt) = elems_ctxt
wenzelm@21236
   251
          |> (snd o ProofContext.add_fixes_i [(AutoBind.thesisN, NONE, NoSyn)])
wenzelm@21236
   252
          |> fold_map assume_case (obtains ~~ propp)
wenzelm@21435
   253
          |-> (fn ths => ProofContext.note_thmss_i Thm.assumptionK
wenzelm@21435
   254
                [((Obtain.thatN, []), [(ths, [])])] #> #2 #> pair ths);
wenzelm@21450
   255
      in ((prems, stmt, facts), goal_ctxt) end);
wenzelm@21036
   256
wenzelm@21036
   257
fun gen_theorem prep_att prep_stmt
wenzelm@21036
   258
    kind before_qed after_qed (name, raw_atts) raw_elems raw_concl lthy0 =
wenzelm@21036
   259
  let
wenzelm@21036
   260
    val _ = LocalTheory.assert lthy0;
wenzelm@21036
   261
    val thy = ProofContext.theory_of lthy0;
wenzelm@21206
   262
wenzelm@21036
   263
    val (loc, ctxt, lthy) =
wenzelm@21036
   264
      (case (TheoryTarget.peek lthy0, exists (fn Locale.Expr _ => true | _ => false) raw_elems) of
wenzelm@21036
   265
        (SOME loc, true) => (* workaround non-modularity of in/includes *)  (* FIXME *)
wenzelm@21275
   266
          (SOME loc, ProofContext.init thy, LocalTheory.restore lthy0)
wenzelm@21036
   267
      | _ => (NONE, lthy0, lthy0));
wenzelm@21036
   268
wenzelm@21435
   269
    val attrib = prep_att thy;
wenzelm@21435
   270
    val atts = map attrib raw_atts;
wenzelm@21435
   271
wenzelm@21236
   272
    val elems = raw_elems |> (map o Locale.map_elem)
wenzelm@21036
   273
      (Element.map_ctxt {name = I, var = I, typ = I, term = I, fact = I, attrib = attrib});
wenzelm@21450
   274
    val ((prems, stmt, facts), goal_ctxt) =
wenzelm@21450
   275
      prep_statement attrib (prep_stmt loc) elems raw_concl ctxt;
wenzelm@21036
   276
wenzelm@21036
   277
    fun after_qed' results goal_ctxt' =
wenzelm@21036
   278
      let val results' = burrow (ProofContext.export_standard goal_ctxt' lthy) results 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)
wenzelm@21036
   292
    |> Proof.refine_insert facts
wenzelm@21036
   293
  end;
wenzelm@21036
   294
wenzelm@21230
   295
in
wenzelm@21230
   296
wenzelm@21036
   297
val theorem = gen_theorem Attrib.intern_src Locale.read_context_statement_i;
wenzelm@21036
   298
val theorem_i = gen_theorem (K I) Locale.cert_context_statement;
wenzelm@21036
   299
wenzelm@18620
   300
end;
wenzelm@21036
   301
wenzelm@21036
   302
end;