src/Pure/Isar/specification.ML
author wenzelm
Thu Feb 16 18:26:04 2006 +0100 (2006-02-16)
changeset 19080 46ba991e27d5
parent 18954 ab48b6ac9327
child 19372 3ff5f1777743
permissions -rw-r--r--
added abbreviation(_i);
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@19080
    31
  val abbreviation: bool -> ((string * string option * mixfix) option * string) list ->
wenzelm@19080
    32
    local_theory -> local_theory
wenzelm@19080
    33
  val abbreviation_i: bool -> ((string * typ option * mixfix) option * term) list ->
wenzelm@19080
    34
    local_theory -> local_theory
wenzelm@18620
    35
end;
wenzelm@18620
    36
wenzelm@18620
    37
structure Specification: SPECIFICATION =
wenzelm@18620
    38
struct
wenzelm@18620
    39
wenzelm@18620
    40
(* prepare specification *)
wenzelm@18620
    41
wenzelm@18828
    42
fun prep_specification prep_vars prep_propp prep_att raw_vars raw_specs ctxt =
wenzelm@18620
    43
  let
wenzelm@18670
    44
    val thy = ProofContext.theory_of ctxt;
wenzelm@18620
    45
wenzelm@18670
    46
    val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
wenzelm@18670
    47
    val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
wenzelm@18670
    48
    val ((specs, vs), specs_ctxt) =
wenzelm@18620
    49
      prep_propp (params_ctxt, map (map (rpair ([], [])) o snd) raw_specs)
wenzelm@18620
    50
      |> swap |>> map (map fst)
wenzelm@18771
    51
      ||>> fold_map ProofContext.inferred_param xs;
wenzelm@18620
    52
wenzelm@18771
    53
    val params = vs ~~ map #3 vars;
wenzelm@18620
    54
    val names = map (fst o fst) raw_specs;
wenzelm@18670
    55
    val atts = map (map (prep_att thy) o snd o fst) raw_specs;
wenzelm@18620
    56
  in ((params, (names ~~ atts) ~~ specs), specs_ctxt) end;
wenzelm@18620
    57
wenzelm@18620
    58
fun read_specification x =
wenzelm@18771
    59
  prep_specification ProofContext.read_vars ProofContext.read_propp Attrib.intern_src x;
wenzelm@18620
    60
fun cert_specification x =
wenzelm@18670
    61
  prep_specification ProofContext.cert_vars ProofContext.cert_propp (K I) x;
wenzelm@18620
    62
wenzelm@18620
    63
wenzelm@18771
    64
(* axiomatization *)
wenzelm@18620
    65
wenzelm@18954
    66
fun gen_axioms prep raw_vars raw_specs ctxt =
wenzelm@18620
    67
  let
wenzelm@18786
    68
    val (vars, specs) = fst (prep raw_vars raw_specs ctxt);
wenzelm@18828
    69
    val cs = map fst vars;
wenzelm@18880
    70
    val spec_frees = member (op =) (fold (fold Term.add_frees o snd) specs []);
wenzelm@18786
    71
wenzelm@18880
    72
    val (consts, consts_ctxt) = ctxt |> LocalTheory.consts_restricted spec_frees vars;
wenzelm@18828
    73
    val subst = Term.subst_atomic (map Free cs ~~ consts);
wenzelm@18786
    74
wenzelm@18771
    75
    val (axioms, axioms_ctxt) =
wenzelm@18771
    76
      consts_ctxt
wenzelm@18771
    77
      |> LocalTheory.axioms (specs |> map (fn (a, props) => (a, map subst props)))
wenzelm@18786
    78
      ||> LocalTheory.theory (Theory.add_finals_i false (map Term.head_of consts));
wenzelm@18954
    79
    val _ = LocalTheory.print_consts ctxt spec_frees cs;
wenzelm@18954
    80
  in ((consts, axioms), axioms_ctxt) end;
wenzelm@18786
    81
wenzelm@18954
    82
val axiomatization = gen_axioms read_specification;
wenzelm@18954
    83
val axiomatization_i = gen_axioms cert_specification;
wenzelm@18620
    84
wenzelm@18786
    85
wenzelm@18786
    86
(* definition *)
wenzelm@18786
    87
wenzelm@18954
    88
fun gen_defs prep args ctxt =
wenzelm@18786
    89
  let
wenzelm@18954
    90
    fun define (raw_var, (raw_a, raw_prop)) ctxt' =
wenzelm@18786
    91
      let
wenzelm@18954
    92
        val (vars, [(a, [prop])]) = fst (prep (the_list raw_var) [(raw_a, [raw_prop])] ctxt');
wenzelm@18954
    93
        val (((x, T), rhs), prove) = LocalDefs.derived_def ctxt' true prop;
wenzelm@18786
    94
        val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
wenzelm@18786
    95
          if x = x' then mx
wenzelm@18786
    96
          else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x'));
wenzelm@18810
    97
      in
wenzelm@18954
    98
        ctxt'
wenzelm@18810
    99
        |> LocalTheory.def_finish prove ((x, mx), (a, rhs))
wenzelm@18810
   100
        |>> pair (x, T)
wenzelm@18810
   101
      end;
wenzelm@18786
   102
wenzelm@18828
   103
    val ((cs, defs), defs_ctxt) = ctxt |> fold_map define args |>> split_list;
wenzelm@18880
   104
    val def_frees = member (op =) (fold (Term.add_frees o fst) defs []);
wenzelm@18954
   105
    val _ = LocalTheory.print_consts ctxt def_frees cs;
wenzelm@18954
   106
  in (defs, defs_ctxt) end;
wenzelm@18786
   107
wenzelm@18954
   108
val definition = gen_defs read_specification;
wenzelm@18954
   109
val definition_i = gen_defs cert_specification;
wenzelm@18786
   110
wenzelm@19080
   111
wenzelm@19080
   112
(* abbreviation *)
wenzelm@19080
   113
wenzelm@19080
   114
fun gen_abbrevs prep revert args ctxt =
wenzelm@19080
   115
  let
wenzelm@19080
   116
    fun abbrev (raw_var, raw_prop) ctxt' =
wenzelm@19080
   117
      let
wenzelm@19080
   118
        val (vars, [(_, [prop])]) = fst (prep (the_list raw_var) [(("", []), [raw_prop])] ctxt');
wenzelm@19080
   119
        val ((x, T), rhs) = #1 (LocalDefs.derived_def ctxt' false prop);
wenzelm@19080
   120
        val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
wenzelm@19080
   121
          if x = x' then mx
wenzelm@19080
   122
          else error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote x'));
wenzelm@19080
   123
      in
wenzelm@19080
   124
        ctxt'
wenzelm@19080
   125
        |> LocalTheory.abbrev revert ((x, mx), rhs)
wenzelm@19080
   126
        |> pair (x, T)
wenzelm@19080
   127
      end;
wenzelm@19080
   128
wenzelm@19080
   129
    val (cs, abbrs_ctxt) = ctxt |> fold_map abbrev args;
wenzelm@19080
   130
    val _ = LocalTheory.print_consts ctxt (K false) cs;
wenzelm@19080
   131
  in abbrs_ctxt end;
wenzelm@19080
   132
wenzelm@19080
   133
val abbreviation = gen_abbrevs read_specification;
wenzelm@19080
   134
val abbreviation_i = gen_abbrevs cert_specification;
wenzelm@19080
   135
wenzelm@18620
   136
end;