src/Pure/Tools/invoke.ML
author wenzelm
Wed, 07 Jun 2006 02:01:36 +0200
changeset 19813 b051be997d50
child 19849 d96a15bb2d88
permissions -rw-r--r--
Schematic invocation of locale expression in proof context.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19813
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Tools/invoke.ML
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
     4
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
     5
Schematic invocation of locale expression in proof context.
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
     6
*)
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
     7
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
     8
signature INVOKE =
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
     9
sig
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    10
  val invoke: string * Attrib.src list -> Locale.expr -> string option list -> bool ->
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    11
    Proof.state -> Proof.state
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    12
  val invoke_i: string * attribute list -> Locale.expr -> term option list -> bool ->
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    13
    Proof.state -> Proof.state
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    14
end;
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    15
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    16
structure Invoke: INVOKE =
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    17
struct
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    18
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    19
(* invoke *)
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    20
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    21
local
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    22
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    23
fun gen_invoke prep_att prep_expr prep_terms
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    24
    (prfx, raw_atts) raw_expr raw_insts int state =
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    25
  let
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    26
    val _ = Proof.assert_forward_or_chain state;
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    27
    val thy = Proof.theory_of state;
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    28
    val ctxt = Proof.context_of state;
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    29
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    30
    val atts = map (prep_att thy) raw_atts;
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    31
    val (elems, _) = prep_expr raw_expr [] (ProofContext.init thy);
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    32
    val xs = maps Element.params_of elems;
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    33
    val As = maps Element.prems_of elems;
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    34
    val xs' = map (Logic.varify o Free) xs;
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    35
    val As' = map Logic.varify As;
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    36
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    37
    val raw_insts' = zip_options xs' raw_insts
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    38
      handle Library.UnequalLengths => error "Too many instantiations";
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    39
    val insts = map #1 raw_insts' ~~
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    40
      prep_terms ctxt (map (fn (t, u) => (u, Term.fastype_of t)) raw_insts');
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    41
    val inst_rules = xs' |> map (fn t =>
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    42
      (case AList.lookup (op =) insts t of
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    43
        SOME u => Drule.mk_term (Thm.cterm_of thy u)
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    44
      | NONE => Drule.termI));
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    45
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    46
    val propp =
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    47
      [(("", []), map (rpair [] o Logic.mk_term) xs'),
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    48
       (("", []), map (rpair [] o Element.mark_witness) As')];
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    49
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    50
    fun after_qed [insts, results] =
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    51
      Proof.put_facts NONE
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    52
      #> Seq.single;
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    53
  in
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    54
    state
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    55
    |> Proof.local_goal (ProofDisplay.print_results int) (K I) ProofContext.bind_propp_schematic_i
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    56
      "invoke" NONE after_qed propp
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    57
    |> Element.refine_witness
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    58
    |> Seq.hd
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    59
    |> Proof.refine (Method.Basic (K (Method.METHOD (K (HEADGOAL (RANGE (map rtac inst_rules)))))))
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    60
    |> Seq.hd
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    61
  end;
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    62
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    63
(* FIXME *)
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    64
fun read_terms ctxt args =
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    65
  #1 (ProofContext.read_termTs ctxt (K false) (K NONE) (K NONE) [] args)
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    66
  |> ProofContext.polymorphic ctxt;
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    67
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    68
(* FIXME *)
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    69
fun cert_terms ctxt args = map fst args;
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    70
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    71
in
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    72
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    73
fun invoke x = gen_invoke Attrib.attribute Locale.read_expr read_terms x;
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    74
fun invoke_i x = gen_invoke (K I) Locale.cert_expr cert_terms x;
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    75
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    76
end;
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    77
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    78
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    79
(* concrete syntax *)
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    80
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    81
local structure P = OuterParse and K = OuterKeyword in
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    82
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    83
val invokeP =
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    84
  OuterSyntax.command "invoke"
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    85
    "schematic invocation of locale expression in proof context"
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    86
    (K.tag_proof K.prf_goal)
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    87
    (P.opt_thm_name ":" -- P.locale_expr -- P.locale_insts >> (fn ((x, y), z) =>
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    88
      Toplevel.print o Toplevel.proof' (invoke x y z)));
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    89
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    90
val _ = OuterSyntax.add_parsers [invokeP];
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    91
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    92
end;
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    93
b051be997d50 Schematic invocation of locale expression in proof context.
wenzelm
parents:
diff changeset
    94
end;