src/Pure/Isar/specification.ML
author wenzelm
Mon Oct 09 02:20:07 2006 +0200 (2006-10-09)
changeset 20914 3f065aa89792
parent 20890 052bde912a51
child 21036 0eed532acfca
permissions -rw-r--r--
added theorems(_i) -- with present_results;
wenzelm@18620
     1
(*  Title:      Pure/Isar/specification.ML
wenzelm@18620
     2
    ID:         $Id$
wenzelm@18620
     3
    Author:     Makarius
wenzelm@18620
     4
wenzelm@19664
     5
Common 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@20784
    37
  val const_syntax: Syntax.mode -> (xstring * mixfix) list -> local_theory -> local_theory
wenzelm@20784
    38
  val const_syntax_i: Syntax.mode -> (string * 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@18620
    43
end;
wenzelm@18620
    44
wenzelm@18620
    45
structure Specification: SPECIFICATION =
wenzelm@18620
    46
struct
wenzelm@18620
    47
wenzelm@20890
    48
(* diagnostics *)
wenzelm@20890
    49
wenzelm@20890
    50
val quiet_mode = ref false;
wenzelm@20890
    51
wenzelm@20890
    52
fun print_consts _ _ [] = ()
wenzelm@20890
    53
  | print_consts ctxt pred cs =
wenzelm@20890
    54
      if ! quiet_mode then () else Pretty.writeln (ProofDisplay.pretty_consts ctxt pred cs);
wenzelm@20890
    55
wenzelm@20914
    56
fun present_results ctxt kind res =
wenzelm@20914
    57
  if ! quiet_mode then () else ProofDisplay.present_results ctxt ((kind, ""), res);
wenzelm@20914
    58
wenzelm@19664
    59
wenzelm@18620
    60
(* prepare specification *)
wenzelm@18620
    61
wenzelm@18828
    62
fun prep_specification prep_vars prep_propp prep_att raw_vars raw_specs ctxt =
wenzelm@18620
    63
  let
wenzelm@18670
    64
    val thy = ProofContext.theory_of ctxt;
wenzelm@18620
    65
wenzelm@18670
    66
    val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
wenzelm@18670
    67
    val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
wenzelm@18670
    68
    val ((specs, vs), specs_ctxt) =
wenzelm@19585
    69
      prep_propp (params_ctxt, map (map (rpair []) o snd) raw_specs)
wenzelm@18620
    70
      |> swap |>> map (map fst)
wenzelm@18771
    71
      ||>> fold_map ProofContext.inferred_param xs;
wenzelm@18620
    72
wenzelm@18771
    73
    val params = vs ~~ map #3 vars;
wenzelm@18620
    74
    val names = map (fst o fst) raw_specs;
wenzelm@18670
    75
    val atts = map (map (prep_att thy) o snd o fst) raw_specs;
wenzelm@18620
    76
  in ((params, (names ~~ atts) ~~ specs), specs_ctxt) end;
wenzelm@18620
    77
wenzelm@18620
    78
fun read_specification x =
wenzelm@18771
    79
  prep_specification ProofContext.read_vars ProofContext.read_propp Attrib.intern_src x;
wenzelm@18620
    80
fun cert_specification x =
wenzelm@18670
    81
  prep_specification ProofContext.cert_vars ProofContext.cert_propp (K I) x;
wenzelm@18620
    82
wenzelm@18620
    83
wenzelm@18771
    84
(* axiomatization *)
wenzelm@18620
    85
wenzelm@20890
    86
fun gen_axioms prep raw_vars raw_specs lthy =
wenzelm@18620
    87
  let
wenzelm@20890
    88
    val (vars, specs) = fst (prep raw_vars raw_specs lthy);
wenzelm@18828
    89
    val cs = map fst vars;
wenzelm@18880
    90
    val spec_frees = member (op =) (fold (fold Term.add_frees o snd) specs []);
wenzelm@18786
    91
wenzelm@20890
    92
    val ((consts, axioms), lthy') = lthy
wenzelm@20890
    93
      |> LocalTheory.consts spec_frees vars
wenzelm@20914
    94
      ||> fold (fold Variable.fix_frees o snd) specs   (* FIXME !? *)
wenzelm@20890
    95
      ||>> LocalTheory.axioms specs;
wenzelm@18786
    96
wenzelm@20890
    97
    (* FIXME generic target!? *)
wenzelm@20890
    98
    val hs = map (Term.head_of o #2 o Logic.dest_equals o Thm.prop_of o #2) consts;
wenzelm@20890
    99
    val lthy'' = lthy' |> LocalTheory.theory (Theory.add_finals_i false hs);
wenzelm@20890
   100
wenzelm@20890
   101
    val _ = print_consts lthy' spec_frees cs;
wenzelm@20890
   102
  in ((map #1 consts, axioms), lthy'') end;
wenzelm@18786
   103
wenzelm@18954
   104
val axiomatization = gen_axioms read_specification;
wenzelm@18954
   105
val axiomatization_i = gen_axioms cert_specification;
wenzelm@18620
   106
wenzelm@18786
   107
wenzelm@18786
   108
(* definition *)
wenzelm@18786
   109
wenzelm@20890
   110
fun gen_defs prep args lthy =
wenzelm@18786
   111
  let
wenzelm@20890
   112
    fun define (raw_var, (raw_a, raw_prop)) lthy1 =
wenzelm@18786
   113
      let
wenzelm@20890
   114
        val (vars, [((raw_name, atts), [prop])]) =
wenzelm@20890
   115
          fst (prep (the_list raw_var) [(raw_a, [raw_prop])] lthy1);
wenzelm@20890
   116
        val (((x, T), rhs), prove) = LocalDefs.derived_def lthy1 true prop;
wenzelm@20890
   117
        val name = Thm.def_name_optional x raw_name;
wenzelm@18786
   118
        val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
wenzelm@18786
   119
          if x = x' then mx
wenzelm@18786
   120
          else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x'));
wenzelm@20890
   121
        val ((lhs, (_, th)), lthy2) = lthy1
wenzelm@20890
   122
(*          |> LocalTheory.def ((x, mx), ((name ^ "_raw", []), rhs));  FIXME *)
wenzelm@20890
   123
          |> LocalTheory.def ((x, mx), ((name, []), rhs));
wenzelm@20890
   124
        val ((b, [th']), lthy3) = lthy2
wenzelm@20914
   125
          |> LocalTheory.note ((name, atts), [prove lthy2 th]);
wenzelm@20890
   126
      in (((x, T), (lhs, (b, th'))), LocalTheory.reinit lthy3) end;
wenzelm@18786
   127
wenzelm@20890
   128
    val ((cs, defs), lthy') = lthy |> fold_map define args |>> split_list;
wenzelm@18880
   129
    val def_frees = member (op =) (fold (Term.add_frees o fst) defs []);
wenzelm@20890
   130
    val _ = print_consts lthy' def_frees cs;
wenzelm@20890
   131
  in (defs, lthy') end;
wenzelm@18786
   132
wenzelm@18954
   133
val definition = gen_defs read_specification;
wenzelm@18954
   134
val definition_i = gen_defs cert_specification;
wenzelm@18786
   135
wenzelm@19080
   136
wenzelm@19080
   137
(* abbreviation *)
wenzelm@19080
   138
wenzelm@20890
   139
fun gen_abbrevs prep mode args lthy =
wenzelm@19080
   140
  let
wenzelm@20890
   141
    fun abbrev (raw_var, raw_prop) lthy1 =
wenzelm@19080
   142
      let
wenzelm@19372
   143
        val ((vars, [(_, [prop])]), _) =
wenzelm@19372
   144
          prep (the_list raw_var) [(("", []), [raw_prop])]
wenzelm@20890
   145
            (lthy1 |> ProofContext.expand_abbrevs false);
wenzelm@20890
   146
        val ((x, T), rhs) = LocalDefs.abs_def (#2 (LocalDefs.cert_def lthy1 prop));
wenzelm@19080
   147
        val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
wenzelm@19080
   148
          if x = x' then mx
wenzelm@19080
   149
          else error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote x'));
wenzelm@19080
   150
      in
wenzelm@20890
   151
        lthy1
wenzelm@19664
   152
        |> LocalTheory.abbrevs mode [((x, mx), rhs)]
wenzelm@19080
   153
        |> pair (x, T)
wenzelm@19080
   154
      end;
wenzelm@19080
   155
wenzelm@20890
   156
    val (cs, lthy1) = lthy
wenzelm@19544
   157
      |> ProofContext.set_syntax_mode mode
wenzelm@19544
   158
      |> fold_map abbrev args
wenzelm@20890
   159
      ||> ProofContext.restore_syntax_mode lthy;
wenzelm@20890
   160
    val _ = print_consts lthy1 (K false) cs;
wenzelm@20890
   161
  in lthy1 end;
wenzelm@19080
   162
wenzelm@19080
   163
val abbreviation = gen_abbrevs read_specification;
wenzelm@19080
   164
val abbreviation_i = gen_abbrevs cert_specification;
wenzelm@19080
   165
wenzelm@19664
   166
wenzelm@19664
   167
(* const syntax *)
wenzelm@19664
   168
wenzelm@20890
   169
fun gen_syntax intern_const mode raw_args lthy =
wenzelm@20890
   170
  let val args = raw_args |> map (apfst (intern_const (ProofContext.consts_of lthy)))
wenzelm@20890
   171
  in lthy |> LocalTheory.const_syntax mode args end;
wenzelm@19664
   172
wenzelm@19664
   173
val const_syntax = gen_syntax Consts.intern;
wenzelm@19664
   174
val const_syntax_i = gen_syntax (K I);
wenzelm@19664
   175
wenzelm@20914
   176
wenzelm@20914
   177
(* theorems *)
wenzelm@20914
   178
wenzelm@20914
   179
fun gen_theorems prep_thms prep_att kind raw_facts lthy =
wenzelm@20914
   180
  let
wenzelm@20914
   181
    val k = if kind = "" then [] else [Attrib.kind kind];
wenzelm@20914
   182
    val attrib = prep_att (ProofContext.theory_of lthy);
wenzelm@20914
   183
    val facts = raw_facts |> map (fn ((name, atts), bs) =>
wenzelm@20914
   184
      ((name, map attrib atts),
wenzelm@20914
   185
        bs |> map (fn (b, more_atts) => (prep_thms lthy b, map attrib more_atts @ k))));
wenzelm@20914
   186
    val (res, lthy') = lthy |> LocalTheory.notes facts;
wenzelm@20914
   187
    val _ = present_results lthy' kind res;
wenzelm@20914
   188
  in (res, lthy') end;
wenzelm@20914
   189
wenzelm@20914
   190
val theorems = gen_theorems ProofContext.get_thms Attrib.intern_src;
wenzelm@20914
   191
val theorems_i = gen_theorems (K I) (K I);
wenzelm@20914
   192
wenzelm@18620
   193
end;