src/Pure/Isar/specification.ML
author wenzelm
Fri Jan 13 01:13:11 2006 +0100 (2006-01-13)
changeset 18670 c3f445b92aff
parent 18640 61627ae3ddc3
child 18728 6790126ab5f6
permissions -rw-r--r--
uniform handling of fixes;
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@18640
    11
  val pretty_consts: Proof.context -> (string * typ) list -> Pretty.T
wenzelm@18620
    12
  val read_specification:
wenzelm@18640
    13
    (string * string option * mixfix) list *
wenzelm@18640
    14
      ((string * Attrib.src list) * string list) list -> Proof.context ->
wenzelm@18640
    15
    ((string * typ * mixfix) list *
wenzelm@18640
    16
      ((string * Context.generic attribute list) * term list) list) * Proof.context
wenzelm@18620
    17
  val cert_specification:
wenzelm@18640
    18
    (string * typ option * mixfix) list *
wenzelm@18640
    19
      ((string * Context.generic attribute list) * term list) list -> Proof.context ->
wenzelm@18640
    20
    ((string * typ * mixfix) list *
wenzelm@18640
    21
      ((string * Context.generic attribute list) * term list) list) * Proof.context
wenzelm@18620
    22
  val axiomatize:
wenzelm@18640
    23
    (string * string option * mixfix) list *
wenzelm@18640
    24
      ((bstring * Attrib.src list) * string list) list -> theory -> thm list list * theory
wenzelm@18620
    25
  val axiomatize_i:
wenzelm@18640
    26
    (string * typ option * mixfix) list *
wenzelm@18640
    27
      ((bstring * Context.generic attribute list) * term list) list -> theory ->
wenzelm@18640
    28
    thm list list * theory
wenzelm@18620
    29
end;
wenzelm@18620
    30
wenzelm@18620
    31
structure Specification: SPECIFICATION =
wenzelm@18620
    32
struct
wenzelm@18620
    33
wenzelm@18620
    34
(* pretty_consts *)
wenzelm@18620
    35
wenzelm@18640
    36
fun pretty_const ctxt (c, T) =
wenzelm@18620
    37
  Pretty.block [Pretty.str c, Pretty.str " ::", Pretty.brk 1,
wenzelm@18640
    38
    Pretty.quote (ProofContext.pretty_typ ctxt T)];
wenzelm@18620
    39
wenzelm@18640
    40
fun pretty_consts ctxt decls =
wenzelm@18640
    41
  Pretty.big_list "constants" (map (pretty_const ctxt) decls);
wenzelm@18620
    42
wenzelm@18620
    43
wenzelm@18620
    44
(* prepare specification *)
wenzelm@18620
    45
wenzelm@18670
    46
fun prep_specification prep_vars prep_propp prep_att
wenzelm@18670
    47
    (raw_vars, raw_specs) ctxt =
wenzelm@18620
    48
  let
wenzelm@18670
    49
    val thy = ProofContext.theory_of ctxt;
wenzelm@18620
    50
wenzelm@18670
    51
    val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
wenzelm@18670
    52
    val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
wenzelm@18670
    53
    val ((specs, vs), specs_ctxt) =
wenzelm@18620
    54
      prep_propp (params_ctxt, map (map (rpair ([], [])) o snd) raw_specs)
wenzelm@18620
    55
      |> swap |>> map (map fst)
wenzelm@18670
    56
      ||>> fold_map ProofContext.inferred_type xs;
wenzelm@18620
    57
wenzelm@18670
    58
    val params = map2 (fn (x, T) => fn (_, _, mx) => (x, T, mx)) vs vars;
wenzelm@18620
    59
    val names = map (fst o fst) raw_specs;
wenzelm@18670
    60
    val atts = map (map (prep_att thy) o snd o fst) raw_specs;
wenzelm@18620
    61
  in ((params, (names ~~ atts) ~~ specs), specs_ctxt) end;
wenzelm@18620
    62
wenzelm@18620
    63
fun read_specification x =
wenzelm@18670
    64
  prep_specification ProofContext.read_vars ProofContext.read_propp Attrib.generic_attribute x;
wenzelm@18620
    65
fun cert_specification x =
wenzelm@18670
    66
  prep_specification ProofContext.cert_vars ProofContext.cert_propp (K I) x;
wenzelm@18620
    67
wenzelm@18620
    68
wenzelm@18620
    69
(* axiomatize *)
wenzelm@18620
    70
wenzelm@18620
    71
fun gen_axiomatize prep args thy =
wenzelm@18620
    72
  let
wenzelm@18620
    73
    val ((consts, specs), ctxt) = prep args (ProofContext.init thy);
wenzelm@18640
    74
    val subst = consts |> map (fn (x, T, _) => (Free (x, T), Const (Sign.full_name thy x, T)));
wenzelm@18640
    75
    val axioms = specs |> map (fn ((name, att), ts) =>
wenzelm@18640
    76
      ((name, map (Term.subst_atomic subst) ts), map Attrib.theory att));
wenzelm@18620
    77
    val (thms, thy') =
wenzelm@18620
    78
      thy
wenzelm@18620
    79
      |> Theory.add_consts_i consts
wenzelm@18620
    80
      |> PureThy.add_axiomss_i axioms
wenzelm@18620
    81
      ||> Theory.add_finals_i false (map snd subst);
wenzelm@18640
    82
  in Pretty.writeln (pretty_consts ctxt (map (dest_Free o fst) subst)); (thms, thy') end;
wenzelm@18620
    83
wenzelm@18620
    84
val axiomatize = gen_axiomatize read_specification;
wenzelm@18620
    85
val axiomatize_i = gen_axiomatize cert_specification;
wenzelm@18620
    86
wenzelm@18620
    87
end;