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