src/HOL/Tools/Quotient/quotient_tacs.ML
author wenzelm
Wed, 25 Jun 2025 16:35:25 +0200
changeset 82768 8f866fd6fae1
parent 82695 d93ead9ac6df
permissions -rw-r--r--
merged
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37744
3daaf23b9ab4 tuned titles
haftmann
parents: 37593
diff changeset
     1
(*  Title:      HOL/Tools/Quotient/quotient_tacs.ML
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     2
    Author:     Cezary Kaliszyk and Christian Urban
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     3
35788
f1deaca15ca3 observe standard header format;
wenzelm
parents: 35625
diff changeset
     4
Tactics for solving goal arising from lifting theorems to quotient
f1deaca15ca3 observe standard header format;
wenzelm
parents: 35625
diff changeset
     5
types.
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     6
*)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     7
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     8
signature QUOTIENT_TACS =
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     9
sig
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    10
  val regularize_tac: Proof.context -> int -> tactic
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    11
  val injection_tac: Proof.context -> int -> tactic
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    12
  val all_injection_tac: Proof.context -> int -> tactic
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    13
  val clean_tac: Proof.context -> int -> tactic
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
    14
38859
053c69cb4a0e quotient package: lemmas to be lifted and descended can be pre-simplified
Christian Urban <urbanc@in.tum.de>
parents: 38719
diff changeset
    15
  val descend_procedure_tac: Proof.context -> thm list -> int -> tactic
053c69cb4a0e quotient package: lemmas to be lifted and descended can be pre-simplified
Christian Urban <urbanc@in.tum.de>
parents: 38719
diff changeset
    16
  val descend_tac: Proof.context -> thm list -> int -> tactic
45782
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
    17
  val partiality_descend_procedure_tac: Proof.context -> thm list -> int -> tactic
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
    18
  val partiality_descend_tac: Proof.context -> thm list -> int -> tactic
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
    19
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
    20
38859
053c69cb4a0e quotient package: lemmas to be lifted and descended can be pre-simplified
Christian Urban <urbanc@in.tum.de>
parents: 38719
diff changeset
    21
  val lift_procedure_tac: Proof.context -> thm list -> thm -> int -> tactic
053c69cb4a0e quotient package: lemmas to be lifted and descended can be pre-simplified
Christian Urban <urbanc@in.tum.de>
parents: 38719
diff changeset
    22
  val lift_tac: Proof.context -> thm list -> thm list -> int -> tactic
37593
2505feaf2d70 separated the lifting and descending procedures in the quotient package
Christian Urban <urbanc@in.tum.de>
parents: 37592
diff changeset
    23
38625
b97bd93fb9e2 allow for pre-simplification of lifted theorems
Christian Urban <urbanc@in.tum.de>
parents: 38624
diff changeset
    24
  val lifted: Proof.context -> typ list -> thm list -> thm -> thm
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    25
  val lifted_attrib: attribute
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    26
end;
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    27
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    28
structure Quotient_Tacs: QUOTIENT_TACS =
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    29
struct
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    30
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    31
(** various helper fuctions **)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    32
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    33
(* Since HOL_basic_ss is too "big" for us, we *)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    34
(* need to set up our own minimal simpset.    *)
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47308
diff changeset
    35
fun mk_minimal_simpset ctxt =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47308
diff changeset
    36
  empty_simpset ctxt
45625
750c5a47400b modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 45350
diff changeset
    37
  |> Simplifier.set_subgoaler asm_simp_tac
750c5a47400b modernized some old-style infix operations, which were left over from the time of ML proof scripts;
wenzelm
parents: 45350
diff changeset
    38
  |> Simplifier.set_mksimps (mksimps [])
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    39
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 52230
diff changeset
    40
fun atomize_thm ctxt thm =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
    41
  let
74245
282cd3aa6cc6 clarified modules;
wenzelm
parents: 69593
diff changeset
    42
    val thm' = Thm.legacy_freezeT (Thm.forall_intr_vars thm) (* FIXME/TODO: is this proper Isar-technology? no! *)
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
    43
    val thm'' = Object_Logic.atomize ctxt (Thm.cprop_of thm')
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
    44
  in
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
    45
    @{thm equal_elim_rule1} OF [thm'', thm']
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
    46
  end
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    47
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    48
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    49
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    50
(*** Regularize Tactic ***)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    51
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    52
(** solvers for equivp and quotient assumptions **)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    53
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    54
fun equiv_tac ctxt =
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67632
diff changeset
    55
  REPEAT_ALL_NEW (resolve_tac ctxt (rev (Named_Theorems.get ctxt \<^named_theorems>\<open>quot_equiv\<close>)))
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    56
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47308
diff changeset
    57
val equiv_solver = mk_solver "Equivalence goal solver" equiv_tac
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    58
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    59
fun quotient_tac ctxt =
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    60
  (REPEAT_ALL_NEW (FIRST'
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
    61
    [resolve_tac ctxt @{thms identity_quotient3},
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67632
diff changeset
    62
     resolve_tac ctxt (rev (Named_Theorems.get ctxt \<^named_theorems>\<open>quot_thm\<close>))]))
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    63
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47308
diff changeset
    64
val quotient_solver = mk_solver "Quotient goal solver" quotient_tac
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    65
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    66
fun solve_quotient_assm ctxt thm =
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    67
  case Seq.pull (quotient_tac ctxt 1 thm) of
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    68
    SOME (t, _) => t
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    69
  | _ => error "Solve_quotient_assm failed. Possibly a quotient theorem is missing."
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    70
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    71
80683
a6da4485a842 misc tuning and clarification: proper context, proper exception;
wenzelm
parents: 80682
diff changeset
    72
fun get_match_inst ctxt pat trm =
a6da4485a842 misc tuning and clarification: proper context, proper exception;
wenzelm
parents: 80682
diff changeset
    73
  (case Unify.matcher (Context.Proof ctxt) [pat] [trm] of
a6da4485a842 misc tuning and clarification: proper context, proper exception;
wenzelm
parents: 80682
diff changeset
    74
    SOME env =>
a6da4485a842 misc tuning and clarification: proper context, proper exception;
wenzelm
parents: 80682
diff changeset
    75
      let
a6da4485a842 misc tuning and clarification: proper context, proper exception;
wenzelm
parents: 80682
diff changeset
    76
        val instT =
a6da4485a842 misc tuning and clarification: proper context, proper exception;
wenzelm
parents: 80682
diff changeset
    77
          TVars.build (Envir.type_env env |> Vartab.fold (fn (x, (S, T)) =>
a6da4485a842 misc tuning and clarification: proper context, proper exception;
wenzelm
parents: 80682
diff changeset
    78
            TVars.add ((x, S), Thm.ctyp_of ctxt T)))
a6da4485a842 misc tuning and clarification: proper context, proper exception;
wenzelm
parents: 80682
diff changeset
    79
        val inst =
a6da4485a842 misc tuning and clarification: proper context, proper exception;
wenzelm
parents: 80682
diff changeset
    80
          Vars.build (Envir.term_env env |> Vartab.fold (fn (x, (T, t)) =>
a6da4485a842 misc tuning and clarification: proper context, proper exception;
wenzelm
parents: 80682
diff changeset
    81
            Vars.add ((x, T), Thm.cterm_of ctxt t)))
a6da4485a842 misc tuning and clarification: proper context, proper exception;
wenzelm
parents: 80682
diff changeset
    82
      in (instT, inst) end
a6da4485a842 misc tuning and clarification: proper context, proper exception;
wenzelm
parents: 80682
diff changeset
    83
  | NONE => raise TERM ("Higher-order match failed", [pat, trm]));
a6da4485a842 misc tuning and clarification: proper context, proper exception;
wenzelm
parents: 80682
diff changeset
    84
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    85
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    86
(* Calculates the instantiations for the lemmas:
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    87
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    88
      ball_reg_eqv_range and bex_reg_eqv_range
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    89
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    90
   Since the left-hand-side contains a non-pattern '?P (f ?x)'
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    91
   we rely on unification/instantiation to check whether the
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    92
   theorem applies and return NONE if it doesn't.
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    93
*)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    94
fun calculate_inst ctxt ball_bex_thm redex R1 R2 =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
    95
  let
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
    96
    fun get_lhs thm = fst (Logic.dest_equals (Thm.concl_of thm))
59638
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
    97
    val ty_inst = map (SOME o Thm.ctyp_of ctxt) [domain_type (fastype_of R2)]
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
    98
    val trm_inst = map (SOME o Thm.cterm_of ctxt) [R2, R1]
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
    99
  in
60801
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
   100
    (case try (Thm.instantiate' ty_inst trm_inst) ball_bex_thm of
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   101
      NONE => NONE
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   102
    | SOME thm' =>
80683
a6da4485a842 misc tuning and clarification: proper context, proper exception;
wenzelm
parents: 80682
diff changeset
   103
        (case try (get_match_inst ctxt (get_lhs thm')) (Thm.term_of redex) of
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   104
          NONE => NONE
43333
2bdec7f430d3 renamed Drule.instantiate to Drule.instantiate_normalize to emphasize its meaning as opposed to plain Thm.instantiate;
wenzelm
parents: 42361
diff changeset
   105
        | SOME inst2 => try (Drule.instantiate_normalize inst2) thm'))
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   106
  end
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   107
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47308
diff changeset
   108
fun ball_bex_range_simproc ctxt redex =
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
   109
  (case Thm.term_of redex of
80678
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   110
    \<^Const_>\<open>Ball _ for \<open>\<^Const_>\<open>Respects _ for \<^Const_>\<open>rel_fun _ _ _ _ for R1 R2\<close>\<close>\<close> _\<close> =>
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   111
      calculate_inst ctxt @{thm ball_reg_eqv_range[THEN eq_reflection]} redex R1 R2
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   112
  | \<^Const_>\<open>Bex _ for \<^Const_>\<open>Respects _ for \<^Const_>\<open>rel_fun _ _ _ _ for R1 R2\<close>\<close> _\<close> =>
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   113
      calculate_inst ctxt @{thm bex_reg_eqv_range[THEN eq_reflection]} redex R1 R2
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
   114
  | _ => NONE)
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   115
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   116
(* Regularize works as follows:
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   117
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   118
  0. preliminary simplification step according to
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   119
     ball_reg_eqv bex_reg_eqv babs_reg_eqv ball_reg_eqv_range bex_reg_eqv_range
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   120
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   121
  1. eliminating simple Ball/Bex instances (ball_reg_right bex_reg_left)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   122
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   123
  2. monos
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   124
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   125
  3. commutation rules for ball and bex (ball_all_comm bex_ex_comm)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   126
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   127
  4. then rel-equalities, which need to be instantiated with 'eq_imp_rel'
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   128
     to avoid loops
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   129
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   130
  5. then simplification like 0
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   131
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   132
  finally jump back to 1
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   133
*)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   134
37493
2377d246a631 Quotient package now uses Partial Equivalence instead place of equivalence
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 36945
diff changeset
   135
fun reflp_get ctxt =
80682
d2920ff62827 tuned: eliminate odd clones;
wenzelm
parents: 80681
diff changeset
   136
  map_filter (fn th => if Thm.no_prems th then SOME (th RS @{thm equivp_reflp}) else NONE
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67632
diff changeset
   137
    handle THM _ => NONE) (rev (Named_Theorems.get ctxt \<^named_theorems>\<open>quot_equiv\<close>))
37493
2377d246a631 Quotient package now uses Partial Equivalence instead place of equivalence
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 36945
diff changeset
   138
67091
1393c2340eec more symbols;
wenzelm
parents: 62913
diff changeset
   139
val eq_imp_rel = @{lemma "equivp R \<Longrightarrow> a = b \<longrightarrow> R a b" by (simp add: equivp_reflp)}
37493
2377d246a631 Quotient package now uses Partial Equivalence instead place of equivalence
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 36945
diff changeset
   140
57960
ee1ba4848896 updated to named_theorems;
wenzelm
parents: 55945
diff changeset
   141
fun eq_imp_rel_get ctxt =
80682
d2920ff62827 tuned: eliminate odd clones;
wenzelm
parents: 80681
diff changeset
   142
  map (fn th => th RS eq_imp_rel) (rev (Named_Theorems.get ctxt \<^named_theorems>\<open>quot_equiv\<close>))
37493
2377d246a631 Quotient package now uses Partial Equivalence instead place of equivalence
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 36945
diff changeset
   143
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
   144
val regularize_simproc =
78808
64973b03b778 more standard simproc_setup using ML antiquotation;
wenzelm
parents: 77879
diff changeset
   145
  \<^simproc_setup>\<open>passive regularize
64973b03b778 more standard simproc_setup using ML antiquotation;
wenzelm
parents: 77879
diff changeset
   146
    ("Ball (Respects (R1 ===> R2)) P" | "Bex (Respects (R1 ===> R2)) P") =
64973b03b778 more standard simproc_setup using ML antiquotation;
wenzelm
parents: 77879
diff changeset
   147
    \<open>K ball_bex_range_simproc\<close>\<close>
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60801
diff changeset
   148
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   149
fun regularize_tac ctxt =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   150
  let
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   151
    val simpset =
80701
39cd50407f79 tuned: prefer canonical argument order;
wenzelm
parents: 80691
diff changeset
   152
      (mk_minimal_simpset ctxt
39cd50407f79 tuned: prefer canonical argument order;
wenzelm
parents: 80691
diff changeset
   153
        addsimps @{thms ball_reg_eqv bex_reg_eqv babs_reg_eqv babs_simp}
39cd50407f79 tuned: prefer canonical argument order;
wenzelm
parents: 80691
diff changeset
   154
        |> Simplifier.add_proc regularize_simproc)
82695
d93ead9ac6df discontinue old infixes;
wenzelm
parents: 80701
diff changeset
   155
      |> Simplifier.add_unsafe_solver equiv_solver
d93ead9ac6df discontinue old infixes;
wenzelm
parents: 80701
diff changeset
   156
      |> Simplifier.add_unsafe_solver quotient_solver
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   157
    val eq_eqvs = eq_imp_rel_get ctxt
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   158
  in
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   159
    simp_tac simpset THEN'
44285
dd203341fd2b Quotient Package: Regularization: do not fail if no progress is made, leave the subgoal to the user. Injection: try assumptions before extensionality to avoid looping.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 43934
diff changeset
   160
    TRY o REPEAT_ALL_NEW (CHANGED o FIRST'
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 58963
diff changeset
   161
      [resolve_tac ctxt @{thms ball_reg_right bex_reg_left bex1_bexeq_reg},
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 58963
diff changeset
   162
       resolve_tac ctxt (Inductive.get_monos ctxt),
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 58963
diff changeset
   163
       resolve_tac ctxt @{thms ball_all_comm bex_ex_comm},
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 58963
diff changeset
   164
       resolve_tac ctxt eq_eqvs,
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   165
       simp_tac simpset])
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   166
  end
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   167
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   168
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   169
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   170
(*** Injection Tactic ***)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   171
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   172
(* Looks for Quot_True assumptions, and in case its parameter
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   173
   is an application, it returns the function and the argument.
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   174
*)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   175
fun find_qt_asm asms =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   176
  let
80678
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   177
    fun find_fun \<^Const_>\<open>Trueprop for \<^Const_>\<open>Quot_True _ for _\<close>\<close> = true
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   178
      | find_fun _ = false
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   179
  in
80678
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   180
    (case find_first find_fun asms of
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   181
      SOME (_ $ (_ $ (f $ a))) => SOME (f, a)
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   182
    | _ => NONE)
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   183
  end
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   184
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   185
fun quot_true_simple_conv ctxt fnctn ctrm =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   186
  (case Thm.term_of ctrm of
80678
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   187
    \<^Const_>\<open>Quot_True _ for x\<close> =>
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   188
      let
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   189
        val fx = fnctn x;
59638
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
   190
        val cx = Thm.cterm_of ctxt x;
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
   191
        val cfx = Thm.cterm_of ctxt fx;
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
   192
        val cxt = Thm.ctyp_of ctxt (fastype_of x);
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
   193
        val cfxt = Thm.ctyp_of ctxt (fastype_of fx);
60801
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
   194
        val thm = Thm.instantiate' [SOME cxt, SOME cfxt] [SOME cx, SOME cfx] @{thm QT_imp}
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   195
      in
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   196
        Conv.rewr_conv thm ctrm
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   197
      end)
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   198
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   199
fun quot_true_conv ctxt fnctn ctrm =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   200
  (case Thm.term_of ctrm of
80678
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   201
    \<^Const_>\<open>Quot_True _ for _\<close> =>
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   202
      quot_true_simple_conv ctxt fnctn ctrm
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   203
  | _ $ _ => Conv.comb_conv (quot_true_conv ctxt fnctn) ctrm
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   204
  | Abs _ => Conv.abs_conv (fn (_, ctxt) => quot_true_conv ctxt fnctn) ctxt ctrm
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   205
  | _ => Conv.all_conv ctrm)
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   206
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   207
fun quot_true_tac ctxt fnctn =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   208
  CONVERSION
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   209
    ((Conv.params_conv ~1 (fn ctxt =>
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   210
        (Conv.prems_conv ~1 (quot_true_conv ctxt fnctn)))) ctxt)
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   211
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   212
fun dest_comb (f $ a) = (f, a)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   213
fun dest_bcomb ((_ $ l) $ r) = (l, r)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   214
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   215
fun unlam t =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   216
  (case t of
74525
c960bfcb91db discontinued Term.dest_abs / Logic.dest_all, which are officially superseded by Variable.dest_abs etc., but there are also Term.dest_abs_global to recover existing tools easily;
wenzelm
parents: 74282
diff changeset
   217
    Abs _ => snd (Term.dest_abs_global t)
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   218
  | _ => unlam (Abs("", domain_type (fastype_of t), (incr_boundvars 1 t) $ (Bound 0))))
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   219
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   220
val bare_concl = HOLogic.dest_Trueprop o Logic.strip_assums_concl
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   221
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   222
(* We apply apply_rsp only in case if the type needs lifting.
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   223
   This is the case if the type of the data in the Quot_True
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   224
   assumption is different from the corresponding type in the goal.
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   225
*)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   226
val apply_rsp_tac =
59638
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
   227
  Subgoal.FOCUS (fn {concl, asms, context = ctxt,...} =>
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   228
    let
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   229
      val bare_concl = HOLogic.dest_Trueprop (Thm.term_of concl)
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   230
      val qt_asm = find_qt_asm (map Thm.term_of asms)
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   231
    in
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   232
      case (bare_concl, qt_asm) of
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   233
        (R2 $ (f $ x) $ (g $ y), SOME (qt_fun, qt_arg)) =>
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   234
          if fastype_of qt_fun = fastype_of f
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   235
          then no_tac
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   236
          else
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   237
            let
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   238
              val ty_x = fastype_of x
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   239
              val ty_b = fastype_of qt_arg
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   240
              val ty_f = range_type (fastype_of f)
59638
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
   241
              val ty_inst = map (SOME o Thm.ctyp_of ctxt) [ty_x, ty_b, ty_f]
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
   242
              val t_inst = map (SOME o Thm.cterm_of ctxt) [R2, f, g, x, y];
60801
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
   243
              val inst_thm = Thm.instantiate' ty_inst
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents: 46468
diff changeset
   244
                ([NONE, NONE, NONE] @ t_inst) @{thm apply_rspQ3}
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   245
            in
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   246
              (resolve_tac ctxt [inst_thm] THEN' SOLVED' (quotient_tac ctxt)) 1
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   247
            end
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   248
      | _ => no_tac
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   249
    end)
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   250
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   251
(* Instantiates and applies 'equals_rsp'. Since the theorem is
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   252
   complex we rely on instantiation to tell us if it applies
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   253
*)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   254
fun equals_rsp_tac R ctxt =
80685
8f53fa93d5f0 misc tuning and clarification;
wenzelm
parents: 80683
diff changeset
   255
  case try (Thm.cterm_of ctxt) R of (* There can be loose bounds in R *)
8f53fa93d5f0 misc tuning and clarification;
wenzelm
parents: 80683
diff changeset
   256
    SOME ct =>
59638
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
   257
      let
80685
8f53fa93d5f0 misc tuning and clarification;
wenzelm
parents: 80683
diff changeset
   258
        val T = Thm.ctyp_of_cterm ct
80691
a56a32ed48a4 more robust (amending 8f53fa93d5f0): R could be anything and Thm.instantiate' involves some type-checks, e.g. relevant for lemma fset_simps in theory Quotient_Examples.Quotient_FSet;
wenzelm
parents: 80685
diff changeset
   259
        val A = try Thm.dest_ctyp0 T
a56a32ed48a4 more robust (amending 8f53fa93d5f0): R could be anything and Thm.instantiate' involves some type-checks, e.g. relevant for lemma fset_simps in theory Quotient_Examples.Quotient_FSet;
wenzelm
parents: 80685
diff changeset
   260
        val try_inst = \<^try>\<open>Thm.instantiate' [SOME (the A)] [SOME ct] @{thm equals_rsp}\<close>
59638
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
   261
      in
80685
8f53fa93d5f0 misc tuning and clarification;
wenzelm
parents: 80683
diff changeset
   262
        case try_inst of
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   263
          SOME thm => resolve_tac ctxt [thm] THEN' quotient_tac ctxt
59638
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
   264
        | NONE => K no_tac
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
   265
      end
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
   266
  | _ => K no_tac
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   267
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   268
fun rep_abs_rsp_tac ctxt =
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   269
  SUBGOAL (fn (goal, i) =>
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   270
    (case try bare_concl goal of
43934
2108763f298d Quotient Package: handle Bound variables in rep_abs_rsp_tac not only at top-level of the goal
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 43596
diff changeset
   271
      SOME (rel $ _ $ (rep $ (abs $ _))) =>
2108763f298d Quotient Package: handle Bound variables in rep_abs_rsp_tac not only at top-level of the goal
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 43596
diff changeset
   272
        (let
40840
2f97215e79bf just one Term.dest_funT;
wenzelm
parents: 40602
diff changeset
   273
          val (ty_a, ty_b) = dest_funT (fastype_of abs);
59638
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
   274
          val ty_inst = map (SOME o Thm.ctyp_of ctxt) [ty_a, ty_b];
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   275
        in
59638
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
   276
          case try (map (SOME o Thm.cterm_of ctxt)) [rel, abs, rep] of
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   277
            SOME t_inst =>
60801
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
   278
              (case try (Thm.instantiate' ty_inst t_inst) @{thm rep_abs_rsp} of
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   279
                SOME inst_thm => (resolve_tac ctxt [inst_thm] THEN' quotient_tac ctxt) i
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   280
              | NONE => no_tac)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   281
          | NONE => no_tac
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   282
        end
43934
2108763f298d Quotient Package: handle Bound variables in rep_abs_rsp_tac not only at top-level of the goal
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 43596
diff changeset
   283
        handle TERM _ => no_tac)
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   284
    | _ => no_tac))
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   285
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   286
38718
c7cbbb18eabe tuned code
Christian Urban <urbanc@in.tum.de>
parents: 38717
diff changeset
   287
(* Injection means to prove that the regularized theorem implies
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   288
   the abs/rep injected one.
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   289
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   290
   The deterministic part:
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   291
    - remove lambdas from both sides
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   292
    - prove Ball/Bex/Babs equalities using ball_rsp, bex_rsp, babs_rsp
55945
e96383acecf9 renamed 'fun_rel' to 'rel_fun'
blanchet
parents: 54742
diff changeset
   293
    - prove Ball/Bex relations using rel_funI
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   294
    - reflexivity of equality
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   295
    - prove equality of relations using equals_rsp
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   296
    - use user-supplied RSP theorems
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   297
    - solve 'relation of relations' goals using quot_rel_rsp
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   298
    - remove rep_abs from the right side
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   299
      (Lambdas under respects may have left us some assumptions)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   300
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   301
   Then in order:
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   302
    - split applications of lifted type (apply_rsp)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   303
    - split applications of non-lifted type (cong_tac)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   304
    - apply extentionality
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   305
    - assumption
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   306
    - reflexivity of the relation
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   307
*)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   308
fun injection_match_tac ctxt = SUBGOAL (fn (goal, i) =>
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   309
  (case bare_concl goal of
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   310
      (* (R1 ===> R2) (%x...) (%x...) ----> [|R1 x y|] ==> R2 (...x) (...y) *)
80678
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   311
    \<^Const_>\<open>rel_fun _ _ _ _ for _ _ \<open>Abs _\<close> \<open>Abs _\<close>\<close> =>
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   312
      resolve_tac ctxt @{thms rel_funI} THEN' quot_true_tac ctxt unlam
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   313
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   314
      (* (op =) (Ball...) (Ball...) ----> (op =) (...) (...) *)
80678
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   315
  | \<^Const_>\<open>HOL.eq _ for
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   316
      \<^Const_>\<open>Ball _ for \<^Const_>\<open>Respects _ for _\<close> _\<close>\<close> $
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   317
      \<^Const_>\<open>Ball _ for \<^Const_>\<open>Respects _ for _\<close> _\<close> =>
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   318
      resolve_tac ctxt @{thms ball_rsp} THEN' dresolve_tac ctxt @{thms QT_all}
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   319
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   320
      (* (R1 ===> op =) (Ball...) (Ball...) ----> [|R1 x y|] ==> (Ball...x) = (Ball...y) *)
80678
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   321
  | \<^Const_>\<open>rel_fun _ _ _ _ for _ _
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   322
      \<^Const_>\<open>Ball _ for \<^Const_>\<open>Respects _ for _\<close> _\<close>\<close> $
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   323
      \<^Const_>\<open>Ball _ for \<^Const_>\<open>Respects _ for _\<close> _\<close> =>
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   324
      resolve_tac ctxt @{thms rel_funI} THEN' quot_true_tac ctxt unlam
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   325
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   326
      (* (op =) (Bex...) (Bex...) ----> (op =) (...) (...) *)
80678
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   327
  | \<^Const_>\<open>HOL.eq _ for
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   328
      \<^Const_>\<open>Bex _ for \<^Const_>\<open>Respects _ for _\<close> _\<close>\<close> $
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   329
      \<^Const_>\<open>Bex _ for \<^Const_>\<open>Respects _ for _\<close> _\<close> =>
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   330
      resolve_tac ctxt @{thms bex_rsp} THEN' dresolve_tac ctxt @{thms QT_ex}
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   331
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   332
      (* (R1 ===> op =) (Bex...) (Bex...) ----> [|R1 x y|] ==> (Bex...x) = (Bex...y) *)
80678
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   333
  | \<^Const_>\<open>rel_fun _ _ _ _ for _ _
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   334
      \<^Const_>\<open>Bex _ for \<^Const_>\<open>Respects _ for _\<close> _\<close>\<close> $
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   335
      \<^Const_>\<open>Bex _ for \<^Const_>\<open>Respects _ for _\<close> _\<close> =>
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   336
      resolve_tac ctxt @{thms rel_funI} THEN' quot_true_tac ctxt unlam
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   337
80678
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   338
  | \<^Const_>\<open>rel_fun _ _ _ _ for _ _ \<^Const_>\<open>Bex1_rel _ for _\<close> \<^Const_>\<open>Bex1_rel _ for _\<close>\<close> =>
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   339
      resolve_tac ctxt @{thms bex1_rel_rsp} THEN' quotient_tac ctxt
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   340
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   341
  | (_ $
80678
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   342
      \<^Const_>\<open>Babs _ _ for \<^Const_>\<open>Respects _ for _\<close> _\<close> $
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   343
      \<^Const_>\<open>Babs _ _ for \<^Const_>\<open>Respects _ for _\<close> _\<close>) =>
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   344
      resolve_tac ctxt @{thms babs_rsp} THEN' quotient_tac ctxt
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   345
80678
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   346
  | \<^Const_>\<open>HOL.eq _ for \<open>R $ _ $ _\<close> \<open>_ $ _ $ _\<close>\<close> =>
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   347
      (resolve_tac ctxt @{thms refl} ORELSE'
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   348
      (equals_rsp_tac R ctxt THEN' RANGE [
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   349
         quot_true_tac ctxt (fst o dest_bcomb), quot_true_tac ctxt (snd o dest_bcomb)]))
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   350
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   351
      (* reflexivity of operators arising from Cong_tac *)
80678
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   352
  | \<^Const_>\<open>HOL.eq _ for _ _\<close> => resolve_tac ctxt @{thms refl}
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   353
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   354
     (* respectfulness of constants; in particular of a simple relation *)
80678
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   355
  | _ $ Const _ $ Const _ => (* rel_fun, list_rel, etc but not equality *)
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   356
      resolve_tac ctxt (rev (Named_Theorems.get ctxt \<^named_theorems>\<open>quot_respect\<close>))
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   357
      THEN_ALL_NEW quotient_tac ctxt
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   358
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   359
      (* R (...) (Rep (Abs ...)) ----> R (...) (...) *)
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   360
      (* observe map_fun *)
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   361
  | _ $ _ $ _
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   362
      => (resolve_tac ctxt @{thms quot_rel_rsp} THEN_ALL_NEW quotient_tac ctxt)
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   363
         ORELSE' rep_abs_rsp_tac ctxt
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   364
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   365
  | _ => K no_tac) i)
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   366
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   367
fun injection_step_tac ctxt rel_refl =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   368
  FIRST' [
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   369
    injection_match_tac ctxt,
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   370
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   371
    (* R (t $ ...) (t' $ ...) ----> apply_rsp   provided type of t needs lifting *)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   372
    apply_rsp_tac ctxt THEN'
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   373
                 RANGE [quot_true_tac ctxt (fst o dest_comb), quot_true_tac ctxt (snd o dest_comb)],
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   374
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   375
    (* (op =) (t $ ...) (t' $ ...) ----> Cong   provided type of t does not need lifting *)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   376
    (* merge with previous tactic *)
58956
a816aa3ff391 proper context for compose_tac, Splitter.split_tac (relevant for unify trace options);
wenzelm
parents: 58950
diff changeset
   377
    Cong_Tac.cong_tac ctxt @{thm cong} THEN'
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   378
                 RANGE [quot_true_tac ctxt (fst o dest_comb), quot_true_tac ctxt (snd o dest_comb)],
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   379
44285
dd203341fd2b Quotient Package: Regularization: do not fail if no progress is made, leave the subgoal to the user. Injection: try assumptions before extensionality to avoid looping.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 43934
diff changeset
   380
    (* resolving with R x y assumptions *)
58963
26bf09b95dda proper context for assume_tac (atac remains as fall-back without context);
wenzelm
parents: 58956
diff changeset
   381
    assume_tac ctxt,
44285
dd203341fd2b Quotient Package: Regularization: do not fail if no progress is made, leave the subgoal to the user. Injection: try assumptions before extensionality to avoid looping.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 43934
diff changeset
   382
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   383
    (* (op =) (%x...) (%y...) ----> (op =) (...) (...) *)
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   384
    resolve_tac ctxt @{thms ext} THEN' quot_true_tac ctxt unlam,
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   385
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   386
    (* reflexivity of the basic relations *)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   387
    (* R ... ... *)
59498
50b60f501b05 proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents: 58963
diff changeset
   388
    resolve_tac ctxt rel_refl]
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   389
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   390
fun injection_tac ctxt =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   391
  let
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   392
    val rel_refl = reflp_get ctxt
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   393
  in
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   394
    injection_step_tac ctxt rel_refl
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   395
  end
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   396
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   397
fun all_injection_tac ctxt =
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   398
  REPEAT_ALL_NEW (injection_tac ctxt)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   399
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   400
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   401
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   402
(*** Cleaning of the Theorem ***)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   403
40602
91e583511113 map_fun combinator in theory Fun
haftmann
parents: 40469
diff changeset
   404
(* expands all map_funs, except in front of the (bound) variables listed in xs *)
91e583511113 map_fun combinator in theory Fun
haftmann
parents: 40469
diff changeset
   405
fun map_fun_simple_conv xs ctrm =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   406
  (case Thm.term_of ctrm of
80678
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   407
    \<^Const_>\<open>map_fun _ _ _ _ for _ _ h _\<close> =>
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   408
      if member (op=) xs h
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   409
      then Conv.all_conv ctrm
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   410
      else Conv.rewr_conv @{thm map_fun_apply [THEN eq_reflection]} ctrm
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   411
  | _ => Conv.all_conv ctrm)
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   412
40602
91e583511113 map_fun combinator in theory Fun
haftmann
parents: 40469
diff changeset
   413
fun map_fun_conv xs ctxt ctrm =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   414
  (case Thm.term_of ctrm of
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   415
    _ $ _ =>
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   416
      (Conv.comb_conv (map_fun_conv xs ctxt) then_conv
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   417
        map_fun_simple_conv xs) ctrm
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   418
  | Abs _ => Conv.abs_conv (fn (x, ctxt) => map_fun_conv (Thm.term_of x :: xs) ctxt) ctxt ctrm
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   419
  | _ => Conv.all_conv ctrm)
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   420
40602
91e583511113 map_fun combinator in theory Fun
haftmann
parents: 40469
diff changeset
   421
fun map_fun_tac ctxt = CONVERSION (map_fun_conv [] ctxt)
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   422
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   423
(* custom matching functions *)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   424
fun mk_abs u i t =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   425
  if incr_boundvars i u aconv t then Bound i
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   426
  else
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   427
    case t of
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   428
      t1 $ t2 => mk_abs u i t1 $ mk_abs u i t2
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   429
    | Abs (s, T, t') => Abs (s, T, mk_abs u (i + 1) t')
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   430
    | Bound j => if i = j then error "make_inst" else t
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   431
    | _ => t
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   432
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   433
fun make_inst lhs t =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   434
  let
80678
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   435
    val _ $ (Abs (_, _, (_ $ ((f as Var (_, \<^Type>\<open>fun T _\<close>)) $ u)))) = lhs;
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   436
    val _ $ (Abs (_, _, (_ $ g))) = t;
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   437
  in
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   438
    (f, Abs ("x", T, mk_abs u 0 g))
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   439
  end
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   440
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   441
fun make_inst_id lhs t =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   442
  let
80678
c5c9b4470d06 tuned: more antiquotations;
wenzelm
parents: 78808
diff changeset
   443
    val _ $ (Abs (_, _, (f as Var (_, \<^Type>\<open>fun T _\<close>)) $ u)) = lhs;
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   444
    val _ $ (Abs (_, _, g)) = t;
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   445
  in
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   446
    (f, Abs ("x", T, mk_abs u 0 g))
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   447
  end
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   448
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   449
(* Simplifies a redex using the 'lambda_prs' theorem.
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   450
   First instantiates the types and known subterms.
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   451
   Then solves the quotient assumptions to get Rep2 and Abs1
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   452
   Finally instantiates the function f using make_inst
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   453
   If Rep2 is an identity then the pattern is simpler and
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   454
   make_inst_id is used
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   455
*)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   456
fun lambda_prs_simple_conv ctxt ctrm =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   457
  (case Thm.term_of ctrm of
80681
3d99104b4b9b tuned: more antiquotations;
wenzelm
parents: 80679
diff changeset
   458
     \<^Const_>\<open>map_fun _ _ _ _ for r1 a2 \<open>Abs _\<close>\<close> =>
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   459
      let
40840
2f97215e79bf just one Term.dest_funT;
wenzelm
parents: 40602
diff changeset
   460
        val (ty_b, ty_a) = dest_funT (fastype_of r1)
2f97215e79bf just one Term.dest_funT;
wenzelm
parents: 40602
diff changeset
   461
        val (ty_c, ty_d) = dest_funT (fastype_of a2)
59638
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
   462
        val tyinst = map (SOME o Thm.ctyp_of ctxt) [ty_a, ty_b, ty_c, ty_d]
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
   463
        val tinst = [NONE, NONE, SOME (Thm.cterm_of ctxt r1), NONE, SOME (Thm.cterm_of ctxt a2)]
60801
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
   464
        val thm1 = Thm.instantiate' tyinst tinst @{thm lambda_prs[THEN eq_reflection]}
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   465
        val thm2 = solve_quotient_assm ctxt (solve_quotient_assm ctxt thm1)
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 52230
diff changeset
   466
        val thm3 = rewrite_rule ctxt @{thms id_apply[THEN eq_reflection]} thm2
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   467
        val (insp, inst) =
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   468
          if ty_c = ty_d
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   469
          then make_inst_id (Thm.term_of (Thm.lhs_of thm3)) (Thm.term_of ctrm)
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   470
          else make_inst (Thm.term_of (Thm.lhs_of thm3)) (Thm.term_of ctrm)
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   471
        val thm4 =
74282
c2ee8d993d6a clarified signature: more scalable operations;
wenzelm
parents: 74245
diff changeset
   472
          Drule.instantiate_normalize
77879
wenzelm
parents: 74525
diff changeset
   473
            (TVars.empty, Vars.make1 (dest_Var insp, Thm.cterm_of ctxt inst)) thm3
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   474
      in
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   475
        Conv.rewr_conv thm4 ctrm
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   476
      end
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   477
  | _ => Conv.all_conv ctrm)
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   478
36936
c52d1c130898 incorporated further conversions and conversionals, after some minor tuning;
wenzelm
parents: 36850
diff changeset
   479
fun lambda_prs_conv ctxt = Conv.top_conv lambda_prs_simple_conv ctxt
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   480
fun lambda_prs_tac ctxt = CONVERSION (lambda_prs_conv ctxt)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   481
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   482
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   483
(* Cleaning consists of:
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   484
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   485
  1. unfolding of ---> in front of everything, except
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   486
     bound variables (this prevents lambda_prs from
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   487
     becoming stuck)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   488
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   489
  2. simplification with lambda_prs
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   490
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   491
  3. simplification with:
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   492
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   493
      - Quotient_abs_rep Quotient_rel_rep
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   494
        babs_prs all_prs ex_prs ex1_prs
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   495
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   496
      - id_simps and preservation lemmas and
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   497
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   498
      - symmetric versions of the definitions
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   499
        (that is definitions of quotient constants
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   500
         are folded)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   501
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   502
  4. test for refl
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   503
*)
57960
ee1ba4848896 updated to named_theorems;
wenzelm
parents: 55945
diff changeset
   504
fun clean_tac ctxt =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   505
  let
57960
ee1ba4848896 updated to named_theorems;
wenzelm
parents: 55945
diff changeset
   506
    val thy =  Proof_Context.theory_of ctxt
45350
257d0b179f0d more use of global operations (see 98ec8b51af9c)
Christian Urban <urbanc@in.tum.de>
parents: 45279
diff changeset
   507
    val defs = map (Thm.symmetric o #def) (Quotient_Info.dest_quotconsts_global thy)
69593
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67632
diff changeset
   508
    val prs = rev (Named_Theorems.get ctxt \<^named_theorems>\<open>quot_preserve\<close>)
3dda49e08b9d isabelle update -u control_cartouches;
wenzelm
parents: 67632
diff changeset
   509
    val ids = rev (Named_Theorems.get ctxt \<^named_theorems>\<open>id_simps\<close>)
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   510
    val thms =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents: 46468
diff changeset
   511
      @{thms Quotient3_abs_rep Quotient3_rel_rep babs_prs all_prs ex_prs ex1_prs} @ ids @ prs @ defs
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   512
82695
d93ead9ac6df discontinue old infixes;
wenzelm
parents: 80701
diff changeset
   513
    val simpset =
d93ead9ac6df discontinue old infixes;
wenzelm
parents: 80701
diff changeset
   514
      (mk_minimal_simpset ctxt) addsimps thms
d93ead9ac6df discontinue old infixes;
wenzelm
parents: 80701
diff changeset
   515
      |> Simplifier.add_unsafe_solver quotient_solver
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   516
  in
41451
892e67be8304 do not open ML structures;
wenzelm
parents: 41444
diff changeset
   517
    EVERY' [
57960
ee1ba4848896 updated to named_theorems;
wenzelm
parents: 55945
diff changeset
   518
      map_fun_tac ctxt,
ee1ba4848896 updated to named_theorems;
wenzelm
parents: 55945
diff changeset
   519
      lambda_prs_tac ctxt,
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47308
diff changeset
   520
      simp_tac simpset,
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   521
      TRY o resolve_tac ctxt [refl]]
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   522
  end
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   523
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   524
38718
c7cbbb18eabe tuned code
Christian Urban <urbanc@in.tum.de>
parents: 38717
diff changeset
   525
(* Tactic for Generalising Free Variables in a Goal *)
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   526
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   527
fun inst_spec ctrm =
60801
7664e0916eec tuned signature;
wenzelm
parents: 60752
diff changeset
   528
  Thm.instantiate' [SOME (Thm.ctyp_of_cterm ctrm)] [NONE, SOME ctrm] @{thm spec}
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   529
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   530
fun inst_spec_tac ctxt ctrms =
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   531
  EVERY' (map (dresolve_tac ctxt o single o inst_spec) ctrms)
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   532
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   533
fun all_list xs trm =
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   534
  fold (fn (x, T) => fn t' => HOLogic.mk_all (x, T, t')) xs trm
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   535
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   536
fun apply_under_Trueprop f =
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   537
  HOLogic.dest_Trueprop #> f #> HOLogic.mk_Trueprop
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   538
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   539
fun gen_frees_tac ctxt =
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   540
  SUBGOAL (fn (concl, i) =>
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   541
    let
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   542
      val vrs = Term.add_frees concl []
59638
cb84e420fc8e clarified context;
wenzelm
parents: 59621
diff changeset
   543
      val cvrs = map (Thm.cterm_of ctxt o Free) vrs
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   544
      val concl' = apply_under_Trueprop (all_list vrs) concl
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   545
      val goal = Logic.mk_implies (concl', concl)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   546
      val rule = Goal.prove ctxt [] [] goal
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   547
        (K (EVERY1 [inst_spec_tac ctxt (rev cvrs), assume_tac ctxt]))
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   548
    in
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   549
      resolve_tac ctxt [rule] i
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   550
    end)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   551
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   552
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   553
(** The General Shape of the Lifting Procedure **)
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   554
45782
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   555
(* in case of partial equivalence relations, this form of the 
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   556
   procedure theorem results in solvable proof obligations 
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   557
*)
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   558
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   559
fun lift_match_error ctxt msg rtrm qtrm =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   560
  let
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   561
    val rtrm_str = Syntax.string_of_term ctxt rtrm
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   562
    val qtrm_str = Syntax.string_of_term ctxt qtrm
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   563
    val msg = cat_lines [enclose "[" "]" msg, "The quotient theorem", qtrm_str,
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   564
      "", "does not match with original theorem", rtrm_str]
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   565
  in
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   566
    error msg
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   567
  end
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   568
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   569
fun procedure_inst ctxt rtrm qtrm =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   570
  let
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   571
    val rtrm' = HOLogic.dest_Trueprop rtrm
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   572
    val qtrm' = HOLogic.dest_Trueprop qtrm
41451
892e67be8304 do not open ML structures;
wenzelm
parents: 41444
diff changeset
   573
    val reg_goal = Quotient_Term.regularize_trm_chk ctxt (rtrm', qtrm')
892e67be8304 do not open ML structures;
wenzelm
parents: 41444
diff changeset
   574
      handle Quotient_Term.LIFT_MATCH msg => lift_match_error ctxt msg rtrm qtrm
892e67be8304 do not open ML structures;
wenzelm
parents: 41444
diff changeset
   575
    val inj_goal = Quotient_Term.inj_repabs_trm_chk ctxt (reg_goal, qtrm')
892e67be8304 do not open ML structures;
wenzelm
parents: 41444
diff changeset
   576
      handle Quotient_Term.LIFT_MATCH msg => lift_match_error ctxt msg rtrm qtrm
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   577
  in
80679
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   578
    (* - A is the original raw theorem
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   579
       - B is the regularized theorem
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   580
       - C is the rep/abs injected version of B
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   581
       - D is the lifted theorem
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   582
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   583
       - 1st prem is the regularization step
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   584
       - 2nd prem is the rep/abs injection step
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   585
       - 3rd prem is the cleaning part
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   586
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   587
       the Quot_True premise in 2nd records the lifted theorem
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   588
    *)
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   589
    \<^instantiate>\<open>
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   590
      A = \<open>Thm.cterm_of ctxt rtrm'\<close> and
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   591
      B = \<open>Thm.cterm_of ctxt reg_goal\<close> and
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   592
      C = \<open>Thm.cterm_of ctxt inj_goal\<close>
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   593
    in
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   594
      lemma (schematic) "A \<Longrightarrow> A \<longrightarrow> B \<Longrightarrow> (Quot_True D \<Longrightarrow> B = C) \<Longrightarrow> C = D \<Longrightarrow> D"
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   595
        by (simp add: Quot_True_def)\<close>
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   596
  end
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   597
37593
2505feaf2d70 separated the lifting and descending procedures in the quotient package
Christian Urban <urbanc@in.tum.de>
parents: 37592
diff changeset
   598
38860
749d09f52fde quotient package: added a list of pre-simplification rules for Ball, Bex and mem
Christian Urban <urbanc@in.tum.de>
parents: 38859
diff changeset
   599
(* Since we use Ball and Bex during the lifting and descending,
38862
2795499a20bd Quotient Package: dont unfold mem_def, use rsp and prs instead
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 38860
diff changeset
   600
   we cannot deal with lemmas containing them, unless we unfold
2795499a20bd Quotient Package: dont unfold mem_def, use rsp and prs instead
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 38860
diff changeset
   601
   them by default. *)
38860
749d09f52fde quotient package: added a list of pre-simplification rules for Ball, Bex and mem
Christian Urban <urbanc@in.tum.de>
parents: 38859
diff changeset
   602
38862
2795499a20bd Quotient Package: dont unfold mem_def, use rsp and prs instead
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 38860
diff changeset
   603
val default_unfolds = @{thms Ball_def Bex_def}
38860
749d09f52fde quotient package: added a list of pre-simplification rules for Ball, Bex and mem
Christian Urban <urbanc@in.tum.de>
parents: 38859
diff changeset
   604
749d09f52fde quotient package: added a list of pre-simplification rules for Ball, Bex and mem
Christian Urban <urbanc@in.tum.de>
parents: 38859
diff changeset
   605
37593
2505feaf2d70 separated the lifting and descending procedures in the quotient package
Christian Urban <urbanc@in.tum.de>
parents: 37592
diff changeset
   606
(** descending as tactic **)
2505feaf2d70 separated the lifting and descending procedures in the quotient package
Christian Urban <urbanc@in.tum.de>
parents: 37592
diff changeset
   607
38859
053c69cb4a0e quotient package: lemmas to be lifted and descended can be pre-simplified
Christian Urban <urbanc@in.tum.de>
parents: 38719
diff changeset
   608
fun descend_procedure_tac ctxt simps =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   609
  let
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47308
diff changeset
   610
    val simpset = (mk_minimal_simpset ctxt) addsimps (simps @ default_unfolds)
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   611
  in
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47308
diff changeset
   612
    full_simp_tac simpset
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 52230
diff changeset
   613
    THEN' Object_Logic.full_atomize_tac ctxt
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   614
    THEN' gen_frees_tac ctxt
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   615
    THEN' SUBGOAL (fn (goal, i) =>
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   616
      let
45279
89a17197cb98 simplified/standardized signatures;
wenzelm
parents: 44285
diff changeset
   617
        val qtys = map #qtyp (Quotient_Info.dest_quotients ctxt)
41451
892e67be8304 do not open ML structures;
wenzelm
parents: 41444
diff changeset
   618
        val rtrm = Quotient_Term.derive_rtrm ctxt qtys goal
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   619
        val rule = procedure_inst ctxt rtrm  goal
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   620
      in
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   621
        resolve_tac ctxt [rule] i
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   622
      end)
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   623
  end
37593
2505feaf2d70 separated the lifting and descending procedures in the quotient package
Christian Urban <urbanc@in.tum.de>
parents: 37592
diff changeset
   624
38859
053c69cb4a0e quotient package: lemmas to be lifted and descended can be pre-simplified
Christian Urban <urbanc@in.tum.de>
parents: 38719
diff changeset
   625
fun descend_tac ctxt simps =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   626
  let
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   627
    val mk_tac_raw =
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   628
      descend_procedure_tac ctxt simps
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   629
      THEN' RANGE
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 52230
diff changeset
   630
        [Object_Logic.rulify_tac ctxt THEN' (K all_tac),
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   631
         regularize_tac ctxt,
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   632
         all_injection_tac ctxt,
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   633
         clean_tac ctxt]
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   634
  in
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   635
    Goal.conjunction_tac THEN_ALL_NEW mk_tac_raw
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   636
  end
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   637
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   638
45782
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   639
(** descending for partial equivalence relations **)
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   640
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   641
fun partiality_procedure_inst ctxt rtrm qtrm =
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   642
  let
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   643
    val rtrm' = HOLogic.dest_Trueprop rtrm
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   644
    val qtrm' = HOLogic.dest_Trueprop qtrm
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   645
    val reg_goal = Quotient_Term.regularize_trm_chk ctxt (rtrm', qtrm')
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   646
      handle Quotient_Term.LIFT_MATCH msg => lift_match_error ctxt msg rtrm qtrm
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   647
    val inj_goal = Quotient_Term.inj_repabs_trm_chk ctxt (reg_goal, qtrm')
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   648
      handle Quotient_Term.LIFT_MATCH msg => lift_match_error ctxt msg rtrm qtrm
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   649
  in
80679
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   650
    \<^instantiate>\<open>
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   651
      B = \<open>Thm.cterm_of ctxt reg_goal\<close> and
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   652
      C = \<open>Thm.cterm_of ctxt inj_goal\<close>
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   653
    in
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   654
      lemma (schematic) "B \<Longrightarrow> (Quot_True D \<Longrightarrow> B = C) \<Longrightarrow> C = D \<Longrightarrow> D"
fd69f98e2182 tuned: more antiquotations;
wenzelm
parents: 80678
diff changeset
   655
        by (simp add: Quot_True_def)\<close>
45782
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   656
  end
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   657
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   658
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   659
fun partiality_descend_procedure_tac ctxt simps =
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   660
  let
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47308
diff changeset
   661
    val simpset = (mk_minimal_simpset ctxt) addsimps (simps @ default_unfolds)
45782
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   662
  in
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47308
diff changeset
   663
    full_simp_tac simpset
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 52230
diff changeset
   664
    THEN' Object_Logic.full_atomize_tac ctxt
45782
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   665
    THEN' gen_frees_tac ctxt
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   666
    THEN' SUBGOAL (fn (goal, i) =>
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   667
      let
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   668
        val qtys = map #qtyp (Quotient_Info.dest_quotients ctxt)
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   669
        val rtrm = Quotient_Term.derive_rtrm ctxt qtys goal
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   670
        val rule = partiality_procedure_inst ctxt rtrm  goal
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   671
      in
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   672
        resolve_tac ctxt [rule] i
45782
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   673
      end)
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   674
  end
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   675
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   676
fun partiality_descend_tac ctxt simps =
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   677
  let
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   678
    val mk_tac_raw =
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   679
      partiality_descend_procedure_tac ctxt simps
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   680
      THEN' RANGE
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 52230
diff changeset
   681
        [Object_Logic.rulify_tac ctxt THEN' (K all_tac),
45782
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   682
         all_injection_tac ctxt,
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   683
         clean_tac ctxt]
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   684
  in
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   685
    Goal.conjunction_tac THEN_ALL_NEW mk_tac_raw
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   686
  end
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   687
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   688
f82020ca3248 added a specific tactic and method that deal with partial equivalence relations
Christian Urban <urbanc@in.tum.de>
parents: 45625
diff changeset
   689
38625
b97bd93fb9e2 allow for pre-simplification of lifted theorems
Christian Urban <urbanc@in.tum.de>
parents: 38624
diff changeset
   690
(** lifting as a tactic **)
37593
2505feaf2d70 separated the lifting and descending procedures in the quotient package
Christian Urban <urbanc@in.tum.de>
parents: 37592
diff changeset
   691
38718
c7cbbb18eabe tuned code
Christian Urban <urbanc@in.tum.de>
parents: 38717
diff changeset
   692
37593
2505feaf2d70 separated the lifting and descending procedures in the quotient package
Christian Urban <urbanc@in.tum.de>
parents: 37592
diff changeset
   693
(* the tactic leaves three subgoals to be proved *)
38859
053c69cb4a0e quotient package: lemmas to be lifted and descended can be pre-simplified
Christian Urban <urbanc@in.tum.de>
parents: 38719
diff changeset
   694
fun lift_procedure_tac ctxt simps rthm =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   695
  let
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47308
diff changeset
   696
    val simpset = (mk_minimal_simpset ctxt) addsimps (simps @ default_unfolds)
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   697
  in
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47308
diff changeset
   698
    full_simp_tac simpset
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 52230
diff changeset
   699
    THEN' Object_Logic.full_atomize_tac ctxt
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   700
    THEN' gen_frees_tac ctxt
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   701
    THEN' SUBGOAL (fn (goal, i) =>
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   702
      let
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   703
        (* full_atomize_tac contracts eta redexes,
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   704
           so we do it also in the original theorem *)
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   705
        val rthm' =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 47308
diff changeset
   706
          rthm |> full_simplify simpset
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   707
               |> Drule.eta_contraction_rule
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   708
               |> Thm.forall_intr_frees
54742
7a86358a3c0b proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
wenzelm
parents: 52230
diff changeset
   709
               |> atomize_thm ctxt
38717
a365f1fc5081 quotient package: deal correctly with frees in lifted theorems
Christian Urban <urbanc@in.tum.de>
parents: 38694
diff changeset
   710
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   711
        val rule = procedure_inst ctxt (Thm.prop_of rthm') goal
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   712
      in
60752
b48830b670a1 prefer tactics with explicit context;
wenzelm
parents: 60642
diff changeset
   713
        (resolve_tac ctxt [rule] THEN' resolve_tac ctxt [rthm']) i
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   714
      end)
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   715
  end
38625
b97bd93fb9e2 allow for pre-simplification of lifted theorems
Christian Urban <urbanc@in.tum.de>
parents: 38624
diff changeset
   716
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   717
fun lift_single_tac ctxt simps rthm =
38859
053c69cb4a0e quotient package: lemmas to be lifted and descended can be pre-simplified
Christian Urban <urbanc@in.tum.de>
parents: 38719
diff changeset
   718
  lift_procedure_tac ctxt simps rthm
38625
b97bd93fb9e2 allow for pre-simplification of lifted theorems
Christian Urban <urbanc@in.tum.de>
parents: 38624
diff changeset
   719
  THEN' RANGE
b97bd93fb9e2 allow for pre-simplification of lifted theorems
Christian Urban <urbanc@in.tum.de>
parents: 38624
diff changeset
   720
    [ regularize_tac ctxt,
b97bd93fb9e2 allow for pre-simplification of lifted theorems
Christian Urban <urbanc@in.tum.de>
parents: 38624
diff changeset
   721
      all_injection_tac ctxt,
b97bd93fb9e2 allow for pre-simplification of lifted theorems
Christian Urban <urbanc@in.tum.de>
parents: 38624
diff changeset
   722
      clean_tac ctxt ]
b97bd93fb9e2 allow for pre-simplification of lifted theorems
Christian Urban <urbanc@in.tum.de>
parents: 38624
diff changeset
   723
38859
053c69cb4a0e quotient package: lemmas to be lifted and descended can be pre-simplified
Christian Urban <urbanc@in.tum.de>
parents: 38719
diff changeset
   724
fun lift_tac ctxt simps rthms =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   725
  Goal.conjunction_tac
38859
053c69cb4a0e quotient package: lemmas to be lifted and descended can be pre-simplified
Christian Urban <urbanc@in.tum.de>
parents: 38719
diff changeset
   726
  THEN' RANGE (map (lift_single_tac ctxt simps) rthms)
38625
b97bd93fb9e2 allow for pre-simplification of lifted theorems
Christian Urban <urbanc@in.tum.de>
parents: 38624
diff changeset
   727
b97bd93fb9e2 allow for pre-simplification of lifted theorems
Christian Urban <urbanc@in.tum.de>
parents: 38624
diff changeset
   728
b97bd93fb9e2 allow for pre-simplification of lifted theorems
Christian Urban <urbanc@in.tum.de>
parents: 38624
diff changeset
   729
(* automated lifting with pre-simplification of the theorems;
b97bd93fb9e2 allow for pre-simplification of lifted theorems
Christian Urban <urbanc@in.tum.de>
parents: 38624
diff changeset
   730
   for internal usage *)
b97bd93fb9e2 allow for pre-simplification of lifted theorems
Christian Urban <urbanc@in.tum.de>
parents: 38624
diff changeset
   731
fun lifted ctxt qtys simps rthm =
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   732
  let
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   733
    val ((_, [rthm']), ctxt') = Variable.import true [rthm] ctxt
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 59498
diff changeset
   734
    val goal = Quotient_Term.derive_qtrm ctxt' qtys (Thm.prop_of rthm')
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   735
  in
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   736
    Goal.prove ctxt' [] [] goal
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   737
      (K (HEADGOAL (lift_single_tac ctxt' simps rthm')))
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 41451
diff changeset
   738
    |> singleton (Proof_Context.export ctxt' ctxt)
41444
7f40120cd814 more precise parentheses and indentation;
wenzelm
parents: 41443
diff changeset
   739
  end
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   740
37593
2505feaf2d70 separated the lifting and descending procedures in the quotient package
Christian Urban <urbanc@in.tum.de>
parents: 37592
diff changeset
   741
38625
b97bd93fb9e2 allow for pre-simplification of lifted theorems
Christian Urban <urbanc@in.tum.de>
parents: 38624
diff changeset
   742
(* lifting as an attribute *)
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   743
61853
fb7756087101 rule_attribute and declaration_attribute implicitly support abstract closure, but mixed_attribute implementations need to be aware of Thm.is_free_dummy;
wenzelm
parents: 61144
diff changeset
   744
val lifted_attrib = Thm.rule_attribute [] (fn context =>
37593
2505feaf2d70 separated the lifting and descending procedures in the quotient package
Christian Urban <urbanc@in.tum.de>
parents: 37592
diff changeset
   745
  let
2505feaf2d70 separated the lifting and descending procedures in the quotient package
Christian Urban <urbanc@in.tum.de>
parents: 37592
diff changeset
   746
    val ctxt = Context.proof_of context
45279
89a17197cb98 simplified/standardized signatures;
wenzelm
parents: 44285
diff changeset
   747
    val qtys = map #qtyp (Quotient_Info.dest_quotients ctxt)
37593
2505feaf2d70 separated the lifting and descending procedures in the quotient package
Christian Urban <urbanc@in.tum.de>
parents: 37592
diff changeset
   748
  in
38625
b97bd93fb9e2 allow for pre-simplification of lifted theorems
Christian Urban <urbanc@in.tum.de>
parents: 38624
diff changeset
   749
    lifted ctxt qtys []
37593
2505feaf2d70 separated the lifting and descending procedures in the quotient package
Christian Urban <urbanc@in.tum.de>
parents: 37592
diff changeset
   750
  end)
35222
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   751
4f1fba00f66d Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   752
end; (* structure *)