src/HOL/Eisbach/match_method.ML
author wenzelm
Mon, 12 Apr 2021 14:14:47 +0200
changeset 73563 55b66a45bc94
parent 73553 b35ef8162807
child 74152 069f6b2c5a07
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
     1
(*  Title:      HOL/Eisbach/match_method.ML
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
     2
    Author:     Daniel Matichuk, NICTA/UNSW
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
     3
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
     4
Setup for "match" proof method. It provides basic fact/term matching in
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
     5
addition to premise/conclusion matching through Subgoal.focus, and binds
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
     6
fact names from matches as well as term patterns within matches.
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
     7
*)
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
     8
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
     9
signature MATCH_METHOD =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    10
sig
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    11
  val focus_schematics: Proof.context -> Envir.tenv
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    12
  val focus_params: Proof.context -> term list
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    13
  (* FIXME proper ML interface for the main thing *)
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    14
end
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    15
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    16
structure Match_Method : MATCH_METHOD =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    17
struct
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    18
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
    19
(* Filter premises by predicate, with premise order; recovers premise order afterwards.*)
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
    20
fun filter_prems_tac' ctxt prem =
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    21
  let
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    22
    fun Then NONE tac = SOME tac
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    23
      | Then (SOME tac) tac' = SOME (tac THEN' tac');
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
    24
    fun thins idxH (tac, n, i) =
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
    25
       if prem idxH then (tac, n + 1 , i)
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
    26
       else (Then tac (rotate_tac n THEN' eresolve_tac ctxt [thin_rl]), 0, i + n);
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    27
  in
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    28
    SUBGOAL (fn (goal, i) =>
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
    29
      let val idxHs = tag_list 0 (Logic.strip_assums_hyp goal) in
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
    30
        (case fold thins idxHs (NONE, 0, 0) of
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
    31
          (NONE, _, _) => no_tac
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
    32
        | (SOME tac, _, n) => tac i THEN rotate_tac (~ n) i)
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    33
      end)
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    34
  end;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    35
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    36
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    37
datatype match_kind =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    38
    Match_Term of term Item_Net.T
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    39
  | Match_Fact of thm Item_Net.T
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    40
  | Match_Concl
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
    41
  | Match_Prems of bool;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    42
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    43
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    44
val aconv_net = Item_Net.init (op aconv) single;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    45
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    46
val parse_match_kind =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 63615
diff changeset
    47
  Scan.lift \<^keyword>\<open>conclusion\<close> >> K Match_Concl ||
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 63615
diff changeset
    48
  Scan.lift (\<^keyword>\<open>premises\<close> |-- Args.mode "local") >> Match_Prems ||
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 63615
diff changeset
    49
  Scan.lift (\<^keyword>\<open>(\<close>) |-- Args.term --| Scan.lift (\<^keyword>\<open>)\<close>) >>
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    50
    (fn t => Match_Term (Item_Net.update t aconv_net)) ||
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    51
  Attrib.thms >> (fn thms => Match_Fact (fold Item_Net.update thms Thm.full_rules));
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    52
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    53
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
    54
fun nameable_match m = (case m of Match_Fact _ => true | Match_Prems _ => true | _ => false);
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    55
fun prop_match m = (case m of Match_Term _ => false | _ => true);
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    56
63185
08369e33c185 apply current morphism to method text before evaluating;
matichuk <daniel.matichuk@nicta.com.au>
parents: 62165
diff changeset
    57
val bound_thm : (thm, binding) Parse_Tools.parse_val parser =
08369e33c185 apply current morphism to method text before evaluating;
matichuk <daniel.matichuk@nicta.com.au>
parents: 62165
diff changeset
    58
  Parse_Tools.parse_thm_val Parse.binding;
08369e33c185 apply current morphism to method text before evaluating;
matichuk <daniel.matichuk@nicta.com.au>
parents: 62165
diff changeset
    59
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    60
val bound_term : (term, binding) Parse_Tools.parse_val parser =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    61
  Parse_Tools.parse_term_val Parse.binding;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    62
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    63
val fixes =
60287
adde5ce1e0a7 updated Eisbach, using version 134bc592909c of its Bitbucket repository;
wenzelm
parents: 60285
diff changeset
    64
  Parse.and_list1 (Scan.repeat1 (Parse.position bound_term) --
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 63615
diff changeset
    65
    Scan.option (\<^keyword>\<open>::\<close> |-- Parse.!!! Parse.typ)
60287
adde5ce1e0a7 updated Eisbach, using version 134bc592909c of its Bitbucket repository;
wenzelm
parents: 60285
diff changeset
    66
    >> (fn (xs, T) => map (fn (x, pos) => ((x, T), pos)) xs)) >> flat;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    67
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 63615
diff changeset
    68
val for_fixes = Scan.optional (\<^keyword>\<open>for\<close> |-- fixes) [];
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    69
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
    70
fun pos_of dyn = Parse_Tools.the_parse_val dyn |> Binding.pos_of;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    71
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    72
(*FIXME: Dynamic facts modify the background theory, so we have to resort
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    73
  to token replacement for matched facts. *)
61814
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
    74
val dynamic_fact =
63185
08369e33c185 apply current morphism to method text before evaluating;
matichuk <daniel.matichuk@nicta.com.au>
parents: 62165
diff changeset
    75
  Scan.lift bound_thm -- Attrib.opt_attribs;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    76
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
    77
type match_args = {multi : bool, cut : int};
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    78
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    79
val parse_match_args =
61835
wenzelm
parents: 61834
diff changeset
    80
  Scan.optional
wenzelm
parents: 61834
diff changeset
    81
    (Args.parens
wenzelm
parents: 61834
diff changeset
    82
      (Parse.enum1 ","
wenzelm
parents: 61834
diff changeset
    83
        (Args.$$$ "multi" >> (fn _ => fn {cut, ...} => {multi = true, cut = cut}) ||
wenzelm
parents: 61834
diff changeset
    84
         Args.$$$ "cut" |-- Scan.optional Parse.nat 1
wenzelm
parents: 61834
diff changeset
    85
          >> (fn n => fn {multi, ...} => {multi = multi, cut = n})))) []
wenzelm
parents: 61834
diff changeset
    86
  >> (fn fs => fold I fs {multi = false, cut = ~1});
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    87
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
    88
fun parse_named_pats match_kind =
61814
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
    89
  Args.context --
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
    90
  Parse.and_list1'
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
    91
    (Scan.option (dynamic_fact --| Scan.lift Args.colon) :--
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
    92
      (fn opt_dyn =>
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
    93
        if is_none opt_dyn orelse nameable_match match_kind
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
    94
        then Scan.lift (Parse_Tools.name_term -- parse_match_args)
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
    95
        else
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
    96
          let val b = #1 (the opt_dyn)
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
    97
          in error ("Cannot bind fact name in term match" ^ Position.here (pos_of b)) end)) --
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 63615
diff changeset
    98
  Scan.lift (for_fixes -- (\<^keyword>\<open>\<Rightarrow>\<close> |-- Parse.token Parse.text))
61912
ad710de5c576 more standard nesting of sub-language: Parse.text allows atomic entities without quotes;
wenzelm
parents: 61852
diff changeset
    99
  >> (fn ((ctxt, ts), (fixes, body)) =>
ad710de5c576 more standard nesting of sub-language: Parse.text allows atomic entities without quotes;
wenzelm
parents: 61852
diff changeset
   100
    (case Token.get_value body of
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   101
      SOME (Token.Source src) =>
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   102
        let
63527
59eff6e56d81 moved method "use" to Pure;
wenzelm
parents: 63185
diff changeset
   103
          val text = Method.read ctxt src;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   104
          val ts' =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   105
            map
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   106
              (fn (b, (Parse_Tools.Real_Val v, match_args)) =>
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   107
                ((Option.map (fn (b, att) =>
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   108
                  (Parse_Tools.the_real_val b, att)) b, match_args), v)
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   109
                | _ => raise Fail "Expected closed term") ts
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   110
          val fixes' = map (fn ((p, _), _) => Parse_Tools.the_real_val p) fixes
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   111
        in (ts', fixes', text) end
61818
6de8e7ad95c3 more direct use of Token.src as token list;
wenzelm
parents: 61814
diff changeset
   112
    | _ =>
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   113
        let
60469
d1ea37df7358 tuned signature;
wenzelm
parents: 60407
diff changeset
   114
          val (fix_names, ctxt3) = ctxt
d1ea37df7358 tuned signature;
wenzelm
parents: 60407
diff changeset
   115
            |> Proof_Context.add_fixes_cmd
d1ea37df7358 tuned signature;
wenzelm
parents: 60407
diff changeset
   116
              (map (fn ((pb, otyp), _) => (Parse_Tools.the_parse_val pb, otyp, NoSyn)) fixes)
d1ea37df7358 tuned signature;
wenzelm
parents: 60407
diff changeset
   117
            ||> Proof_Context.set_mode Proof_Context.mode_schematic;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   118
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   119
          fun parse_term term =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   120
            if prop_match match_kind
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   121
            then Syntax.parse_prop ctxt3 term
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   122
            else Syntax.parse_term ctxt3 term;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   123
62134
2405ab06d5b1 remove Eisbach's dependency on HOL
matichuk <daniel.matichuk@nicta.com.au>
parents: 62133
diff changeset
   124
          fun drop_judgment_dummy t =
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   125
            (case t of
62134
2405ab06d5b1 remove Eisbach's dependency on HOL
matichuk <daniel.matichuk@nicta.com.au>
parents: 62133
diff changeset
   126
              Const (judgment, _) $
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 63615
diff changeset
   127
                (Const (\<^syntax_const>\<open>_type_constraint_\<close>, T) $
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 63615
diff changeset
   128
                  Const (\<^const_name>\<open>Pure.dummy_pattern\<close>, _)) =>
62134
2405ab06d5b1 remove Eisbach's dependency on HOL
matichuk <daniel.matichuk@nicta.com.au>
parents: 62133
diff changeset
   129
                if judgment = Object_Logic.judgment_name ctxt then
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 63615
diff changeset
   130
                    Const (\<^syntax_const>\<open>_type_constraint_\<close>, T) $
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 63615
diff changeset
   131
                      Const (\<^const_name>\<open>Pure.dummy_pattern\<close>, propT)
62134
2405ab06d5b1 remove Eisbach's dependency on HOL
matichuk <daniel.matichuk@nicta.com.au>
parents: 62133
diff changeset
   132
                else t
2405ab06d5b1 remove Eisbach's dependency on HOL
matichuk <daniel.matichuk@nicta.com.au>
parents: 62133
diff changeset
   133
            | t1 $ t2 => drop_judgment_dummy t1 $ drop_judgment_dummy t2
2405ab06d5b1 remove Eisbach's dependency on HOL
matichuk <daniel.matichuk@nicta.com.au>
parents: 62133
diff changeset
   134
            | Abs (a, T, b) => Abs (a, T, drop_judgment_dummy b)
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   135
            | _ => t);
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   136
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   137
          val pats =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   138
            map (fn (_, (term, _)) => parse_term (Parse_Tools.the_parse_val term)) ts
62134
2405ab06d5b1 remove Eisbach's dependency on HOL
matichuk <daniel.matichuk@nicta.com.au>
parents: 62133
diff changeset
   139
            |> map drop_judgment_dummy
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   140
            |> (fn ts => fold_map Term.replace_dummy_patterns ts (Variable.maxidx_of ctxt3 + 1))
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   141
            |> fst
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   142
            |> Syntax.check_terms ctxt3;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   143
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   144
          val pat_fixes = fold (Term.add_frees) pats [] |> map fst;
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   145
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   146
          val _ =
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   147
            map2 (fn nm => fn (_, pos) =>
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   148
                member (op =) pat_fixes nm orelse
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   149
                error ("For-fixed variable must be bound in some pattern" ^ Position.here pos))
60407
53ef7b78e78a tuned signature;
wenzelm
parents: 60379
diff changeset
   150
              fix_names fixes;
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   151
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   152
          val _ = map (Term.map_types Type.no_tvars) pats;
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   153
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   154
          val ctxt4 = fold Variable.declare_term pats ctxt3;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   155
60407
53ef7b78e78a tuned signature;
wenzelm
parents: 60379
diff changeset
   156
          val (real_fixes, ctxt5) = ctxt4
53ef7b78e78a tuned signature;
wenzelm
parents: 60379
diff changeset
   157
            |> fold_map Proof_Context.inferred_param fix_names |>> map Free;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   158
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   159
          fun reject_extra_free (Free (x, _)) () =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   160
                if Variable.is_fixed ctxt5 x then ()
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   161
                else error ("Illegal use of free (unfixed) variable " ^ quote x)
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   162
            | reject_extra_free _ () = ();
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   163
          val _ = (fold o fold_aterms) reject_extra_free pats ();
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   164
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   165
          val binds =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   166
            map (fn (b, _) => Option.map (fn (b, att) => (Parse_Tools.the_parse_val b, att)) b) ts;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   167
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   168
          fun upd_ctxt (SOME (b, att)) pat (tms, ctxt) =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   169
                let
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   170
                  val ([nm], ctxt') =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   171
                    Variable.variant_fixes [Name.internal (Binding.name_of b)] ctxt;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   172
                  val abs_nms = Term.strip_all_vars pat;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   173
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   174
                  val param_thm = map (Drule.mk_term o Thm.cterm_of ctxt' o Free) abs_nms
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   175
                    |> Conjunction.intr_balanced
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   176
                    |> Drule.generalize ([], map fst abs_nms)
61852
d273c24b5776 tuned signature -- clarified modules;
wenzelm
parents: 61846
diff changeset
   177
                    |> Thm.tag_free_dummy;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   178
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   179
                  val atts = map (Attrib.attribute ctxt') att;
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   180
                  val (param_thm', ctxt'') = Thm.proof_attributes atts param_thm ctxt';
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   181
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   182
                  fun label_thm thm =
60367
e201bd8973db clarified context;
wenzelm
parents: 60287
diff changeset
   183
                    Thm.cterm_of ctxt'' (Free (nm, propT))
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   184
                    |> Drule.mk_term
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   185
                    |> not (null abs_nms) ? Conjunction.intr thm
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   186
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   187
                  val [head_thm, body_thm] =
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   188
                    Drule.zero_var_indexes_list (map label_thm [param_thm, param_thm'])
61852
d273c24b5776 tuned signature -- clarified modules;
wenzelm
parents: 61846
diff changeset
   189
                    |> map Thm.tag_free_dummy;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   190
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   191
                  val ctxt''' =
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   192
                    Attrib.local_notes "" [((b, []), [([body_thm], [])])] ctxt''
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   193
                    |> snd
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   194
                    |> Variable.declare_maxidx (Thm.maxidx_of head_thm);
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   195
                in
63185
08369e33c185 apply current morphism to method text before evaluating;
matichuk <daniel.matichuk@nicta.com.au>
parents: 62165
diff changeset
   196
                  (SOME (head_thm, att) :: tms, ctxt''')
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   197
                end
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   198
            | upd_ctxt NONE _ (tms, ctxt) = (NONE :: tms, ctxt);
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   199
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   200
          val (binds, ctxt6) = ctxt5
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   201
            |> (fn ctxt => fold2 upd_ctxt binds pats ([], ctxt) |> apfst rev)
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   202
            ||> Proof_Context.restore_mode ctxt;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   203
63527
59eff6e56d81 moved method "use" to Pure;
wenzelm
parents: 63185
diff changeset
   204
          val (text, src) = Method.read_closure_input ctxt6 (Token.input_of body);
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   205
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   206
          val morphism =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   207
            Variable.export_morphism ctxt6
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   208
              (ctxt
61814
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
   209
                |> fold Token.declare_maxidx src
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   210
                |> Variable.declare_maxidx (Variable.maxidx_of ctxt6));
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   211
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   212
          val pats' = map (Term.map_types Type_Infer.paramify_vars #> Morphism.term morphism) pats;
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   213
          val _ = ListPair.app (fn ((_, (v, _)), t) => Parse_Tools.the_parse_fun v t) (ts, pats');
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   214
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   215
          fun close_src src =
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   216
            let
61814
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
   217
              val src' = src |> map (Token.closure #> Token.transform morphism);
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   218
              val _ =
61814
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
   219
                (Token.args_of_src src ~~ Token.args_of_src src')
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
   220
                |> List.app (fn (tok, tok') =>
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
   221
                  (case Token.get_value tok' of
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
   222
                    SOME value => ignore (Token.assign (SOME value) tok)
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
   223
                  | NONE => ()));
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   224
            in src' end;
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   225
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   226
          val binds' =
63185
08369e33c185 apply current morphism to method text before evaluating;
matichuk <daniel.matichuk@nicta.com.au>
parents: 62165
diff changeset
   227
            map (Option.map (fn (t, atts) => (Morphism.thm morphism t, map close_src atts))) binds;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   228
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   229
          val _ =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   230
            ListPair.app
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   231
              (fn ((SOME ((v, _)), _), SOME (t, _)) => Parse_Tools.the_parse_fun v t
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   232
                | ((NONE, _), NONE) => ()
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   233
                | _ => error "Mismatch between real and parsed bound variables")
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   234
              (ts, binds');
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   235
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   236
          val real_fixes' = map (Morphism.term morphism) real_fixes;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   237
          val _ =
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   238
            ListPair.app (fn (((v, _) , _), t) => Parse_Tools.the_parse_fun v t)
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   239
              (fixes, real_fixes');
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   240
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   241
          val match_args = map (fn (_, (_, match_args)) => match_args) ts;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   242
          val binds'' = (binds' ~~ match_args) ~~ pats';
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   243
61814
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
   244
          val src' = map (Token.transform morphism) src;
61912
ad710de5c576 more standard nesting of sub-language: Parse.text allows atomic entities without quotes;
wenzelm
parents: 61852
diff changeset
   245
          val _ = Token.assign (SOME (Token.Source src')) body;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   246
        in
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   247
          (binds'', real_fixes', text)
61814
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
   248
        end));
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   249
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   250
63185
08369e33c185 apply current morphism to method text before evaluating;
matichuk <daniel.matichuk@nicta.com.au>
parents: 62165
diff changeset
   251
fun dest_internal_term t =
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   252
  (case try Logic.dest_conjunction t of
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   253
    SOME (params, head) =>
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   254
     (params |> Logic.dest_conjunctions |> map Logic.dest_term,
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   255
      head |> Logic.dest_term)
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   256
  | NONE => ([], t |> Logic.dest_term));
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   257
63185
08369e33c185 apply current morphism to method text before evaluating;
matichuk <daniel.matichuk@nicta.com.au>
parents: 62165
diff changeset
   258
fun dest_internal_fact thm = dest_internal_term (Thm.prop_of thm);
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   259
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   260
fun inst_thm ctxt env ts params thm =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   261
  let
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   262
    val ts' = map (Envir.norm_term env) ts;
60791
e3f2262786ea updated to infer_instantiate;
wenzelm
parents: 60695
diff changeset
   263
    val insts = map (#1 o dest_Var) ts' ~~ map (Thm.cterm_of ctxt) params;
e3f2262786ea updated to infer_instantiate;
wenzelm
parents: 60695
diff changeset
   264
  in infer_instantiate ctxt insts thm end;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   265
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   266
fun do_inst fact_insts' env text ctxt =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   267
  let
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   268
    val fact_insts =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   269
      map_filter
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   270
        (fn ((((SOME ((_, head), att), _), _), _), thms) => SOME (head, (thms, att))
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   271
          | _ => NONE) fact_insts';
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   272
73553
wenzelm
parents: 69593
diff changeset
   273
    fun try_dest_term thm = \<^try>\<open>#2 (dest_internal_fact thm)\<close>;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   274
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   275
    fun expand_fact fact_insts thm =
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   276
      the_default [thm]
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   277
        (case try_dest_term thm of
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   278
          SOME t_ident => AList.lookup (op aconv) fact_insts t_ident
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   279
        | NONE => NONE);
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   280
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   281
    fun fact_morphism fact_insts =
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   282
      Morphism.term_morphism "do_inst.term" (Envir.norm_term env) $>
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   283
      Morphism.typ_morphism "do_inst.type" (Envir.norm_type (Envir.type_env env)) $>
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   284
      Morphism.fact_morphism "do_inst.fact" (maps (expand_fact fact_insts));
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   285
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   286
    fun apply_attribute (head, (fact, atts)) (fact_insts, ctxt) =
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   287
      let
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   288
        val morphism = fact_morphism fact_insts;
61814
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
   289
        val atts' = map (Attrib.attribute ctxt o map (Token.transform morphism)) atts;
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   290
        val (fact'', ctxt') = fold_map (Thm.proof_attributes atts') fact ctxt;
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   291
      in ((head, fact'') :: fact_insts, ctxt') end;
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   292
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   293
     (*TODO: What to do about attributes that raise errors?*)
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   294
    val (fact_insts', ctxt') = fold_rev (apply_attribute) fact_insts ([], ctxt);
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   295
61814
1ca1142e1711 clarified type Token.src: plain token list, with usual implicit value assignment;
wenzelm
parents: 60949
diff changeset
   296
    val text' = (Method.map_source o map) (Token.transform (fact_morphism fact_insts')) text;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   297
  in
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   298
    (text', ctxt')
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   299
  end;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   300
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   301
fun prep_fact_pat ((x, args), pat) ctxt =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   302
  let
60695
757549b4bbe6 Variable.focus etc.: optional bindings provided by user;
wenzelm
parents: 60642
diff changeset
   303
    val ((params, pat'), ctxt') = Variable.focus NONE pat ctxt;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   304
    val params' = map (Free o snd) params;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   305
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   306
    val morphism =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   307
      Variable.export_morphism ctxt'
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   308
        (ctxt |> Variable.declare_maxidx (Variable.maxidx_of ctxt'));
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   309
    val pat'' :: params'' = map (Morphism.term morphism) (pat' :: params');
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   310
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   311
    fun prep_head (t, att) = (dest_internal_fact t, att);
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   312
  in
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   313
    ((((Option.map prep_head x, args), params''), pat''), ctxt')
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   314
  end;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   315
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   316
fun morphism_env morphism env =
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   317
  let
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   318
    val tenv = Envir.term_env env
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   319
      |> Vartab.map (K (fn (T, t) => (Morphism.typ morphism T, Morphism.term morphism t)));
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   320
    val tyenv = Envir.type_env env
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   321
      |> Vartab.map (K (fn (S, T) => (S, Morphism.typ morphism T)));
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   322
   in Envir.Envir {maxidx = Envir.maxidx_of env, tenv = tenv, tyenv = tyenv} end;
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   323
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   324
fun export_with_params ctxt morphism (SOME ts, params) thm env =
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   325
      let
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   326
        val outer_env = morphism_env morphism env;
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   327
        val thm' = Morphism.thm morphism thm;
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   328
      in inst_thm ctxt outer_env params ts thm' end
60552
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   329
  | export_with_params _ morphism (NONE, _) thm _ = Morphism.thm morphism thm;
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   330
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   331
fun match_filter_env is_newly_fixed pat_vars fixes params env =
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   332
  let
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   333
    val param_vars = map Term.dest_Var params;
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   334
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   335
    val tenv = Envir.term_env env;
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   336
    val params' = map (fn (xi, _) => Vartab.lookup tenv xi) param_vars;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   337
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   338
    val fixes_vars = map Term.dest_Var fixes;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   339
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   340
    val all_vars = Vartab.keys tenv;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   341
    val extra_vars = subtract (fn ((xi, _), xi') => xi = xi') fixes_vars all_vars;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   342
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   343
    val tenv' = tenv |> fold (Vartab.delete_safe) extra_vars;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   344
    val env' =
61846
2c79790d270d more standard term equality;
wenzelm
parents: 61841
diff changeset
   345
      Envir.Envir {maxidx = Envir.maxidx_of env, tenv = tenv', tyenv = Envir.type_env env};
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   346
60552
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   347
    val all_params_bound = forall (fn SOME (_, Free (x, _)) => is_newly_fixed x | _ => false) params';
61846
2c79790d270d more standard term equality;
wenzelm
parents: 61841
diff changeset
   348
    val all_params_distinct = not (has_duplicates (eq_option (eq_pair (op =) (op aconv))) params');
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   349
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   350
    val pat_fixes = inter (eq_fst (op =)) fixes_vars pat_vars;
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   351
    val all_pat_fixes_bound = forall (fn (xi, _) => is_some (Vartab.lookup tenv' xi)) pat_fixes;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   352
  in
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   353
    if all_params_bound andalso all_pat_fixes_bound andalso all_params_distinct
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   354
    then SOME env'
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   355
    else NONE
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   356
  end;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   357
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   358
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   359
(* Slightly hacky way of uniquely identifying focus premises *)
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   360
val prem_idN = "premise_id";
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   361
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   362
fun prem_id_eq ((id, _ : thm), (id', _ : thm)) = id = id';
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   363
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   364
val prem_rules : (int * thm) Item_Net.T =
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   365
  Item_Net.init prem_id_eq (single o Thm.full_prop_of o snd);
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   366
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   367
fun raw_thm_to_id thm =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   368
  (case Properties.get (Thm.get_tags thm) prem_idN of NONE => NONE | SOME id => Int.fromString id)
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   369
  |> the_default ~1;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   370
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   371
structure Focus_Data = Proof_Data
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   372
(
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   373
  type T =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   374
    (int * (int * thm) Item_Net.T) *  (*prems*)
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   375
    Envir.tenv *  (*schematics*)
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   376
    term list  (*params*)
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   377
  fun init _ : T = ((0, prem_rules), Vartab.empty, [])
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   378
);
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   379
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   380
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   381
(* focus prems *)
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   382
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   383
val focus_prems = #1 o Focus_Data.get;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   384
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   385
fun transfer_focus_prems from_ctxt =
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   386
  Focus_Data.map (@{apply 3(1)} (K (focus_prems from_ctxt)))
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   387
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   388
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   389
fun add_focus_prem prem =
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   390
  `(Focus_Data.get #> #1 #> #1) ##>
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   391
  (Focus_Data.map o @{apply 3(1)}) (fn (next, net) =>
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   392
    (next + 1, Item_Net.update (next, Thm.tag_rule (prem_idN, string_of_int next) prem) net));
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   393
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   394
fun remove_focus_prem' (ident, thm) =
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   395
  (Focus_Data.map o @{apply 3(1)} o apsnd)
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   396
    (Item_Net.remove (ident, thm));
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   397
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   398
fun remove_focus_prem thm = remove_focus_prem' (raw_thm_to_id thm, thm);
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   399
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   400
(*TODO: Preliminary analysis to see if we're trying to clear in a non-focus match?*)
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   401
val _ =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   402
  Theory.setup
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 63615
diff changeset
   403
    (Attrib.setup \<^binding>\<open>thin\<close>
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   404
      (Scan.succeed
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   405
        (Thm.declaration_attribute (fn th => Context.mapping I (remove_focus_prem th))))
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   406
        "clear premise inside match method");
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   407
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   408
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   409
(* focus schematics *)
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   410
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   411
val focus_schematics = #2 o Focus_Data.get;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   412
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60553
diff changeset
   413
fun add_focus_schematics schematics =
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   414
  (Focus_Data.map o @{apply 3(2)})
60642
48dd1cefb4ae simplified Thm.instantiate and derivatives: the LHS refers to non-certified variables -- this merely serves as index into already certified structures (or is ignored);
wenzelm
parents: 60553
diff changeset
   415
    (fold (fn ((xi, T), ct) => Vartab.update_new (xi, (T, Thm.term_of ct))) schematics);
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   416
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   417
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   418
(* focus params *)
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   419
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   420
val focus_params = #3 o Focus_Data.get;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   421
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   422
fun add_focus_params params =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   423
  (Focus_Data.map o @{apply 3(3)})
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   424
    (append (map (fn (_, ct) => Thm.term_of ct) params));
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   425
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   426
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   427
(* Add focus elements as proof data *)
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   428
fun augment_focus (focus: Subgoal.focus) : (int list * Subgoal.focus) =
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   429
  let
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   430
    val {context, params, prems, asms, concl, schematics} = focus;
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   431
60552
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   432
    val (prem_ids, ctxt') = context
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   433
      |> add_focus_params params
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   434
      |> add_focus_schematics (snd schematics)
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   435
      |> fold_map add_focus_prem (rev prems)
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   436
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   437
  in
60552
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   438
    (prem_ids,
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   439
      {context = ctxt',
60552
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   440
       params = params,
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   441
       prems = prems,
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   442
       concl = concl,
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   443
       schematics = schematics,
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   444
       asms = asms})
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   445
  end;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   446
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   447
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   448
(* Fix schematics in the goal *)
60695
757549b4bbe6 Variable.focus etc.: optional bindings provided by user;
wenzelm
parents: 60642
diff changeset
   449
fun focus_concl ctxt i bindings goal =
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   450
  let
60367
e201bd8973db clarified context;
wenzelm
parents: 60287
diff changeset
   451
    val ({context = ctxt', concl, params, prems, asms, schematics}, goal') =
60695
757549b4bbe6 Variable.focus etc.: optional bindings provided by user;
wenzelm
parents: 60642
diff changeset
   452
      Subgoal.focus_params ctxt i bindings goal;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   453
60367
e201bd8973db clarified context;
wenzelm
parents: 60287
diff changeset
   454
    val (inst, ctxt'') = Variable.import_inst true [Thm.term_of concl] ctxt';
60805
4cc49ead6e75 tuned signature;
wenzelm
parents: 60791
diff changeset
   455
    val schematic_terms = map (apsnd (Thm.cterm_of ctxt'')) (#2 inst);
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   456
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   457
    val goal'' = Thm.instantiate ([], schematic_terms) goal';
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   458
    val concl' = Thm.instantiate_cterm ([], schematic_terms) concl;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   459
    val (schematic_types, schematic_terms') = schematics;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   460
    val schematics' = (schematic_types, schematic_terms @ schematic_terms');
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   461
  in
60367
e201bd8973db clarified context;
wenzelm
parents: 60287
diff changeset
   462
    ({context = ctxt'', concl = concl', params = params, prems = prems,
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   463
      schematics = schematics', asms = asms} : Subgoal.focus, goal'')
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   464
  end;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   465
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   466
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   467
fun deduplicate eq prev seq =
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   468
  Seq.make (fn () =>
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   469
    (case Seq.pull seq of
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   470
      SOME (x, seq') =>
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   471
        if member eq prev x
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   472
        then Seq.pull (deduplicate eq prev seq')
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   473
        else SOME (x, deduplicate eq (x :: prev) seq')
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   474
    | NONE => NONE));
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   475
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   476
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   477
fun consistent_env env =
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   478
  let
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   479
    val tenv = Envir.term_env env;
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   480
    val tyenv = Envir.type_env env;
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   481
  in
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   482
    forall (fn (_, (T, t)) => Envir.norm_type tyenv T = fastype_of t) (Vartab.dest tenv)
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   483
  end;
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   484
60552
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   485
fun term_eq_wrt (env1, env2) (t1, t2) =
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   486
  Envir.eta_contract (Envir.norm_term env1 t1) aconv
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   487
  Envir.eta_contract (Envir.norm_term env2 t2);
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   488
60552
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   489
fun type_eq_wrt (env1, env2) (T1, T2) =
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   490
  Envir.norm_type (Envir.type_env env1) T1 = Envir.norm_type (Envir.type_env env2) T2;
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   491
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   492
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   493
fun eq_env (env1, env2) =
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   494
    Envir.maxidx_of env1 = Envir.maxidx_of env1 andalso
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   495
    ListPair.allEq (fn ((var, (_, t)), (var', (_, t'))) =>
60552
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   496
        (var = var' andalso term_eq_wrt (env1, env2) (t, t')))
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   497
      (apply2 Vartab.dest (Envir.term_env env1, Envir.term_env env2))
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   498
    andalso
60248
f7e4294216d2 updated Eisbach, using version fb741500f533 of its Bitbucket repository;
wenzelm
parents: 60209
diff changeset
   499
    ListPair.allEq (fn ((var, (_, T)), (var', (_, T'))) =>
60552
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   500
        var = var' andalso type_eq_wrt (env1, env2) (T, T'))
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   501
      (apply2 Vartab.dest (Envir.type_env env1, Envir.type_env env2));
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   502
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   503
60552
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   504
fun merge_env (env1, env2) =
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   505
  let
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   506
    val tenv =
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   507
      Vartab.merge (eq_snd (term_eq_wrt (env1, env2))) (Envir.term_env env1, Envir.term_env env2);
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   508
    val tyenv =
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   509
      Vartab.merge (eq_snd (type_eq_wrt (env1, env2)) andf eq_fst (op =))
60552
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   510
        (Envir.type_env env1, Envir.type_env env2);
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   511
    val maxidx = Int.max (Envir.maxidx_of env1, Envir.maxidx_of env2);
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   512
  in Envir.Envir {maxidx = maxidx, tenv = tenv, tyenv = tyenv} end;
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   513
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   514
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   515
fun import_with_tags thms ctxt =
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   516
  let
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   517
    val ((_, thms'), ctxt') = Variable.import false thms ctxt;
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   518
    val thms'' = map2 (fn thm => Thm.map_tags (K (Thm.get_tags thm))) thms thms';
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   519
  in (thms'', ctxt') end;
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   520
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   521
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   522
fun try_merge (env, env') = SOME (merge_env (env, env')) handle Vartab.DUP _ => NONE
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   523
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   524
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   525
fun Seq_retrieve seq f =
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   526
  let
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   527
    fun retrieve' (list, seq) f =
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   528
      (case Seq.pull seq of
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   529
        SOME (x, seq') =>
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   530
          if f x then (SOME x, (list, seq'))
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   531
          else retrieve' (list @ [x], seq') f
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   532
      | NONE => (NONE, (list, seq)));
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   533
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   534
    val (result, (list, seq)) = retrieve' ([], seq) f;
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   535
  in (result, Seq.append (Seq.of_list list) seq) end;
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   536
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   537
fun match_facts ctxt fixes prop_pats get =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   538
  let
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   539
    fun is_multi (((_, x : match_args), _), _) = #multi x;
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   540
    fun get_cut (((_, x : match_args), _), _) = #cut x;
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   541
    fun do_cut n = if n = ~1 then I else Seq.take n;
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   542
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   543
    val raw_thmss = map (get o snd) prop_pats;
60552
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   544
    val (thmss, ctxt') = fold_burrow import_with_tags raw_thmss ctxt;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   545
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   546
    val newly_fixed = Variable.is_newly_fixed ctxt' ctxt;
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   547
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   548
    val morphism = Variable.export_morphism ctxt' ctxt;
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   549
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   550
    fun match_thm (((x, params), pat), thm)  =
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   551
      let
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   552
        val pat_vars = Term.add_vars pat [];
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   553
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   554
        val ts = Option.map (fst o fst) (fst x);
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   555
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   556
        val item' = Thm.prop_of thm;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   557
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   558
        val matches =
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   559
          (Unify.matchers (Context.Proof ctxt) [(pat, item')])
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   560
          |> Seq.filter consistent_env
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   561
          |> Seq.map_filter (fn env' =>
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   562
              (case match_filter_env newly_fixed pat_vars fixes params env' of
60552
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   563
                SOME env'' => SOME (export_with_params ctxt morphism (ts, params) thm env', env'')
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   564
              | NONE => NONE))
60209
022ca2799c73 updated Eisbach, using version 5df3d8c72403 of its Bitbucket repository;
wenzelm
parents: 60119
diff changeset
   565
          |> Seq.map (apfst (Thm.map_tags (K (Thm.get_tags thm))))
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   566
          |> deduplicate (eq_pair Thm.eq_thm_prop eq_env) []
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   567
      in matches end;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   568
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   569
    val all_matches =
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   570
      map2 pair prop_pats thmss
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   571
      |> map (fn (pat, matches) => (pat, map (fn thm => match_thm (pat, thm)) matches));
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   572
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   573
    fun proc_multi_match (pat, thmenvs) (pats, env) =
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   574
      do_cut (get_cut pat)
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   575
        (if is_multi pat then
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   576
          let
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   577
            fun maximal_set tail seq envthms =
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   578
              Seq.make (fn () =>
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   579
                (case Seq.pull seq of
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   580
                  SOME ((thm, env'), seq') =>
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   581
                    let
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   582
                      val (result, envthms') =
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   583
                        Seq_retrieve envthms (fn (env, _) => eq_env (env, env'));
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   584
                    in        
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   585
                      (case result of
60552
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   586
                        SOME (_, thms) => SOME ((env', thm :: thms), maximal_set tail seq' envthms')
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   587
                      | NONE => Seq.pull (maximal_set (tail @ [(env', [thm])]) seq' envthms'))
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   588
                    end
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   589
                 | NONE => Seq.pull (Seq.append envthms (Seq.of_list tail))));
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   590
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   591
            val maximal_sets = fold (maximal_set []) thmenvs Seq.empty;
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   592
          in
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   593
            maximal_sets
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   594
            |> Seq.map swap
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   595
            |> Seq.filter (fn (thms, _) => not (null thms))
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   596
            |> Seq.map_filter (fn (thms, env') =>
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   597
              (case try_merge (env, env') of
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   598
                SOME env'' => SOME ((pat, thms) :: pats, env'')
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   599
              | NONE => NONE))
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   600
          end
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   601
        else
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   602
          let
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   603
            fun just_one (thm, env') =
60552
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   604
              (case try_merge (env, env') of
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   605
                SOME env'' => SOME ((pat, [thm]) :: pats, env'')
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   606
              | NONE => NONE);
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   607
          in fold (fn seq => Seq.append (Seq.map_filter just_one seq)) thmenvs Seq.empty end);
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   608
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   609
    val all_matches =
63615
wenzelm
parents: 63527
diff changeset
   610
      Seq.EVERY (map proc_multi_match all_matches) ([], Envir.init);
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   611
  in
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   612
    all_matches
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   613
    |> Seq.map (apsnd (morphism_env morphism))
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   614
  end;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   615
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   616
fun real_match using outer_ctxt fixes m text pats st =
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   617
  let              
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   618
    val goal_ctxt =
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   619
      fold Variable.declare_term fixes outer_ctxt
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   620
      (*FIXME Is this a good idea? We really only care about the maxidx*)
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   621
      |> fold (fn (_, t) => Variable.declare_term t) pats;
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   622
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   623
    fun make_fact_matches ctxt get =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   624
      let
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   625
        val (pats', ctxt') = fold_map prep_fact_pat pats ctxt;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   626
      in
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   627
        match_facts ctxt' fixes pats' get
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   628
        |> Seq.map (fn (fact_insts, env) => do_inst fact_insts env text ctxt')
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   629
      end;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   630
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   631
    fun make_term_matches ctxt get =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   632
      let
61839
6f47a66afcd3 clarified ML scopes;
wenzelm
parents: 61838
diff changeset
   633
        val pats' = map
6f47a66afcd3 clarified ML scopes;
wenzelm
parents: 61838
diff changeset
   634
          (fn ((SOME _, _), _) => error "Cannot name term match"
6f47a66afcd3 clarified ML scopes;
wenzelm
parents: 61838
diff changeset
   635
            | ((_, x), t) => (((NONE, x), []), Logic.mk_term t)) pats;
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   636
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   637
        val thm_of = Drule.mk_term o Thm.cterm_of ctxt;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   638
        fun get' t = get (Logic.dest_term t) |> map thm_of;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   639
      in
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   640
        match_facts ctxt fixes pats' get'
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   641
        |> Seq.map (fn (fact_insts, env) => do_inst fact_insts env text ctxt)
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   642
      end;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   643
  in
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   644
    (case m of
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   645
      Match_Fact net =>
61841
4d3527b94f2a more general types Proof.method / context_tactic;
wenzelm
parents: 61840
diff changeset
   646
        make_fact_matches goal_ctxt (Item_Net.retrieve net)
4d3527b94f2a more general types Proof.method / context_tactic;
wenzelm
parents: 61840
diff changeset
   647
        |> Seq.map (fn (text, ctxt') =>
63527
59eff6e56d81 moved method "use" to Pure;
wenzelm
parents: 63185
diff changeset
   648
          Method.evaluate_runtime text ctxt' using (ctxt', st)
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   649
          |> Seq.filter_results |> Seq.map (fn (_, thm) => (outer_ctxt, thm)))
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   650
    | Match_Term net =>
61841
4d3527b94f2a more general types Proof.method / context_tactic;
wenzelm
parents: 61840
diff changeset
   651
        make_term_matches goal_ctxt (Item_Net.retrieve net)
4d3527b94f2a more general types Proof.method / context_tactic;
wenzelm
parents: 61840
diff changeset
   652
        |> Seq.map (fn (text, ctxt') =>
63527
59eff6e56d81 moved method "use" to Pure;
wenzelm
parents: 63185
diff changeset
   653
          Method.evaluate_runtime text ctxt' using (ctxt', st)
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   654
          |> Seq.filter_results |> Seq.map (fn (_, thm) => (outer_ctxt, thm)))
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   655
    | match_kind =>
61839
6f47a66afcd3 clarified ML scopes;
wenzelm
parents: 61838
diff changeset
   656
        if Thm.no_prems st then Seq.empty
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   657
        else
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   658
          let
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   659
            fun focus_cases f g =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   660
              (case match_kind of
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   661
                Match_Prems b => f b
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   662
              | Match_Concl => g
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   663
              | _ => raise Fail "Match kind fell through");
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   664
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   665
            val ((local_premids, {context = focus_ctxt, params, asms, concl, prems, ...}), focused_goal) =
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   666
              focus_cases (K Subgoal.focus_prems) focus_concl goal_ctxt 1 NONE st
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   667
              |>> augment_focus;
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   668
            
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   669
            val texts =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   670
              focus_cases
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   671
                (fn is_local => fn _ =>
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   672
                  make_fact_matches focus_ctxt
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   673
                    (Item_Net.retrieve (focus_prems focus_ctxt |> snd)
60552
742b553d88b2 tuned whitespace;
wenzelm
parents: 60469
diff changeset
   674
                     #> is_local ? filter (fn (p, _) => exists (fn id' => id' = p) local_premids)
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   675
                     #> order_list))
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   676
                (fn _ =>
61839
6f47a66afcd3 clarified ML scopes;
wenzelm
parents: 61838
diff changeset
   677
                  make_term_matches focus_ctxt
6f47a66afcd3 clarified ML scopes;
wenzelm
parents: 61838
diff changeset
   678
                    (fn _ => [Logic.strip_imp_concl (Thm.term_of concl)]))
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   679
                ();
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   680
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   681
            (*TODO: How to handle cases? *)
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   682
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   683
            fun do_retrofit (inner_ctxt, st1) =
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   684
              let
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   685
                val (_, before_prems) = focus_prems focus_ctxt;
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   686
                val (_, after_prems) = focus_prems inner_ctxt;
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   687
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   688
                val removed_prems =
62165
b10046b14dd8 tuned signature;
wenzelm
parents: 62135
diff changeset
   689
                  Item_Net.filter (null o Item_Net.lookup after_prems) before_prems
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   690
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   691
                val removed_local_prems = Item_Net.content removed_prems
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   692
                  |> filter (fn (id, _) => member (op =) local_premids id)
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   693
                  |> map (fn (_, prem) => Thm.prop_of prem)
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   694
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   695
                fun filter_removed_prems prems =
62165
b10046b14dd8 tuned signature;
wenzelm
parents: 62135
diff changeset
   696
                  Item_Net.filter (null o Item_Net.lookup removed_prems) prems;
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   697
                                     
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   698
                val outer_ctxt' = outer_ctxt 
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   699
                  |> Focus_Data.map (@{apply 3(1)} (apsnd filter_removed_prems));
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   700
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   701
                val n_subgoals = Thm.nprems_of st1;
60285
b4f1a0a701ae updated Eisbach, using version 4863020a8fe9 of its Bitbucket repository;
wenzelm
parents: 60248
diff changeset
   702
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   703
                val removed_prem_idxs = 
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   704
                  prems
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   705
                  |> tag_list 0
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   706
                  |> filter (member (op aconv) removed_local_prems o Thm.prop_of o snd)
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   707
                  |> map fst
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   708
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   709
                fun filter_prem (i, _) = not (member (op =) removed_prem_idxs i); 
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   710
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   711
              in
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   712
                Subgoal.retrofit inner_ctxt goal_ctxt params asms 1 st1 st
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   713
                |> focus_cases 
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   714
                   (fn _ => (n_subgoals > 0 andalso length removed_local_prems > 0) ?
61840
wenzelm
parents: 61839
diff changeset
   715
                    (Seq.map (Goal.restrict 1 n_subgoals)
wenzelm
parents: 61839
diff changeset
   716
                      #> Seq.maps (ALLGOALS (fn i =>
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   717
                          DETERM (filter_prems_tac' goal_ctxt filter_prem i)))
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   718
                      #> Seq.map (Goal.unrestrict 1)))
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   719
                   I
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   720
                |> Seq.map (pair outer_ctxt')
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   721
              end;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   722
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   723
            fun apply_text (text, ctxt') =
63527
59eff6e56d81 moved method "use" to Pure;
wenzelm
parents: 63185
diff changeset
   724
                Method.evaluate_runtime text ctxt' using (ctxt', focused_goal)
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   725
              |> Seq.filter_results
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   726
              |> Seq.maps (Seq.DETERM do_retrofit)             
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   727
61839
6f47a66afcd3 clarified ML scopes;
wenzelm
parents: 61838
diff changeset
   728
          in Seq.map apply_text texts end)
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   729
  end;
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   730
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   731
val _ =
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   732
  Theory.setup
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 63615
diff changeset
   733
    (Method.setup \<^binding>\<open>match\<close>
61837
wenzelm
parents: 61836
diff changeset
   734
      (parse_match_kind :--
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 63615
diff changeset
   735
        (fn kind => Scan.lift \<^keyword>\<open>in\<close> |-- Parse.enum1' "\<bar>" (parse_named_pats kind)) >>
61837
wenzelm
parents: 61836
diff changeset
   736
        (fn (matches, bodies) => fn ctxt =>
61841
4d3527b94f2a more general types Proof.method / context_tactic;
wenzelm
parents: 61840
diff changeset
   737
          CONTEXT_METHOD (fn using => Method.RUNTIME (fn (goal_ctxt, st) =>
61837
wenzelm
parents: 61836
diff changeset
   738
            let
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   739
              val ctxt' = transfer_focus_prems goal_ctxt ctxt;
61841
4d3527b94f2a more general types Proof.method / context_tactic;
wenzelm
parents: 61840
diff changeset
   740
              fun exec (pats, fixes, text) st' =
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   741
                real_match using ctxt' fixes matches text pats st';
61841
4d3527b94f2a more general types Proof.method / context_tactic;
wenzelm
parents: 61840
diff changeset
   742
            in
4d3527b94f2a more general types Proof.method / context_tactic;
wenzelm
parents: 61840
diff changeset
   743
              Seq.flat (Seq.FIRST (map exec bodies) st)
62133
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   744
              |> Seq.map (apfst (fn ctxt' => transfer_focus_prems ctxt' goal_ctxt))
43518f70b438 match method now makes proper use of context_tactic
matichuk <daniel.matichuk@nicta.com.au>
parents: 61912
diff changeset
   745
              |> Seq.make_results
61841
4d3527b94f2a more general types Proof.method / context_tactic;
wenzelm
parents: 61840
diff changeset
   746
            end))))
60119
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   747
      "structural analysis/matching on goals");
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   748
54bea620e54f added Eisbach, using version 3752768caa17 of its Bitbucket repository;
wenzelm
parents:
diff changeset
   749
end;