src/Pure/Isar/specification.ML
author wenzelm
Fri Jan 27 19:03:16 2006 +0100 (2006-01-27)
changeset 18810 6dc5416368e9
parent 18786 591a37d48794
child 18828 26b80ed2259b
permissions -rw-r--r--
swapped Toplevel.theory_context;
definition(_i): actually rulify as well, support more of object-logic;
definition(_i): more precise treatment of local fixes;
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@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@18810
    21
    (term list * (bstring * thm list) list) * (Proof.context * theory)
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@18810
    24
    (term list * (bstring * thm list) list) * (Proof.context * theory)
wenzelm@18786
    25
  val definition: xstring option ->
wenzelm@18786
    26
    ((string * string option * mixfix) option * ((string * Attrib.src list) * string)) list ->
wenzelm@18810
    27
    theory -> (term * (bstring * thm)) list * (Proof.context * theory)
wenzelm@18786
    28
  val definition_i: string option ->
wenzelm@18786
    29
    ((string * typ option * mixfix) option * ((string * Attrib.src list) * term)) list ->
wenzelm@18810
    30
    theory -> (term * (bstring * thm)) list * (Proof.context * 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@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@18810
    76
    val _ =
wenzelm@18810
    77
      if null vars then ()
wenzelm@18810
    78
      else Pretty.writeln (LocalTheory.pretty_consts ctxt (map fst vars));
wenzelm@18810
    79
  in ((consts, axioms), LocalTheory.exit axioms_ctxt) end;
wenzelm@18620
    80
wenzelm@18771
    81
val axiomatization = gen_axiomatization read_specification LocalTheory.init;
wenzelm@18771
    82
val axiomatization_i = gen_axiomatization cert_specification LocalTheory.init_i;
wenzelm@18620
    83
wenzelm@18786
    84
wenzelm@18786
    85
(* definition *)
wenzelm@18786
    86
wenzelm@18786
    87
fun gen_definition prep init locale args thy =
wenzelm@18786
    88
  let
wenzelm@18786
    89
    fun define (raw_var, (raw_a, raw_prop)) ctxt =
wenzelm@18786
    90
      let
wenzelm@18786
    91
        val (vars, [(a, [prop])]) = fst (prep (the_list raw_var) [(raw_a, [raw_prop])] ctxt);
wenzelm@18786
    92
        val ((x, T), rhs) = prop
wenzelm@18810
    93
          |> ObjectLogic.rulify_term thy
wenzelm@18810
    94
          |> ObjectLogic.unatomize_term thy   (*produce meta-level equality*)
wenzelm@18786
    95
          |> Logic.strip_imp_concl
wenzelm@18786
    96
          |> (snd o ProofContext.cert_def ctxt)
wenzelm@18786
    97
          |> ProofContext.abs_def;
wenzelm@18786
    98
        val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
wenzelm@18786
    99
          if x = x' then mx
wenzelm@18786
   100
          else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x'));
wenzelm@18786
   101
wenzelm@18786
   102
        fun prove ctxt' const def =
wenzelm@18786
   103
          let
wenzelm@18810
   104
            val thy' = ProofContext.theory_of ctxt';
wenzelm@18786
   105
            val prop' = Term.subst_atomic [(Free (x, T), const)] prop;
wenzelm@18810
   106
            val frees = Term.fold_aterms (fn Free (x, _) =>
wenzelm@18810
   107
              if ProofContext.is_fixed ctxt' x then I else insert (op =) x | _ => I) prop' [];
wenzelm@18786
   108
          in
wenzelm@18810
   109
            Goal.prove thy' frees [] prop' (K (ALLGOALS
wenzelm@18810
   110
              (ObjectLogic.rulify_tac THEN'
wenzelm@18810
   111
                ObjectLogic.unatomize_tac THEN'
wenzelm@18786
   112
                Tactic.rewrite_goal_tac [def] THEN'
wenzelm@18786
   113
                Tactic.resolve_tac [Drule.reflexive_thm])))
wenzelm@18810
   114
            handle ERROR msg => cat_error msg "Failed to prove definitional specification."
wenzelm@18786
   115
          end;
wenzelm@18810
   116
      in
wenzelm@18810
   117
        ctxt
wenzelm@18810
   118
        |> LocalTheory.def_finish prove ((x, mx), (a, rhs))
wenzelm@18810
   119
        |>> pair (x, T)
wenzelm@18810
   120
      end;
wenzelm@18786
   121
wenzelm@18786
   122
    val ctxt = init locale thy;
wenzelm@18786
   123
    val ((decls, defs), defs_ctxt) = ctxt |> fold_map define args |>> split_list;
wenzelm@18810
   124
    val _ =
wenzelm@18810
   125
      if null decls then ()
wenzelm@18810
   126
      else Pretty.writeln (LocalTheory.pretty_consts ctxt decls);
wenzelm@18810
   127
  in (defs, LocalTheory.exit defs_ctxt) end;
wenzelm@18786
   128
wenzelm@18786
   129
val definition = gen_definition read_specification LocalTheory.init;
wenzelm@18786
   130
val definition_i = gen_definition cert_specification LocalTheory.init_i;
wenzelm@18786
   131
wenzelm@18620
   132
end;