src/Pure/Isar/specification.ML
author wenzelm
Fri, 27 Jan 2006 19:03:16 +0100
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;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Isar/specification.ML
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     4
18810
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
     5
Common theory/locale specifications --- with type-inference and
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
     6
toplevel polymorphism.
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     7
*)
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     8
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
     9
signature SPECIFICATION =
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    10
sig
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    11
  val read_specification: (string * string option * mixfix) list ->
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    12
    ((string * Attrib.src list) * string list) list -> Proof.context ->
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    13
    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    14
    Proof.context
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    15
  val cert_specification: (string * typ option * mixfix) list ->
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    16
    ((string * Attrib.src list) * term list) list -> Proof.context ->
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    17
    (((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) *
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    18
    Proof.context
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    19
  val axiomatization: xstring option -> (string * string option * mixfix) list ->
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    20
    ((bstring * Attrib.src list) * string list) list -> theory ->
18810
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
    21
    (term list * (bstring * thm list) list) * (Proof.context * theory)
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    22
  val axiomatization_i: string option -> (string * typ option * mixfix) list ->
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    23
    ((bstring * Attrib.src list) * term list) list -> theory ->
18810
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
    24
    (term list * (bstring * thm list) list) * (Proof.context * theory)
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    25
  val definition: xstring option ->
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    26
    ((string * string option * mixfix) option * ((string * Attrib.src list) * string)) list ->
18810
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
    27
    theory -> (term * (bstring * thm)) list * (Proof.context * theory)
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    28
  val definition_i: string option ->
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    29
    ((string * typ option * mixfix) option * ((string * Attrib.src list) * term)) list ->
18810
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
    30
    theory -> (term * (bstring * thm)) list * (Proof.context * theory)
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    31
end;
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    32
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    33
structure Specification: SPECIFICATION =
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    34
struct
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    35
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    36
(* prepare specification *)
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    37
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    38
fun prep_specification prep_vars prep_propp prep_att
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    39
    raw_vars raw_specs ctxt =
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    40
  let
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    41
    val thy = ProofContext.theory_of ctxt;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    42
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    43
    val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars;
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    44
    val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    45
    val ((specs, vs), specs_ctxt) =
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    46
      prep_propp (params_ctxt, map (map (rpair ([], [])) o snd) raw_specs)
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    47
      |> swap |>> map (map fst)
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    48
      ||>> fold_map ProofContext.inferred_param xs;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    49
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    50
    val params = vs ~~ map #3 vars;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    51
    val names = map (fst o fst) raw_specs;
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    52
    val atts = map (map (prep_att thy) o snd o fst) raw_specs;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    53
  in ((params, (names ~~ atts) ~~ specs), specs_ctxt) end;
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    54
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    55
fun read_specification x =
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    56
  prep_specification ProofContext.read_vars ProofContext.read_propp Attrib.intern_src x;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    57
fun cert_specification x =
18670
c3f445b92aff uniform handling of fixes;
wenzelm
parents: 18640
diff changeset
    58
  prep_specification ProofContext.cert_vars ProofContext.cert_propp (K I) x;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    59
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    60
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    61
(* axiomatization *)
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    62
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    63
fun gen_axiomatization prep init locale raw_vars raw_specs thy =
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    64
  let
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    65
    val ctxt = init locale thy;
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    66
    val (vars, specs) = fst (prep raw_vars raw_specs ctxt);
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    67
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    68
    val (consts, consts_ctxt) = ctxt |> LocalTheory.consts vars;
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    69
    val subst = Term.subst_atomic (map (Free o fst) vars ~~ consts);
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    70
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    71
    val (axioms, axioms_ctxt) =
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    72
      consts_ctxt
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    73
      |> LocalTheory.axioms (specs |> map (fn (a, props) => (a, map subst props)))
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    74
      ||> LocalTheory.theory (Theory.add_finals_i false (map Term.head_of consts));
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    75
18810
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
    76
    val _ =
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
    77
      if null vars then ()
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
    78
      else Pretty.writeln (LocalTheory.pretty_consts ctxt (map fst vars));
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
    79
  in ((consts, axioms), LocalTheory.exit axioms_ctxt) end;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    80
18771
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    81
val axiomatization = gen_axiomatization read_specification LocalTheory.init;
63efe00371af renamed axiomatize(_i) to axiomatization(_i);
wenzelm
parents: 18728
diff changeset
    82
val axiomatization_i = gen_axiomatization cert_specification LocalTheory.init_i;
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
    83
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    84
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    85
(* definition *)
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    86
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    87
fun gen_definition prep init locale args thy =
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    88
  let
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    89
    fun define (raw_var, (raw_a, raw_prop)) ctxt =
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    90
      let
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    91
        val (vars, [(a, [prop])]) = fst (prep (the_list raw_var) [(raw_a, [raw_prop])] ctxt);
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    92
        val ((x, T), rhs) = prop
18810
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
    93
          |> ObjectLogic.rulify_term thy
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
    94
          |> ObjectLogic.unatomize_term thy   (*produce meta-level equality*)
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    95
          |> Logic.strip_imp_concl
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    96
          |> (snd o ProofContext.cert_def ctxt)
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    97
          |> ProofContext.abs_def;
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    98
        val mx = (case vars of [] => NoSyn | [((x', _), mx)] =>
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
    99
          if x = x' then mx
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   100
          else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x'));
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   101
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   102
        fun prove ctxt' const def =
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   103
          let
18810
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
   104
            val thy' = ProofContext.theory_of ctxt';
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   105
            val prop' = Term.subst_atomic [(Free (x, T), const)] prop;
18810
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
   106
            val frees = Term.fold_aterms (fn Free (x, _) =>
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
   107
              if ProofContext.is_fixed ctxt' x then I else insert (op =) x | _ => I) prop' [];
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   108
          in
18810
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
   109
            Goal.prove thy' frees [] prop' (K (ALLGOALS
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
   110
              (ObjectLogic.rulify_tac THEN'
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
   111
                ObjectLogic.unatomize_tac THEN'
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   112
                Tactic.rewrite_goal_tac [def] THEN'
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   113
                Tactic.resolve_tac [Drule.reflexive_thm])))
18810
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
   114
            handle ERROR msg => cat_error msg "Failed to prove definitional specification."
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   115
          end;
18810
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
   116
      in
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
   117
        ctxt
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
   118
        |> LocalTheory.def_finish prove ((x, mx), (a, rhs))
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
   119
        |>> pair (x, T)
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
   120
      end;
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   121
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   122
    val ctxt = init locale thy;
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   123
    val ((decls, defs), defs_ctxt) = ctxt |> fold_map define args |>> split_list;
18810
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
   124
    val _ =
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
   125
      if null decls then ()
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
   126
      else Pretty.writeln (LocalTheory.pretty_consts ctxt decls);
6dc5416368e9 swapped Toplevel.theory_context;
wenzelm
parents: 18786
diff changeset
   127
  in (defs, LocalTheory.exit defs_ctxt) end;
18786
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   128
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   129
val definition = gen_definition read_specification LocalTheory.init;
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   130
val definition_i = gen_definition cert_specification LocalTheory.init_i;
591a37d48794 added definition(_i);
wenzelm
parents: 18771
diff changeset
   131
18620
fc8b5f275359 Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff changeset
   132
end;