src/Pure/Isar/specification.ML
author wenzelm
Sun Nov 26 18:07:29 2006 +0100 (2006-11-26)
changeset 21532 8c162b766cef
parent 21450 f16c9e6401d1
child 21602 cb13024d0e36
permissions -rw-r--r--
abbrevs: no result;
Element.map_ctxt_attrib;
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@21532
    34
    local_theory -> local_theory
wenzelm@20784
    35
  val abbreviation_i: Syntax.mode -> ((string * typ option * mixfix) option * term) list ->
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 ->
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@21532
   160
        val lthy2 = lthy1 |> LocalTheory.abbrevs mode [((x, mx), rhs)];
wenzelm@21532
   161
      in ((x, T), LocalTheory.restore lthy2) end;
wenzelm@19080
   162
wenzelm@21532
   163
    val (cs, lthy') = lthy
wenzelm@19544
   164
      |> ProofContext.set_syntax_mode mode
wenzelm@19544
   165
      |> fold_map abbrev args
wenzelm@20890
   166
      ||> ProofContext.restore_syntax_mode lthy;
wenzelm@21532
   167
    val _ = print_consts lthy' (K false) cs;
wenzelm@21532
   168
  in lthy' end;
wenzelm@19080
   169
wenzelm@19080
   170
val abbreviation = gen_abbrevs read_specification;
wenzelm@19080
   171
val abbreviation_i = gen_abbrevs cert_specification;
wenzelm@19080
   172
wenzelm@19664
   173
wenzelm@21230
   174
(* notation *)
wenzelm@19664
   175
wenzelm@21206
   176
fun gen_notation prep_const mode args lthy =
wenzelm@21206
   177
  lthy |> LocalTheory.notation mode (map (apfst (prep_const lthy)) args);
wenzelm@19664
   178
wenzelm@21206
   179
val notation = gen_notation ProofContext.read_const;
wenzelm@21206
   180
val notation_i = gen_notation (K I);
wenzelm@19664
   181
wenzelm@20914
   182
wenzelm@21036
   183
(* fact statements *)
wenzelm@20914
   184
wenzelm@20914
   185
fun gen_theorems prep_thms prep_att kind raw_facts lthy =
wenzelm@20914
   186
  let
wenzelm@20914
   187
    val attrib = prep_att (ProofContext.theory_of lthy);
wenzelm@20914
   188
    val facts = raw_facts |> map (fn ((name, atts), bs) =>
wenzelm@20914
   189
      ((name, map attrib atts),
wenzelm@21435
   190
        bs |> map (fn (b, more_atts) => (prep_thms lthy b, map attrib more_atts))));
wenzelm@21435
   191
    val (res, lthy') = lthy |> LocalTheory.notes kind facts;
wenzelm@21036
   192
    val _ = present_results' lthy' kind res;
wenzelm@20914
   193
  in (res, lthy') end;
wenzelm@20914
   194
wenzelm@20914
   195
val theorems = gen_theorems ProofContext.get_thms Attrib.intern_src;
wenzelm@20914
   196
val theorems_i = gen_theorems (K I) (K I);
wenzelm@20914
   197
wenzelm@21036
   198
wenzelm@21230
   199
(* complex goal statements *)
wenzelm@21036
   200
wenzelm@21036
   201
local
wenzelm@21036
   202
wenzelm@21450
   203
fun subtract_prems ctxt1 ctxt2 =
wenzelm@21450
   204
  Library.drop (length (Assumption.prems_of ctxt1), Assumption.prems_of ctxt2);
wenzelm@21450
   205
wenzelm@21236
   206
fun prep_statement prep_att prep_stmt elems concl ctxt =
wenzelm@21236
   207
  (case concl of
wenzelm@21236
   208
    Element.Shows shows =>
wenzelm@21230
   209
      let
wenzelm@21450
   210
        val (_, loc_ctxt, elems_ctxt, propp) = prep_stmt elems (map snd shows) ctxt;
wenzelm@21450
   211
        val prems = subtract_prems loc_ctxt elems_ctxt;
wenzelm@21450
   212
        val stmt = Attrib.map_specs prep_att (map fst shows ~~ propp);
wenzelm@21370
   213
        val goal_ctxt = fold (fold (Variable.auto_fixes o fst)) propp elems_ctxt;
wenzelm@21450
   214
      in ((prems, stmt, []), goal_ctxt) end
wenzelm@21236
   215
  | Element.Obtains obtains =>
wenzelm@21230
   216
      let
wenzelm@21236
   217
        val case_names = obtains |> map_index
wenzelm@21236
   218
          (fn (i, ("", _)) => string_of_int (i + 1) | (_, (name, _)) => name);
wenzelm@21236
   219
        val constraints = obtains |> map (fn (_, (vars, _)) =>
wenzelm@21236
   220
          Locale.Elem (Element.Constrains
wenzelm@21236
   221
            (vars |> map_filter (fn (x, SOME T) => SOME (x, T) | _ => NONE))));
wenzelm@21036
   222
wenzelm@21236
   223
        val raw_propp = obtains |> map (fn (_, (_, props)) => map (rpair []) props);
wenzelm@21450
   224
        val (_, loc_ctxt, elems_ctxt, propp) = prep_stmt (elems @ constraints) raw_propp ctxt;
wenzelm@21236
   225
wenzelm@21236
   226
        val thesis = ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) AutoBind.thesisN;
wenzelm@21036
   227
wenzelm@21236
   228
        fun assume_case ((name, (vars, _)), asms) ctxt' =
wenzelm@21236
   229
          let
wenzelm@21236
   230
            val xs = map fst vars;
wenzelm@21236
   231
            val props = map fst asms;
wenzelm@21236
   232
            val (parms, _) = ctxt'
wenzelm@21236
   233
              |> fold Variable.declare_term props
wenzelm@21236
   234
              |> fold_map ProofContext.inferred_param xs;
wenzelm@21236
   235
            val asm = Term.list_all_free (parms, Logic.list_implies (props, thesis));
wenzelm@21236
   236
          in
wenzelm@21236
   237
            ctxt' |> (snd o ProofContext.add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs));
wenzelm@21370
   238
            ctxt' |> Variable.auto_fixes asm
wenzelm@21236
   239
            |> ProofContext.add_assms_i Assumption.assume_export
wenzelm@21236
   240
              [((name, [ContextRules.intro_query NONE]), [(asm, [])])]
wenzelm@21236
   241
            |>> (fn [(_, [th])] => th)
wenzelm@21236
   242
          end;
wenzelm@21236
   243
wenzelm@21236
   244
        val atts = map Attrib.internal
wenzelm@21236
   245
          [RuleCases.consumes (~ (length obtains)), RuleCases.case_names case_names];
wenzelm@21450
   246
        val prems = subtract_prems loc_ctxt elems_ctxt;
wenzelm@21236
   247
        val stmt = [(("", atts), [(thesis, [])])];
wenzelm@21236
   248
wenzelm@21236
   249
        val (facts, goal_ctxt) = elems_ctxt
wenzelm@21236
   250
          |> (snd o ProofContext.add_fixes_i [(AutoBind.thesisN, NONE, NoSyn)])
wenzelm@21236
   251
          |> fold_map assume_case (obtains ~~ propp)
wenzelm@21435
   252
          |-> (fn ths => ProofContext.note_thmss_i Thm.assumptionK
wenzelm@21435
   253
                [((Obtain.thatN, []), [(ths, [])])] #> #2 #> pair ths);
wenzelm@21450
   254
      in ((prems, stmt, facts), goal_ctxt) end);
wenzelm@21036
   255
wenzelm@21036
   256
fun gen_theorem prep_att prep_stmt
wenzelm@21036
   257
    kind before_qed after_qed (name, raw_atts) raw_elems raw_concl lthy0 =
wenzelm@21036
   258
  let
wenzelm@21036
   259
    val _ = LocalTheory.assert 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@21036
   276
      let val results' = burrow (ProofContext.export_standard goal_ctxt' lthy) results in
wenzelm@21036
   277
        lthy
wenzelm@21435
   278
        |> LocalTheory.notes kind (map2 (fn (a, _) => fn ths => (a, [(ths, [])])) stmt results')
wenzelm@21036
   279
        |> (fn (res, lthy') =>
wenzelm@21036
   280
          (present_results lthy' ((kind, name), res);
wenzelm@21435
   281
            if name = "" andalso null atts then lthy'
wenzelm@21435
   282
            else #2 (LocalTheory.notes kind [((name, atts), [(maps #2 res, [])])] lthy')))
wenzelm@21036
   283
        |> after_qed results'
wenzelm@21036
   284
      end;
wenzelm@21036
   285
  in
wenzelm@21036
   286
    goal_ctxt
wenzelm@21450
   287
    |> ProofContext.note_thmss_i Thm.assumptionK [((AutoBind.assmsN, []), [(prems, [])])]
wenzelm@21450
   288
    |> snd
wenzelm@21435
   289
    |> Proof.theorem_i before_qed after_qed' (map snd stmt)
wenzelm@21036
   290
    |> Proof.refine_insert facts
wenzelm@21036
   291
  end;
wenzelm@21036
   292
wenzelm@21230
   293
in
wenzelm@21230
   294
wenzelm@21036
   295
val theorem = gen_theorem Attrib.intern_src Locale.read_context_statement_i;
wenzelm@21036
   296
val theorem_i = gen_theorem (K I) Locale.cert_context_statement;
wenzelm@21036
   297
wenzelm@18620
   298
end;
wenzelm@21036
   299
wenzelm@21036
   300
end;