src/Pure/Isar/specification.ML
author wenzelm
Fri, 10 Feb 2006 02:22:59 +0100
changeset 19007 0f7b92f75df7
parent 18954 ab48b6ac9327
child 19080 46ba991e27d5
permissions -rw-r--r--
statement: improved error msg;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/specification.ML
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     4
18810
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
     5
Common theory/locale specifications --- with type-inference and
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
     6
toplevel polymorphism.
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     7
*)
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     8
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     9
signature SPECIFICATION =
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    10
sig
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    11
  val read_specification: (string * string option * mixfix) list ->
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    12
    ((string * Attrib.src list) * string list) list -> local_theory ->
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    13
    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    14
    local_theory
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    15
  val cert_specification: (string * typ option * mixfix) list ->
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    16
    ((string * Attrib.src list) * term list) list -> local_theory ->
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    17
    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    18
    local_theory
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    19
  val axiomatization: (string * string option * mixfix) list ->
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    20
    ((bstring * Attrib.src list) * string list) list -> local_theory ->
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    21
    (term list * (bstring * thm list) list) * local_theory
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    22
  val axiomatization_i: (string * typ option * mixfix) list ->
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    23
    ((bstring * Attrib.src list) * term list) list -> local_theory ->
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    24
    (term list * (bstring * thm list) list) * local_theory
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    25
  val definition:
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    26
    ((string * string option * mixfix) option * ((string * Attrib.src list) * string)) list ->
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    27
    local_theory -> (term * (bstring * thm)) list * local_theory
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    28
  val definition_i:
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    29
    ((string * typ option * mixfix) option * ((string * Attrib.src list) * term)) list ->
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    30
    local_theory -> (term * (bstring * thm)) list * local_theory
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    31
end;
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    32
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    33
structure Specification: SPECIFICATION =
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    34
struct
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    35
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    36
(* prepare specification *)
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    37
18828
26b80ed2259b added axiomatization_loc, definition_loc;
wenzelm
parents: 18810
diff changeset
    38
fun prep_specification prep_vars prep_propp prep_att raw_vars raw_specs ctxt =
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    39
  let
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    40
    val thy = ProofContext.theory_of ctxt;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    41
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    42
    val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    43
    val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    44
    val ((specs, vs), specs_ctxt) =
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    45
      prep_propp (params_ctxt, map (map (rpair ([], [])) o snd) raw_specs)
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    46
      |> swap |>> map (map fst)
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    47
      ||>> fold_map ProofContext.inferred_param xs;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    48
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    49
    val params = vs ~~ map #3 vars;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    50
    val names = map (fst o fst) raw_specs;
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    51
    val atts = map (map (prep_att thy) o snd o fst) raw_specs;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    52
  in ((params, (names ~~ atts) ~~ specs), specs_ctxt) end;
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    53
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    54
fun read_specification x =
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    55
  prep_specification ProofContext.read_vars ProofContext.read_propp Attrib.intern_src x;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    56
fun cert_specification x =
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    57
  prep_specification ProofContext.cert_vars ProofContext.cert_propp (K I) x;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    58
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    59
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    60
(* axiomatization *)
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    61
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    62
fun gen_axioms prep raw_vars raw_specs ctxt =
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    63
  let
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    64
    val (vars, specs) = fst (prep raw_vars raw_specs ctxt);
18828
26b80ed2259b added axiomatization_loc, definition_loc;
wenzelm
parents: 18810
diff changeset
    65
    val cs = map fst vars;
18880
b8a1c3cdf739 axiomatization: retrict parameters to occurrences in specs;
wenzelm
parents: 18828
diff changeset
    66
    val spec_frees = member (op =) (fold (fold Term.add_frees o snd) specs []);
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    67
18880
b8a1c3cdf739 axiomatization: retrict parameters to occurrences in specs;
wenzelm
parents: 18828
diff changeset
    68
    val (consts, consts_ctxt) = ctxt |> LocalTheory.consts_restricted spec_frees vars;
18828
26b80ed2259b added axiomatization_loc, definition_loc;
wenzelm
parents: 18810
diff changeset
    69
    val subst = Term.subst_atomic (map Free cs ~~ consts);
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    70
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    71
    val (axioms, axioms_ctxt) =
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    72
      consts_ctxt
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    73
      |> LocalTheory.axioms (specs |> map (fn (a, props) => (a, map subst props)))
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    74
      ||> LocalTheory.theory (Theory.add_finals_i false (map Term.head_of consts));
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    75
    val _ = LocalTheory.print_consts ctxt spec_frees cs;
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    76
  in ((consts, axioms), axioms_ctxt) end;
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    77
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    78
val axiomatization = gen_axioms read_specification;
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    79
val axiomatization_i = gen_axioms cert_specification;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    80
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    81
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    82
(* definition *)
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    83
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    84
fun gen_defs prep args ctxt =
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    85
  let
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    86
    fun define (raw_var, (raw_a, raw_prop)) ctxt' =
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    87
      let
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    88
        val (vars, [(a, [prop])]) = fst (prep (the_list raw_var) [(raw_a, [raw_prop])] ctxt');
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    89
        val (((x, T), rhs), prove) = LocalDefs.derived_def ctxt' true prop;
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    90
        val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    91
          if x = x' then mx
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    92
          else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x'));
18810
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
    93
      in
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
    94
        ctxt'
18810
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
    95
        |> LocalTheory.def_finish prove ((x, mx), (a, rhs))
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
    96
        |>> pair (x, T)
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
    97
      end;
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    98
18828
26b80ed2259b added axiomatization_loc, definition_loc;
wenzelm
parents: 18810
diff changeset
    99
    val ((cs, defs), defs_ctxt) = ctxt |> fold_map define args |>> split_list;
18880
b8a1c3cdf739 axiomatization: retrict parameters to occurrences in specs;
wenzelm
parents: 18828
diff changeset
   100
    val def_frees = member (op =) (fold (Term.add_frees o fst) defs []);
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
   101
    val _ = LocalTheory.print_consts ctxt def_frees cs;
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
   102
  in (defs, defs_ctxt) end;
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   103
18954
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
   104
val definition = gen_defs read_specification;
ab48b6ac9327 type local_theory;
wenzelm
parents: 18880
diff changeset
   105
val definition_i = gen_defs cert_specification;
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   106
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
   107
end;