src/Pure/Isar/rule_insts.ML
author wenzelm
Thu Aug 03 17:30:44 2006 +0200 (2006-08-03)
changeset 20336 aac494583949
child 20343 e093a54bf25e
permissions -rw-r--r--
Rule instantiations -- operations within a rule/subgoal context.
wenzelm@20336
     1
(*  Title:      Pure/Isar/rule_insts.ML
wenzelm@20336
     2
    ID:         $Id$
wenzelm@20336
     3
    Author:     Makarius
wenzelm@20336
     4
wenzelm@20336
     5
Rule instantiations -- operations within a rule/subgoal context.
wenzelm@20336
     6
*)
wenzelm@20336
     7
wenzelm@20336
     8
signature RULE_INSTS =
wenzelm@20336
     9
sig
wenzelm@20336
    10
  val bires_inst_tac: bool -> Proof.context -> (indexname * string) list ->
wenzelm@20336
    11
    thm -> int -> tactic
wenzelm@20336
    12
end;
wenzelm@20336
    13
wenzelm@20336
    14
structure RuleInsts: RULE_INSTS =
wenzelm@20336
    15
struct
wenzelm@20336
    16
wenzelm@20336
    17
wenzelm@20336
    18
(** attributes **)
wenzelm@20336
    19
wenzelm@20336
    20
(* read_instantiate: named instantiation of type and term variables *)
wenzelm@20336
    21
wenzelm@20336
    22
local
wenzelm@20336
    23
wenzelm@20336
    24
fun is_tvar (x, _) = (case Symbol.explode x of "'" :: _ => true | _ => false);
wenzelm@20336
    25
wenzelm@20336
    26
fun error_var msg xi = error (msg ^ Syntax.string_of_vname xi);
wenzelm@20336
    27
wenzelm@20336
    28
fun the_sort sorts xi = the (sorts xi)
wenzelm@20336
    29
  handle Option.Option => error_var "No such type variable in theorem: " xi;
wenzelm@20336
    30
wenzelm@20336
    31
fun the_type types xi = the (types xi)
wenzelm@20336
    32
  handle Option.Option => error_var "No such variable in theorem: " xi;
wenzelm@20336
    33
wenzelm@20336
    34
fun unify_types thy types (xi, u) (unifier, maxidx) =
wenzelm@20336
    35
  let
wenzelm@20336
    36
    val T = the_type types xi;
wenzelm@20336
    37
    val U = Term.fastype_of u;
wenzelm@20336
    38
    val maxidx' = Int.max (maxidx, Int.max (#2 xi, Term.maxidx_of_term u));
wenzelm@20336
    39
  in
wenzelm@20336
    40
    Sign.typ_unify thy (T, U) (unifier, maxidx')
wenzelm@20336
    41
      handle Type.TUNIFY => error_var "Incompatible type for instantiation of " xi
wenzelm@20336
    42
  end;
wenzelm@20336
    43
wenzelm@20336
    44
fun typ_subst env = apsnd (Term.typ_subst_TVars env);
wenzelm@20336
    45
fun subst env = apsnd (Term.subst_TVars env);
wenzelm@20336
    46
wenzelm@20336
    47
fun instantiate thy envT env thm =
wenzelm@20336
    48
  let
wenzelm@20336
    49
    val (_, sorts) = Drule.types_sorts thm;
wenzelm@20336
    50
    fun prepT (a, T) = (Thm.ctyp_of thy (TVar (a, the_sort sorts a)), Thm.ctyp_of thy T);
wenzelm@20336
    51
    fun prep (xi, t) = pairself (Thm.cterm_of thy) (Var (xi, Term.fastype_of t), t);
wenzelm@20336
    52
  in
wenzelm@20336
    53
    Drule.instantiate (map prepT (distinct (op =) envT),
wenzelm@20336
    54
      map prep (distinct (fn ((xi, t), (yj, u)) => xi = yj andalso t aconv u) env)) thm
wenzelm@20336
    55
  end;
wenzelm@20336
    56
wenzelm@20336
    57
in
wenzelm@20336
    58
wenzelm@20336
    59
fun read_instantiate mixed_insts (context, thm) =
wenzelm@20336
    60
  let
wenzelm@20336
    61
    val thy = Context.theory_of context;
wenzelm@20336
    62
    val ctxt = Context.proof_of context;
wenzelm@20336
    63
wenzelm@20336
    64
    val (type_insts, term_insts) = List.partition (is_tvar o fst) (map snd mixed_insts);
wenzelm@20336
    65
    val internal_insts = term_insts |> map_filter
wenzelm@20336
    66
      (fn (xi, Args.Term t) => SOME (xi, t)
wenzelm@20336
    67
      | (_, Args.Name _) => NONE
wenzelm@20336
    68
      | (xi, _) => error_var "Term argument expected for " xi);
wenzelm@20336
    69
    val external_insts = term_insts |> map_filter
wenzelm@20336
    70
      (fn (xi, Args.Name s) => SOME (xi, s) | _ => NONE);
wenzelm@20336
    71
wenzelm@20336
    72
wenzelm@20336
    73
    (* type instantiations *)
wenzelm@20336
    74
wenzelm@20336
    75
    val sorts = #2 (Drule.types_sorts thm);
wenzelm@20336
    76
wenzelm@20336
    77
    fun readT (xi, arg) =
wenzelm@20336
    78
      let
wenzelm@20336
    79
        val S = the_sort sorts xi;
wenzelm@20336
    80
        val T =
wenzelm@20336
    81
          (case arg of
wenzelm@20336
    82
            Args.Name s => ProofContext.read_typ ctxt s
wenzelm@20336
    83
          | Args.Typ T => T
wenzelm@20336
    84
          | _ => error_var "Type argument expected for " xi);
wenzelm@20336
    85
      in
wenzelm@20336
    86
        if Sign.of_sort thy (T, S) then (xi, T)
wenzelm@20336
    87
        else error_var "Incompatible sort for typ instantiation of " xi
wenzelm@20336
    88
      end;
wenzelm@20336
    89
wenzelm@20336
    90
    val type_insts' = map readT type_insts;
wenzelm@20336
    91
    val thm' = instantiate thy type_insts' [] thm;
wenzelm@20336
    92
wenzelm@20336
    93
wenzelm@20336
    94
    (* internal term instantiations *)
wenzelm@20336
    95
wenzelm@20336
    96
    val types' = #1 (Drule.types_sorts thm');
wenzelm@20336
    97
    val unifier = map (apsnd snd) (Vartab.dest (#1
wenzelm@20336
    98
      (fold (unify_types thy types') internal_insts (Vartab.empty, 0))));
wenzelm@20336
    99
wenzelm@20336
   100
    val type_insts'' = map (typ_subst unifier) type_insts';
wenzelm@20336
   101
    val internal_insts'' = map (subst unifier) internal_insts;
wenzelm@20336
   102
    val thm'' = instantiate thy unifier internal_insts'' thm';
wenzelm@20336
   103
wenzelm@20336
   104
wenzelm@20336
   105
    (* external term instantiations *)
wenzelm@20336
   106
wenzelm@20336
   107
    val types'' = #1 (Drule.types_sorts thm'');
wenzelm@20336
   108
wenzelm@20336
   109
    val (xs, ss) = split_list external_insts;
wenzelm@20336
   110
    val Ts = map (the_type types'') xs;
wenzelm@20336
   111
    val (ts, inferred) = ProofContext.read_termTs ctxt (K false)
wenzelm@20336
   112
        (K NONE) (K NONE) (Drule.add_used thm'' []) (ss ~~ Ts);
wenzelm@20336
   113
wenzelm@20336
   114
    val type_insts''' = map (typ_subst inferred) type_insts'';
wenzelm@20336
   115
    val internal_insts''' = map (subst inferred) internal_insts'';
wenzelm@20336
   116
wenzelm@20336
   117
    val external_insts''' = xs ~~ ts;
wenzelm@20336
   118
    val term_insts''' = internal_insts''' @ external_insts''';
wenzelm@20336
   119
    val thm''' = instantiate thy inferred external_insts''' thm'';
wenzelm@20336
   120
wenzelm@20336
   121
wenzelm@20336
   122
    (* assign internalized values *)
wenzelm@20336
   123
wenzelm@20336
   124
    val _ =
wenzelm@20336
   125
      mixed_insts |> List.app (fn (arg, (xi, _)) =>
wenzelm@20336
   126
        if is_tvar xi then
wenzelm@20336
   127
          Args.assign (SOME (Args.Typ (the (AList.lookup (op =) type_insts''' xi)))) arg
wenzelm@20336
   128
        else
wenzelm@20336
   129
          Args.assign (SOME (Args.Term (the (AList.lookup (op =) term_insts''' xi)))) arg);
wenzelm@20336
   130
wenzelm@20336
   131
  in (context, thm''' |> RuleCases.save thm) end;
wenzelm@20336
   132
wenzelm@20336
   133
end;
wenzelm@20336
   134
wenzelm@20336
   135
wenzelm@20336
   136
(* where: named instantiation *)
wenzelm@20336
   137
wenzelm@20336
   138
local
wenzelm@20336
   139
wenzelm@20336
   140
val value =
wenzelm@20336
   141
  Args.internal_typ >> Args.Typ ||
wenzelm@20336
   142
  Args.internal_term >> Args.Term ||
wenzelm@20336
   143
  Args.name >> Args.Name;
wenzelm@20336
   144
wenzelm@20336
   145
val inst = Args.var -- (Args.$$$ "=" |-- Args.ahead -- value)
wenzelm@20336
   146
  >> (fn (xi, (a, v)) => (a, (xi, v)));
wenzelm@20336
   147
wenzelm@20336
   148
in
wenzelm@20336
   149
wenzelm@20336
   150
val where_att = Attrib.syntax (Args.and_list (Scan.lift inst) >> read_instantiate);
wenzelm@20336
   151
wenzelm@20336
   152
end;
wenzelm@20336
   153
wenzelm@20336
   154
wenzelm@20336
   155
(* of: positional instantiation (term arguments only) *)
wenzelm@20336
   156
wenzelm@20336
   157
local
wenzelm@20336
   158
wenzelm@20336
   159
fun read_instantiate' (args, concl_args) (context, thm) =
wenzelm@20336
   160
  let
wenzelm@20336
   161
    fun zip_vars _ [] = []
wenzelm@20336
   162
      | zip_vars (_ :: xs) ((_, NONE) :: rest) = zip_vars xs rest
wenzelm@20336
   163
      | zip_vars ((x, _) :: xs) ((arg, SOME t) :: rest) = (arg, (x, t)) :: zip_vars xs rest
wenzelm@20336
   164
      | zip_vars [] _ = error "More instantiations than variables in theorem";
wenzelm@20336
   165
    val insts =
wenzelm@20336
   166
      zip_vars (rev (Term.add_vars (Thm.full_prop_of thm) [])) args @
wenzelm@20336
   167
      zip_vars (rev (Term.add_vars (Thm.concl_of thm) [])) concl_args;
wenzelm@20336
   168
  in read_instantiate insts (context, thm) end;
wenzelm@20336
   169
wenzelm@20336
   170
val value =
wenzelm@20336
   171
  Args.internal_term >> Args.Term ||
wenzelm@20336
   172
  Args.name >> Args.Name;
wenzelm@20336
   173
wenzelm@20336
   174
val inst = Args.ahead -- Args.maybe value;
wenzelm@20336
   175
val concl = Args.$$$ "concl" -- Args.colon;
wenzelm@20336
   176
wenzelm@20336
   177
val insts =
wenzelm@20336
   178
  Scan.repeat (Scan.unless concl inst) --
wenzelm@20336
   179
  Scan.optional (concl |-- Scan.repeat inst) [];
wenzelm@20336
   180
wenzelm@20336
   181
in
wenzelm@20336
   182
wenzelm@20336
   183
val of_att = Attrib.syntax (Scan.lift insts >> read_instantiate');
wenzelm@20336
   184
wenzelm@20336
   185
end;
wenzelm@20336
   186
wenzelm@20336
   187
wenzelm@20336
   188
(* setup *)
wenzelm@20336
   189
wenzelm@20336
   190
val _ = Context.add_setup (Attrib.add_attributes
wenzelm@20336
   191
 [("where", where_att, "named instantiation of theorem"),
wenzelm@20336
   192
  ("of", of_att, "rule applied to terms")]);
wenzelm@20336
   193
wenzelm@20336
   194
wenzelm@20336
   195
wenzelm@20336
   196
(** methods **)
wenzelm@20336
   197
wenzelm@20336
   198
(* rule_tac etc. -- refer to dynamic goal state!! *)   (* FIXME cleanup!! *)
wenzelm@20336
   199
wenzelm@20336
   200
fun bires_inst_tac bires_flag ctxt insts thm =
wenzelm@20336
   201
  let
wenzelm@20336
   202
    val thy = ProofContext.theory_of ctxt;
wenzelm@20336
   203
    (* Separate type and term insts *)
wenzelm@20336
   204
    fun has_type_var ((x, _), _) = (case Symbol.explode x of
wenzelm@20336
   205
          "'"::cs => true | cs => false);
wenzelm@20336
   206
    val Tinsts = List.filter has_type_var insts;
wenzelm@20336
   207
    val tinsts = filter_out has_type_var insts;
wenzelm@20336
   208
    (* Tactic *)
wenzelm@20336
   209
    fun tac i st =
wenzelm@20336
   210
      let
wenzelm@20336
   211
        (* Preprocess state: extract environment information:
wenzelm@20336
   212
           - variables and their types
wenzelm@20336
   213
           - type variables and their sorts
wenzelm@20336
   214
           - parameters and their types *)
wenzelm@20336
   215
        val (types, sorts) = types_sorts st;
wenzelm@20336
   216
    (* Process type insts: Tinsts_env *)
wenzelm@20336
   217
    fun absent xi = error
wenzelm@20336
   218
          ("No such variable in theorem: " ^ Syntax.string_of_vname xi);
wenzelm@20336
   219
    val (rtypes, rsorts) = types_sorts thm;
wenzelm@20336
   220
    fun readT (xi, s) =
wenzelm@20336
   221
        let val S = case rsorts xi of SOME S => S | NONE => absent xi;
wenzelm@20336
   222
            val T = Sign.read_typ (thy, sorts) s;
wenzelm@20336
   223
            val U = TVar (xi, S);
wenzelm@20336
   224
        in if Sign.typ_instance thy (T, U) then (U, T)
wenzelm@20336
   225
           else error
wenzelm@20336
   226
             ("Instantiation of " ^ Syntax.string_of_vname xi ^ " fails")
wenzelm@20336
   227
        end;
wenzelm@20336
   228
    val Tinsts_env = map readT Tinsts;
wenzelm@20336
   229
    (* Preprocess rule: extract vars and their types, apply Tinsts *)
wenzelm@20336
   230
    fun get_typ xi =
wenzelm@20336
   231
      (case rtypes xi of
wenzelm@20336
   232
           SOME T => typ_subst_atomic Tinsts_env T
wenzelm@20336
   233
         | NONE => absent xi);
wenzelm@20336
   234
    val (xis, ss) = Library.split_list tinsts;
wenzelm@20336
   235
    val Ts = map get_typ xis;
wenzelm@20336
   236
        val (_, _, Bi, _) = dest_state(st,i)
wenzelm@20336
   237
        val params = Logic.strip_params Bi
wenzelm@20336
   238
                             (* params of subgoal i as string typ pairs *)
wenzelm@20336
   239
        val params = rev(Term.rename_wrt_term Bi params)
wenzelm@20336
   240
                           (* as they are printed: bound variables with *)
wenzelm@20336
   241
                           (* the same name are renamed during printing *)
wenzelm@20336
   242
        fun types' (a, ~1) = (case AList.lookup (op =) params a of
wenzelm@20336
   243
                NONE => types (a, ~1)
wenzelm@20336
   244
              | some => some)
wenzelm@20336
   245
          | types' xi = types xi;
wenzelm@20336
   246
        fun internal x = is_some (types' (x, ~1));
wenzelm@20336
   247
        val used = Drule.add_used thm (Drule.add_used st []);
wenzelm@20336
   248
        val (ts, envT) =
wenzelm@20336
   249
          ProofContext.read_termTs_schematic ctxt internal types' sorts used (ss ~~ Ts);
wenzelm@20336
   250
        val envT' = map (fn (ixn, T) =>
wenzelm@20336
   251
          (TVar (ixn, the (rsorts ixn)), T)) envT @ Tinsts_env;
wenzelm@20336
   252
        val cenv =
wenzelm@20336
   253
          map
wenzelm@20336
   254
            (fn (xi, t) =>
wenzelm@20336
   255
              pairself (Thm.cterm_of thy) (Var (xi, fastype_of t), t))
wenzelm@20336
   256
            (distinct
wenzelm@20336
   257
              (fn ((x1, t1), (x2, t2)) => x1 = x2 andalso t1 aconv t2)
wenzelm@20336
   258
              (xis ~~ ts));
wenzelm@20336
   259
        (* Lift and instantiate rule *)
wenzelm@20336
   260
        val {maxidx, ...} = rep_thm st;
wenzelm@20336
   261
        val paramTs = map #2 params
wenzelm@20336
   262
        and inc = maxidx+1
wenzelm@20336
   263
        fun liftvar (Var ((a,j), T)) =
wenzelm@20336
   264
              Var((a, j+inc), paramTs ---> Logic.incr_tvar inc T)
wenzelm@20336
   265
          | liftvar t = raise TERM("Variable expected", [t]);
wenzelm@20336
   266
        fun liftterm t = list_abs_free
wenzelm@20336
   267
              (params, Logic.incr_indexes(paramTs,inc) t)
wenzelm@20336
   268
        fun liftpair (cv,ct) =
wenzelm@20336
   269
              (cterm_fun liftvar cv, cterm_fun liftterm ct)
wenzelm@20336
   270
        val lifttvar = pairself (ctyp_of thy o Logic.incr_tvar inc);
wenzelm@20336
   271
        val rule = Drule.instantiate
wenzelm@20336
   272
              (map lifttvar envT', map liftpair cenv)
wenzelm@20336
   273
              (Thm.lift_rule (Thm.cprem_of st i) thm)
wenzelm@20336
   274
      in
wenzelm@20336
   275
        if i > nprems_of st then no_tac st
wenzelm@20336
   276
        else st |>
wenzelm@20336
   277
          compose_tac (bires_flag, rule, nprems_of thm) i
wenzelm@20336
   278
      end
wenzelm@20336
   279
           handle TERM (msg,_)   => (warning msg; no_tac st)
wenzelm@20336
   280
                | THM  (msg,_,_) => (warning msg; no_tac st);
wenzelm@20336
   281
  in tac end;
wenzelm@20336
   282
wenzelm@20336
   283
local
wenzelm@20336
   284
wenzelm@20336
   285
fun gen_inst _ tac _ (quant, ([], thms)) =
wenzelm@20336
   286
      Method.METHOD (fn facts => quant (Method.insert_tac facts THEN' tac thms))
wenzelm@20336
   287
  | gen_inst inst_tac _ ctxt (quant, (insts, [thm])) =
wenzelm@20336
   288
      Method.METHOD (fn facts =>
wenzelm@20336
   289
        quant (Method.insert_tac facts THEN' inst_tac ctxt insts thm))
wenzelm@20336
   290
  | gen_inst _ _ _ _ = error "Cannot have instantiations with multiple rules";
wenzelm@20336
   291
wenzelm@20336
   292
in
wenzelm@20336
   293
wenzelm@20336
   294
val res_inst_meth = gen_inst (bires_inst_tac false) Tactic.resolve_tac;
wenzelm@20336
   295
wenzelm@20336
   296
val eres_inst_meth = gen_inst (bires_inst_tac true) Tactic.eresolve_tac;
wenzelm@20336
   297
wenzelm@20336
   298
val cut_inst_meth =
wenzelm@20336
   299
  gen_inst
wenzelm@20336
   300
    (fn ctxt => fn insts => bires_inst_tac false ctxt insts o Tactic.make_elim_preserve)
wenzelm@20336
   301
    Tactic.cut_rules_tac;
wenzelm@20336
   302
wenzelm@20336
   303
val dres_inst_meth =
wenzelm@20336
   304
  gen_inst
wenzelm@20336
   305
    (fn ctxt => fn insts => bires_inst_tac true ctxt insts o Tactic.make_elim_preserve)
wenzelm@20336
   306
    Tactic.dresolve_tac;
wenzelm@20336
   307
wenzelm@20336
   308
val forw_inst_meth =
wenzelm@20336
   309
  gen_inst
wenzelm@20336
   310
    (fn ctxt => fn insts => fn rule =>
wenzelm@20336
   311
       bires_inst_tac false ctxt insts (Tactic.make_elim_preserve rule) THEN'
wenzelm@20336
   312
       assume_tac)
wenzelm@20336
   313
    Tactic.forward_tac;
wenzelm@20336
   314
wenzelm@20336
   315
fun subgoal_tac ctxt sprop =
wenzelm@20336
   316
  DETERM o bires_inst_tac false ctxt [(("psi", 0), sprop)] cut_rl;
wenzelm@20336
   317
wenzelm@20336
   318
fun subgoals_tac ctxt sprops = EVERY' (map (subgoal_tac ctxt) sprops);
wenzelm@20336
   319
wenzelm@20336
   320
fun thin_tac ctxt s =
wenzelm@20336
   321
  bires_inst_tac true ctxt [(("V", 0), s)] thin_rl;
wenzelm@20336
   322
wenzelm@20336
   323
wenzelm@20336
   324
(* method syntax *)
wenzelm@20336
   325
wenzelm@20336
   326
val insts =
wenzelm@20336
   327
  Scan.optional
wenzelm@20336
   328
    (Args.enum1 "and" (Scan.lift (Args.name -- (Args.$$$ "=" |-- Args.!!! Args.name))) --|
wenzelm@20336
   329
      Scan.lift (Args.$$$ "in")) [] -- Attrib.thms;
wenzelm@20336
   330
wenzelm@20336
   331
fun inst_args f src ctxt =
wenzelm@20336
   332
  f ctxt (#2 (Method.syntax (Args.goal_spec HEADGOAL -- insts) src ctxt));
wenzelm@20336
   333
wenzelm@20336
   334
val insts_var =
wenzelm@20336
   335
  Scan.optional
wenzelm@20336
   336
    (Args.enum1 "and" (Scan.lift (Args.var -- (Args.$$$ "=" |-- Args.!!! Args.name))) --|
wenzelm@20336
   337
      Scan.lift (Args.$$$ "in")) [] -- Attrib.thms;
wenzelm@20336
   338
wenzelm@20336
   339
fun inst_args_var f src ctxt =
wenzelm@20336
   340
  f ctxt (#2 (Method.syntax (Args.goal_spec HEADGOAL -- insts_var) src ctxt));
wenzelm@20336
   341
wenzelm@20336
   342
wenzelm@20336
   343
(* setup *)
wenzelm@20336
   344
wenzelm@20336
   345
val _ = Context.add_setup (Method.add_methods
wenzelm@20336
   346
 [("rule_tac", inst_args_var res_inst_meth,
wenzelm@20336
   347
    "apply rule (dynamic instantiation)"),
wenzelm@20336
   348
  ("erule_tac", inst_args_var eres_inst_meth,
wenzelm@20336
   349
    "apply rule in elimination manner (dynamic instantiation)"),
wenzelm@20336
   350
  ("drule_tac", inst_args_var dres_inst_meth,
wenzelm@20336
   351
    "apply rule in destruct manner (dynamic instantiation)"),
wenzelm@20336
   352
  ("frule_tac", inst_args_var forw_inst_meth,
wenzelm@20336
   353
    "apply rule in forward manner (dynamic instantiation)"),
wenzelm@20336
   354
  ("cut_tac", inst_args_var cut_inst_meth,
wenzelm@20336
   355
    "cut rule (dynamic instantiation)"),
wenzelm@20336
   356
  ("subgoal_tac", Method.goal_args_ctxt (Scan.repeat1 Args.name) subgoals_tac,
wenzelm@20336
   357
    "insert subgoal (dynamic instantiation)"),
wenzelm@20336
   358
  ("thin_tac", Method.goal_args_ctxt Args.name thin_tac,
wenzelm@20336
   359
    "remove premise (dynamic instantiation)")]);
wenzelm@20336
   360
wenzelm@20336
   361
end;
wenzelm@20336
   362
wenzelm@20336
   363
end;