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