| 
24592
 | 
     1  | 
(*  Title:      Pure/Isar/instance.ML
  | 
| 
 | 
     2  | 
    ID:         $Id$
  | 
| 
 | 
     3  | 
    Author:     Florian Haftmann, TU Muenchen
  | 
| 
 | 
     4  | 
  | 
| 
25462
 | 
     5  | 
A primitive instance command, based on instantiation target.
  | 
| 
24592
 | 
     6  | 
*)
  | 
| 
 | 
     7  | 
  | 
| 
 | 
     8  | 
signature INSTANCE =
  | 
| 
 | 
     9  | 
sig
  | 
| 
25536
 | 
    10  | 
  val instantiate: string list * sort list * sort -> (local_theory -> 'a * local_theory)
  | 
| 
 | 
    11  | 
    -> (Proof.context -> 'a -> tactic) -> theory -> theory
  | 
| 
25462
 | 
    12  | 
  | 
| 
25536
 | 
    13  | 
  val instantiation_cmd: xstring list * sort * xstring -> theory -> local_theory
  | 
| 
 | 
    14  | 
  val instance_cmd: xstring * sort * xstring -> ((bstring * Attrib.src list) * xstring) list
  | 
| 
25462
 | 
    15  | 
    -> theory -> Proof.state
  | 
| 
24592
 | 
    16  | 
end;
  | 
| 
 | 
    17  | 
  | 
| 
 | 
    18  | 
structure Instance : INSTANCE =
  | 
| 
 | 
    19  | 
struct
  | 
| 
 | 
    20  | 
  | 
| 
25536
 | 
    21  | 
fun read_multi_arity thy (raw_tycos, raw_sorts, raw_sort) =
  | 
| 
 | 
    22  | 
  let
  | 
| 
 | 
    23  | 
    val all_arities = map (fn raw_tyco => Sign.read_arity thy
  | 
| 
 | 
    24  | 
      (raw_tyco, raw_sorts, raw_sort)) raw_tycos;
  | 
| 
 | 
    25  | 
    val tycos = map #1 all_arities;
  | 
| 
 | 
    26  | 
    val (_, sorts, sort) = hd all_arities;
  | 
| 
 | 
    27  | 
  in (tycos, sorts, sort) end;
  | 
| 
 | 
    28  | 
  | 
| 
25462
 | 
    29  | 
fun instantiation_cmd raw_arities thy =
  | 
| 
25536
 | 
    30  | 
  TheoryTarget.instantiation (read_multi_arity thy raw_arities) thy;
  | 
| 
25462
 | 
    31  | 
  | 
| 
 | 
    32  | 
fun instantiate arities f tac =
  | 
| 
 | 
    33  | 
  TheoryTarget.instantiation arities
  | 
| 
 | 
    34  | 
  #> f
  | 
| 
25536
 | 
    35  | 
  #-> (fn result => Class.prove_instantiation_instance (fn ctxt => tac ctxt result))
  | 
| 
25462
 | 
    36  | 
  #> LocalTheory.exit
  | 
| 
 | 
    37  | 
  #> ProofContext.theory_of;
  | 
| 
24592
 | 
    38  | 
  | 
| 
25502
 | 
    39  | 
fun gen_instance prep_arity prep_attr parse_term do_proof do_proof' raw_arities defs thy =
  | 
| 
24592
 | 
    40  | 
  let
  | 
| 
25536
 | 
    41  | 
    val (tyco, sorts, sort) = prep_arity thy raw_arities;
  | 
| 
25462
 | 
    42  | 
    fun export_defs ctxt = 
  | 
| 
 | 
    43  | 
      let
  | 
| 
 | 
    44  | 
        val ctxt_thy = ProofContext.init (ProofContext.theory_of ctxt);
  | 
| 
 | 
    45  | 
      in
  | 
| 
 | 
    46  | 
        map (snd o snd)
  | 
| 
 | 
    47  | 
        #> map (Assumption.export false ctxt ctxt_thy)
  | 
| 
 | 
    48  | 
        #> Variable.export ctxt ctxt_thy
  | 
| 
 | 
    49  | 
      end;
  | 
| 
 | 
    50  | 
    fun mk_def ctxt ((name, raw_attr), raw_t) =
  | 
| 
24592
 | 
    51  | 
      let
  | 
| 
25462
 | 
    52  | 
        val attr = map (prep_attr thy) raw_attr;
  | 
| 
25485
 | 
    53  | 
        val t = parse_term ctxt raw_t;
  | 
| 
25462
 | 
    54  | 
      in (NONE, ((name, attr), t)) end;
  | 
| 
25485
 | 
    55  | 
    fun define def ctxt =
  | 
| 
 | 
    56  | 
      let
  | 
| 
 | 
    57  | 
        val def' = (apsnd o apsnd) (Syntax.check_prop ctxt) def;
  | 
| 
 | 
    58  | 
      in Specification.definition def' ctxt end;
  | 
| 
25536
 | 
    59  | 
  in if not (null defs) orelse forall (Class.is_class thy) sort
  | 
| 
25502
 | 
    60  | 
  then
  | 
| 
25462
 | 
    61  | 
    thy
  | 
| 
25536
 | 
    62  | 
    |> TheoryTarget.instantiation ([tyco], sorts, sort)
  | 
| 
25462
 | 
    63  | 
    |> `(fn ctxt => map (mk_def ctxt) defs)
  | 
| 
 | 
    64  | 
    |-> (fn defs => fold_map Specification.definition defs)
  | 
| 
 | 
    65  | 
    |-> (fn defs => `(fn ctxt => export_defs ctxt defs))
  | 
| 
25514
 | 
    66  | 
    ||> LocalTheory.reinit
  | 
| 
25502
 | 
    67  | 
    |-> (fn defs => do_proof defs)
  | 
| 
 | 
    68  | 
  else
  | 
| 
 | 
    69  | 
    thy
  | 
| 
25536
 | 
    70  | 
    |> do_proof' (tyco, sorts, sort)
  | 
| 
25462
 | 
    71  | 
  end;
  | 
| 
24592
 | 
    72  | 
  | 
| 
25485
 | 
    73  | 
val instance_cmd = gen_instance Sign.read_arity Attrib.intern_src Syntax.parse_prop
  | 
| 
25502
 | 
    74  | 
  (fn _ => Class.instantiation_instance Class.conclude_instantiation) (Class.instance_arity I);
  | 
| 
24592
 | 
    75  | 
  | 
| 
 | 
    76  | 
end;
  |