src/Pure/Isar/specification.ML
author wenzelm
Wed Jan 25 00:21:44 2006 +0100 (2006-01-25)
changeset 18786 591a37d48794
parent 18771 63efe00371af
child 18810 6dc5416368e9
permissions -rw-r--r--
added definition(_i);
wenzelm@18620
     1
(*  Title:      Pure/Isar/specification.ML
wenzelm@18620
     2
    ID:         $Id$
wenzelm@18620
     3
    Author:     Makarius
wenzelm@18620
     4
wenzelm@18771
     5
Common theory/locale specifications --- with type-inference, but
wenzelm@18771
     6
without internal 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@18771
    12
    ((string * Attrib.src list) * string list) list -> Proof.context ->
wenzelm@18771
    13
    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
wenzelm@18771
    14
    Proof.context
wenzelm@18771
    15
  val cert_specification: (string * typ option * mixfix) list ->
wenzelm@18771
    16
    ((string * Attrib.src list) * term list) list -> Proof.context ->
wenzelm@18771
    17
    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
wenzelm@18771
    18
    Proof.context
wenzelm@18771
    19
  val axiomatization: xstring option -> (string * string option * mixfix) list ->
wenzelm@18771
    20
    ((bstring * Attrib.src list) * string list) list -> theory ->
wenzelm@18771
    21
    (term list * (bstring * thm list) list) * (theory * Proof.context)
wenzelm@18771
    22
  val axiomatization_i: string option -> (string * typ option * mixfix) list ->
wenzelm@18771
    23
    ((bstring * Attrib.src list) * term list) list -> theory ->
wenzelm@18771
    24
    (term list * (bstring * thm list) list) * (theory * Proof.context)
wenzelm@18786
    25
  val definition: xstring option ->
wenzelm@18786
    26
    ((string * string option * mixfix) option * ((string * Attrib.src list) * string)) list ->
wenzelm@18786
    27
    theory -> (term * (bstring * thm)) list * (theory * Proof.context)
wenzelm@18786
    28
  val definition_i: string option ->
wenzelm@18786
    29
    ((string * typ option * mixfix) option * ((string * Attrib.src list) * term)) list ->
wenzelm@18786
    30
    theory -> (term * (bstring * thm)) list * (theory * Proof.context)
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@18670
    38
fun prep_specification prep_vars prep_propp prep_att
wenzelm@18771
    39
    raw_vars raw_specs ctxt =
wenzelm@18620
    40
  let
wenzelm@18670
    41
    val thy = ProofContext.theory_of ctxt;
wenzelm@18620
    42
wenzelm@18670
    43
    val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
wenzelm@18670
    44
    val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
wenzelm@18670
    45
    val ((specs, vs), specs_ctxt) =
wenzelm@18620
    46
      prep_propp (params_ctxt, map (map (rpair ([], [])) o snd) raw_specs)
wenzelm@18620
    47
      |> swap |>> map (map fst)
wenzelm@18771
    48
      ||>> fold_map ProofContext.inferred_param xs;
wenzelm@18620
    49
wenzelm@18771
    50
    val params = vs ~~ map #3 vars;
wenzelm@18620
    51
    val names = map (fst o fst) raw_specs;
wenzelm@18670
    52
    val atts = map (map (prep_att thy) o snd o fst) raw_specs;
wenzelm@18620
    53
  in ((params, (names ~~ atts) ~~ specs), specs_ctxt) end;
wenzelm@18620
    54
wenzelm@18620
    55
fun read_specification x =
wenzelm@18771
    56
  prep_specification ProofContext.read_vars ProofContext.read_propp Attrib.intern_src x;
wenzelm@18620
    57
fun cert_specification x =
wenzelm@18670
    58
  prep_specification ProofContext.cert_vars ProofContext.cert_propp (K I) x;
wenzelm@18620
    59
wenzelm@18620
    60
wenzelm@18771
    61
(* axiomatization *)
wenzelm@18620
    62
wenzelm@18771
    63
fun gen_axiomatization prep init locale raw_vars raw_specs thy =
wenzelm@18620
    64
  let
wenzelm@18786
    65
    val ctxt = init locale thy;
wenzelm@18786
    66
    val (vars, specs) = fst (prep raw_vars raw_specs ctxt);
wenzelm@18786
    67
wenzelm@18771
    68
    val (consts, consts_ctxt) = ctxt |> LocalTheory.consts vars;
wenzelm@18771
    69
    val subst = Term.subst_atomic (map (Free o fst) vars ~~ 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@18786
    75
wenzelm@18771
    76
    val _ = Pretty.writeln (LocalTheory.pretty_consts ctxt (map fst vars));
wenzelm@18771
    77
  in ((consts, axioms), `LocalTheory.exit axioms_ctxt) end;
wenzelm@18620
    78
wenzelm@18771
    79
val axiomatization = gen_axiomatization read_specification LocalTheory.init;
wenzelm@18771
    80
val axiomatization_i = gen_axiomatization cert_specification LocalTheory.init_i;
wenzelm@18620
    81
wenzelm@18786
    82
wenzelm@18786
    83
(* definition *)
wenzelm@18786
    84
wenzelm@18786
    85
fun gen_definition prep init locale args thy =
wenzelm@18786
    86
  let
wenzelm@18786
    87
    fun define (raw_var, (raw_a, raw_prop)) ctxt =
wenzelm@18786
    88
      let
wenzelm@18786
    89
        val (vars, [(a, [prop])]) = fst (prep (the_list raw_var) [(raw_a, [raw_prop])] ctxt);
wenzelm@18786
    90
        val ((x, T), rhs) = prop
wenzelm@18786
    91
          |> Logic.strip_imp_concl
wenzelm@18786
    92
          |> ObjectLogic.reverse_atomize_term thy
wenzelm@18786
    93
          |> (snd o ProofContext.cert_def ctxt)
wenzelm@18786
    94
          |> ProofContext.abs_def;
wenzelm@18786
    95
        val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
wenzelm@18786
    96
          if x = x' then mx
wenzelm@18786
    97
          else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x'));
wenzelm@18786
    98
wenzelm@18786
    99
        fun prove ctxt' const def =
wenzelm@18786
   100
          let
wenzelm@18786
   101
            val prop' = Term.subst_atomic [(Free (x, T), const)] prop;
wenzelm@18786
   102
            val (As, B) = Logic.strip_horn prop';
wenzelm@18786
   103
          in
wenzelm@18786
   104
            (Goal.prove (ProofContext.theory_of ctxt') [] As B (K (ALLGOALS
wenzelm@18786
   105
              (ObjectLogic.reverse_atomize_tac THEN'
wenzelm@18786
   106
                Tactic.rewrite_goal_tac [def] THEN'
wenzelm@18786
   107
                Tactic.resolve_tac [Drule.reflexive_thm])))
wenzelm@18786
   108
              handle ERROR msg => cat_error msg "Failed to prove definitional specification.")
wenzelm@18786
   109
            |> LocalTheory.standard (ProofContext.fix_frees prop' ctxt')
wenzelm@18786
   110
          end;
wenzelm@18786
   111
      in ctxt |> LocalTheory.def' prove ((x, mx), (a, rhs)) |>> pair (x, T) end;
wenzelm@18786
   112
wenzelm@18786
   113
    val ctxt = init locale thy;
wenzelm@18786
   114
    val ((decls, defs), defs_ctxt) = ctxt |> fold_map define args |>> split_list;
wenzelm@18786
   115
    val _ = Pretty.writeln (LocalTheory.pretty_consts ctxt decls);
wenzelm@18786
   116
  in (defs, `LocalTheory.exit defs_ctxt) end;
wenzelm@18786
   117
wenzelm@18786
   118
val definition = gen_definition read_specification LocalTheory.init;
wenzelm@18786
   119
val definition_i = gen_definition cert_specification LocalTheory.init_i;
wenzelm@18786
   120
wenzelm@18620
   121
end;