src/Pure/Isar/specification.ML
author wenzelm
Tue Nov 07 19:39:52 2006 +0100 (2006-11-07)
changeset 21230 abfdce60b371
parent 21206 2af4c7b3f7ef
child 21236 890fafbcf8b0
permissions -rw-r--r--
theorem statements: incorporate Obtain.statement, tuned;
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@19080
    34
    local_theory -> local_theory
wenzelm@20784
    35
  val abbreviation_i: Syntax.mode -> ((string * typ option * mixfix) option * term) list ->
wenzelm@19080
    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@18620
    89
fun cert_specification x =
wenzelm@18670
    90
  prep_specification ProofContext.cert_vars ProofContext.cert_propp (K I) x;
wenzelm@18620
    91
wenzelm@18620
    92
wenzelm@18771
    93
(* axiomatization *)
wenzelm@18620
    94
wenzelm@20890
    95
fun gen_axioms prep raw_vars raw_specs lthy =
wenzelm@18620
    96
  let
wenzelm@20890
    97
    val (vars, specs) = fst (prep raw_vars raw_specs lthy);
wenzelm@18828
    98
    val cs = map fst vars;
wenzelm@18880
    99
    val spec_frees = member (op =) (fold (fold Term.add_frees o snd) specs []);
wenzelm@18786
   100
wenzelm@20890
   101
    val ((consts, axioms), lthy') = lthy
wenzelm@20890
   102
      |> LocalTheory.consts spec_frees vars
wenzelm@20914
   103
      ||> fold (fold Variable.fix_frees o snd) specs   (* FIXME !? *)
wenzelm@20890
   104
      ||>> LocalTheory.axioms specs;
wenzelm@18786
   105
wenzelm@20890
   106
    (* FIXME generic target!? *)
wenzelm@20890
   107
    val hs = map (Term.head_of o #2 o Logic.dest_equals o Thm.prop_of o #2) consts;
wenzelm@20890
   108
    val lthy'' = lthy' |> LocalTheory.theory (Theory.add_finals_i false hs);
wenzelm@20890
   109
wenzelm@20890
   110
    val _ = print_consts lthy' spec_frees cs;
wenzelm@20890
   111
  in ((map #1 consts, axioms), lthy'') end;
wenzelm@18786
   112
wenzelm@18954
   113
val axiomatization = gen_axioms read_specification;
wenzelm@18954
   114
val axiomatization_i = gen_axioms cert_specification;
wenzelm@18620
   115
wenzelm@18786
   116
wenzelm@18786
   117
(* definition *)
wenzelm@18786
   118
wenzelm@20890
   119
fun gen_defs prep args lthy =
wenzelm@18786
   120
  let
wenzelm@20890
   121
    fun define (raw_var, (raw_a, raw_prop)) lthy1 =
wenzelm@18786
   122
      let
wenzelm@20890
   123
        val (vars, [((raw_name, atts), [prop])]) =
wenzelm@20890
   124
          fst (prep (the_list raw_var) [(raw_a, [raw_prop])] lthy1);
wenzelm@20890
   125
        val (((x, T), rhs), prove) = LocalDefs.derived_def lthy1 true prop;
wenzelm@20890
   126
        val name = Thm.def_name_optional x raw_name;
wenzelm@18786
   127
        val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
wenzelm@18786
   128
          if x = x' then mx
wenzelm@18786
   129
          else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x'));
wenzelm@20890
   130
        val ((lhs, (_, th)), lthy2) = lthy1
wenzelm@20890
   131
(*          |> LocalTheory.def ((x, mx), ((name ^ "_raw", []), rhs));  FIXME *)
wenzelm@20890
   132
          |> LocalTheory.def ((x, mx), ((name, []), rhs));
wenzelm@20890
   133
        val ((b, [th']), lthy3) = lthy2
wenzelm@20914
   134
          |> LocalTheory.note ((name, atts), [prove lthy2 th]);
wenzelm@20890
   135
      in (((x, T), (lhs, (b, th'))), LocalTheory.reinit lthy3) end;
wenzelm@18786
   136
wenzelm@20890
   137
    val ((cs, defs), lthy') = lthy |> fold_map define args |>> split_list;
wenzelm@18880
   138
    val def_frees = member (op =) (fold (Term.add_frees o fst) defs []);
wenzelm@20890
   139
    val _ = print_consts lthy' def_frees cs;
wenzelm@20890
   140
  in (defs, lthy') end;
wenzelm@18786
   141
wenzelm@18954
   142
val definition = gen_defs read_specification;
wenzelm@18954
   143
val definition_i = gen_defs cert_specification;
wenzelm@18786
   144
wenzelm@19080
   145
wenzelm@19080
   146
(* abbreviation *)
wenzelm@19080
   147
wenzelm@20890
   148
fun gen_abbrevs prep mode args lthy =
wenzelm@19080
   149
  let
wenzelm@20890
   150
    fun abbrev (raw_var, raw_prop) lthy1 =
wenzelm@19080
   151
      let
wenzelm@19372
   152
        val ((vars, [(_, [prop])]), _) =
wenzelm@19372
   153
          prep (the_list raw_var) [(("", []), [raw_prop])]
wenzelm@20890
   154
            (lthy1 |> ProofContext.expand_abbrevs false);
wenzelm@20890
   155
        val ((x, T), rhs) = LocalDefs.abs_def (#2 (LocalDefs.cert_def lthy1 prop));
wenzelm@19080
   156
        val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
wenzelm@19080
   157
          if x = x' then mx
wenzelm@19080
   158
          else error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote x'));
wenzelm@19080
   159
      in
wenzelm@20890
   160
        lthy1
wenzelm@19664
   161
        |> LocalTheory.abbrevs mode [((x, mx), rhs)]
wenzelm@19080
   162
        |> pair (x, T)
wenzelm@19080
   163
      end;
wenzelm@19080
   164
wenzelm@20890
   165
    val (cs, lthy1) = lthy
wenzelm@19544
   166
      |> ProofContext.set_syntax_mode mode
wenzelm@19544
   167
      |> fold_map abbrev args
wenzelm@20890
   168
      ||> ProofContext.restore_syntax_mode lthy;
wenzelm@20890
   169
    val _ = print_consts lthy1 (K false) cs;
wenzelm@20890
   170
  in lthy1 end;
wenzelm@19080
   171
wenzelm@19080
   172
val abbreviation = gen_abbrevs read_specification;
wenzelm@19080
   173
val abbreviation_i = gen_abbrevs cert_specification;
wenzelm@19080
   174
wenzelm@19664
   175
wenzelm@21230
   176
(* notation *)
wenzelm@19664
   177
wenzelm@21206
   178
fun gen_notation prep_const mode args lthy =
wenzelm@21206
   179
  lthy |> LocalTheory.notation mode (map (apfst (prep_const lthy)) args);
wenzelm@19664
   180
wenzelm@21206
   181
val notation = gen_notation ProofContext.read_const;
wenzelm@21206
   182
val notation_i = gen_notation (K I);
wenzelm@19664
   183
wenzelm@20914
   184
wenzelm@21036
   185
(* fact statements *)
wenzelm@20914
   186
wenzelm@20914
   187
fun gen_theorems prep_thms prep_att kind raw_facts lthy =
wenzelm@20914
   188
  let
wenzelm@20914
   189
    val k = if kind = "" then [] else [Attrib.kind kind];
wenzelm@20914
   190
    val attrib = prep_att (ProofContext.theory_of lthy);
wenzelm@20914
   191
    val facts = raw_facts |> map (fn ((name, atts), bs) =>
wenzelm@20914
   192
      ((name, map attrib atts),
wenzelm@20914
   193
        bs |> map (fn (b, more_atts) => (prep_thms lthy b, map attrib more_atts @ k))));
wenzelm@20914
   194
    val (res, lthy') = lthy |> LocalTheory.notes facts;
wenzelm@21036
   195
    val _ = present_results' lthy' kind res;
wenzelm@20914
   196
  in (res, lthy') end;
wenzelm@20914
   197
wenzelm@20914
   198
val theorems = gen_theorems ProofContext.get_thms Attrib.intern_src;
wenzelm@20914
   199
val theorems_i = gen_theorems (K I) (K I);
wenzelm@20914
   200
wenzelm@21036
   201
wenzelm@21230
   202
(* complex goal statements *)
wenzelm@21036
   203
wenzelm@21036
   204
local
wenzelm@21036
   205
wenzelm@21230
   206
fun conclusion prep_att (Element.Shows concl) =
wenzelm@21230
   207
      let
wenzelm@21230
   208
        fun mk_stmt propp ctxt =
wenzelm@21230
   209
          ((Attrib.map_specs prep_att (map fst concl ~~ propp), []),
wenzelm@21230
   210
            fold (fold (Variable.fix_frees o fst)) propp ctxt);
wenzelm@21230
   211
      in (([], map snd concl), mk_stmt) end
wenzelm@21230
   212
  | conclusion _ (Element.Obtains cases) =
wenzelm@21230
   213
      let
wenzelm@21230
   214
        val names =
wenzelm@21230
   215
          cases |> map_index (fn (i, ("", _)) => string_of_int (i + 1) | (_, (name, _)) => name);
wenzelm@21230
   216
        val elems = cases |> map (fn (_, (vars, _)) =>
wenzelm@21230
   217
          Element.Constrains (vars |> map_filter (fn (x, SOME T) => SOME (x, T) | _ => NONE)));
wenzelm@21230
   218
        val concl = cases |> map (fn (_, (_, props)) => map (rpair []) props);
wenzelm@21036
   219
wenzelm@21230
   220
        fun mk_stmt propp ctxt =
wenzelm@21230
   221
          let
wenzelm@21230
   222
            val thesis =
wenzelm@21230
   223
              ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) AutoBind.thesisN;
wenzelm@21230
   224
            val atts = map Attrib.internal
wenzelm@21230
   225
              [RuleCases.consumes (~ (length cases)), RuleCases.case_names names];
wenzelm@21036
   226
wenzelm@21230
   227
            fun assume_case ((name, (vars, _)), asms) ctxt' =
wenzelm@21230
   228
              let
wenzelm@21230
   229
                val xs = map fst vars;
wenzelm@21230
   230
                val props = map fst asms;
wenzelm@21230
   231
                val (parms, _) = ctxt'
wenzelm@21230
   232
                  |> fold Variable.declare_term props
wenzelm@21230
   233
                  |> fold_map ProofContext.inferred_param xs;
wenzelm@21230
   234
                val asm = Term.list_all_free (parms, Logic.list_implies (props, thesis));
wenzelm@21230
   235
              in
wenzelm@21230
   236
                ctxt' |> (snd o ProofContext.add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs));
wenzelm@21230
   237
                ctxt' |> Variable.fix_frees asm
wenzelm@21230
   238
                |> ProofContext.add_assms_i Assumption.assume_export
wenzelm@21230
   239
                  [((name, [ContextRules.intro_query NONE]), [(asm, [])])]
wenzelm@21230
   240
                |>> (fn [(_, [th])] => th)
wenzelm@21230
   241
              end;
wenzelm@21230
   242
            val (ths, ctxt') = ctxt
wenzelm@21230
   243
              |> (snd o ProofContext.add_fixes_i [(AutoBind.thesisN, NONE, NoSyn)])
wenzelm@21230
   244
              |> fold_map assume_case (cases ~~ propp)
wenzelm@21230
   245
              |-> (fn ths =>
wenzelm@21230
   246
                ProofContext.note_thmss_i [((Obtain.thatN, []), [(ths, [])])] #> #2 #> pair ths);
wenzelm@21230
   247
          in (([(("", atts), [(thesis, [])])], ths), ctxt') end;
wenzelm@21230
   248
      in ((map Locale.Elem elems, concl), mk_stmt) end;
wenzelm@21036
   249
wenzelm@21036
   250
fun gen_theorem prep_att prep_stmt
wenzelm@21036
   251
    kind before_qed after_qed (name, raw_atts) raw_elems raw_concl lthy0 =
wenzelm@21036
   252
  let
wenzelm@21036
   253
    val _ = LocalTheory.assert lthy0;
wenzelm@21036
   254
    val thy = ProofContext.theory_of lthy0;
wenzelm@21206
   255
wenzelm@21036
   256
    val (loc, ctxt, lthy) =
wenzelm@21036
   257
      (case (TheoryTarget.peek lthy0, exists (fn Locale.Expr _ => true | _ => false) raw_elems) of
wenzelm@21036
   258
        (SOME loc, true) => (* workaround non-modularity of in/includes *)  (* FIXME *)
wenzelm@21230
   259
          (SOME loc, ProofContext.init thy, LocalTheory.reinit lthy0)
wenzelm@21036
   260
      | _ => (NONE, lthy0, lthy0));
wenzelm@21036
   261
wenzelm@21036
   262
    val attrib = prep_att thy;
wenzelm@21036
   263
    val elem_attrib = Locale.map_elem
wenzelm@21036
   264
      (Element.map_ctxt {name = I, var = I, typ = I, term = I, fact = I, attrib = attrib});
wenzelm@21036
   265
wenzelm@21036
   266
    val ((concl_elems, concl), mk_stmt) = conclusion attrib raw_concl;
wenzelm@21036
   267
    val elems = map elem_attrib (raw_elems @ concl_elems);
wenzelm@21036
   268
wenzelm@21230
   269
    val (_, _, elems_ctxt, propp) = prep_stmt loc elems concl ctxt;
wenzelm@21230
   270
    val ((stmt, facts), goal_ctxt) = mk_stmt propp elems_ctxt;
wenzelm@21036
   271
wenzelm@21036
   272
    val k = if kind = "" then [] else [Attrib.kind kind];
wenzelm@21230
   273
    val names = map (fst o fst) stmt;
wenzelm@21230
   274
    val attss = map (fn ((_, atts), _) => map attrib atts @ k) stmt;
wenzelm@21036
   275
    val atts = map attrib raw_atts @ k;
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@21036
   280
        |> LocalTheory.notes ((names ~~ attss) ~~ map (fn ths => [(ths, [])]) results')
wenzelm@21036
   281
        |> (fn (res, lthy') =>
wenzelm@21036
   282
          (present_results lthy' ((kind, name), res);
wenzelm@21036
   283
            if name = "" andalso null raw_atts then lthy'
wenzelm@21036
   284
            else #2 (LocalTheory.notes [((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@21230
   289
    |> Proof.global_goal (K (K ())) Attrib.attribute_i ProofContext.bind_propp_schematic_i
wenzelm@21230
   290
      kind before_qed after_qed' NONE (name, []) stmt
wenzelm@21036
   291
    |> Proof.refine_insert facts
wenzelm@21036
   292
  end;
wenzelm@21036
   293
wenzelm@21230
   294
in
wenzelm@21230
   295
wenzelm@21036
   296
val theorem = gen_theorem Attrib.intern_src Locale.read_context_statement_i;
wenzelm@21036
   297
val theorem_i = gen_theorem (K I) Locale.cert_context_statement;
wenzelm@21036
   298
wenzelm@18620
   299
end;
wenzelm@21036
   300
wenzelm@21036
   301
end;