src/Pure/Isar/specification.ML
author wenzelm
Sat Jan 07 23:28:01 2006 +0100 (2006-01-07)
changeset 18620 fc8b5f275359
child 18640 61627ae3ddc3
permissions -rw-r--r--
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm@18620
     1
(*  Title:      Pure/Isar/specification.ML
wenzelm@18620
     2
    ID:         $Id$
wenzelm@18620
     3
    Author:     Makarius
wenzelm@18620
     4
wenzelm@18620
     5
Theory specifications --- with type-inference, but no internal
wenzelm@18620
     6
polymorphism.
wenzelm@18620
     7
*)
wenzelm@18620
     8
wenzelm@18620
     9
signature SPECIFICATION =
wenzelm@18620
    10
sig
wenzelm@18620
    11
  val pretty_consts: theory -> (string * typ) list -> Pretty.T
wenzelm@18620
    12
  val read_specification:
wenzelm@18620
    13
    (string * string option * mixfix) list * ((string * Attrib.src list) * string list) list ->
wenzelm@18620
    14
    Proof.context ->
wenzelm@18620
    15
    ((string * typ * mixfix) list * ((string * theory attribute list) * term list) list) *
wenzelm@18620
    16
    Proof.context
wenzelm@18620
    17
  val cert_specification:
wenzelm@18620
    18
    (string * typ option * mixfix) list * ((string * theory attribute list) * term list) list ->
wenzelm@18620
    19
    Proof.context ->
wenzelm@18620
    20
    ((string * typ * mixfix) list * ((string * theory attribute list) * term list) list) *
wenzelm@18620
    21
    Proof.context
wenzelm@18620
    22
  val axiomatize:
wenzelm@18620
    23
    (string * string option * mixfix) list * ((bstring * Attrib.src list) * string list) list ->
wenzelm@18620
    24
    theory -> thm list list * theory
wenzelm@18620
    25
  val axiomatize_i:
wenzelm@18620
    26
    (string * typ option * mixfix) list * ((bstring * theory attribute list) * term list) list ->
wenzelm@18620
    27
    theory -> thm list list * theory
wenzelm@18620
    28
end;
wenzelm@18620
    29
wenzelm@18620
    30
structure Specification: SPECIFICATION =
wenzelm@18620
    31
struct
wenzelm@18620
    32
wenzelm@18620
    33
(* pretty_consts *)
wenzelm@18620
    34
wenzelm@18620
    35
fun pretty_const thy (c, T) =
wenzelm@18620
    36
  Pretty.block [Pretty.str c, Pretty.str " ::", Pretty.brk 1,
wenzelm@18620
    37
    Pretty.quote (Sign.pretty_typ thy T)];
wenzelm@18620
    38
wenzelm@18620
    39
fun pretty_consts thy decls =
wenzelm@18620
    40
  Pretty.big_list "constants" (map (pretty_const thy) decls);
wenzelm@18620
    41
wenzelm@18620
    42
wenzelm@18620
    43
(* prepare specification *)
wenzelm@18620
    44
wenzelm@18620
    45
fun prep_specification prep_typ prep_propp prep_att
wenzelm@18620
    46
    (raw_params, raw_specs) context =
wenzelm@18620
    47
  let
wenzelm@18620
    48
    fun prep_param (x, raw_T, mx) ctxt =
wenzelm@18620
    49
      let
wenzelm@18620
    50
        val x' = Syntax.const_name x mx and mx' = Syntax.fix_mixfix x mx;
wenzelm@18620
    51
        val T = Option.map (prep_typ ctxt) raw_T;
wenzelm@18620
    52
      in ((x', mx'), ProofContext.add_fixes [(x', T, SOME mx')] ctxt) end;
wenzelm@18620
    53
wenzelm@18620
    54
    val ((xs, mxs), params_ctxt) =
wenzelm@18620
    55
      context |> fold_map prep_param raw_params |>> split_list;
wenzelm@18620
    56
    val ((specs, vars), specs_ctxt) =
wenzelm@18620
    57
      prep_propp (params_ctxt, map (map (rpair ([], [])) o snd) raw_specs)
wenzelm@18620
    58
      |> swap |>> map (map fst)
wenzelm@18620
    59
      ||>> fold_map ProofContext.declared_type xs;
wenzelm@18620
    60
wenzelm@18620
    61
    val params = map2 (fn (x, T) => fn mx => (x, T, mx)) vars mxs;
wenzelm@18620
    62
    val names = map (fst o fst) raw_specs;
wenzelm@18620
    63
    val atts = map (map (prep_att (ProofContext.theory_of context)) o snd o fst) raw_specs;
wenzelm@18620
    64
  in ((params, (names ~~ atts) ~~ specs), specs_ctxt) end;
wenzelm@18620
    65
wenzelm@18620
    66
fun read_specification x =
wenzelm@18620
    67
  prep_specification ProofContext.read_typ ProofContext.read_propp Attrib.global_attribute x;
wenzelm@18620
    68
fun cert_specification x =
wenzelm@18620
    69
  prep_specification ProofContext.cert_typ ProofContext.cert_propp (K I) x;
wenzelm@18620
    70
wenzelm@18620
    71
wenzelm@18620
    72
(* axiomatize *)
wenzelm@18620
    73
wenzelm@18620
    74
fun gen_axiomatize prep args thy =
wenzelm@18620
    75
  let
wenzelm@18620
    76
    val ((consts, specs), ctxt) = prep args (ProofContext.init thy);
wenzelm@18620
    77
    val subst = map (fn (x, T, _) => (Free (x, T), Const (Sign.full_name thy x, T))) consts;
wenzelm@18620
    78
    val axioms =
wenzelm@18620
    79
      map (fn ((name, att), ts) => ((name, map (Term.subst_atomic subst) ts), att)) specs;
wenzelm@18620
    80
    val (thms, thy') =
wenzelm@18620
    81
      thy
wenzelm@18620
    82
      |> Theory.add_consts_i consts
wenzelm@18620
    83
      |> PureThy.add_axiomss_i axioms
wenzelm@18620
    84
      ||> Theory.add_finals_i false (map snd subst);
wenzelm@18620
    85
  in Pretty.writeln (pretty_consts thy' (map (dest_Free o fst) subst)); (thms, thy') end;
wenzelm@18620
    86
wenzelm@18620
    87
val axiomatize = gen_axiomatize read_specification;
wenzelm@18620
    88
val axiomatize_i = gen_axiomatize cert_specification;
wenzelm@18620
    89
wenzelm@18620
    90
end;