Nominal/Tacs.thy
author Christian Urban <urbanc@in.tum.de>
Mon, 16 Aug 2010 17:39:16 +0800
changeset 2401 7645e18e8b19
parent 2302 c6db12ddb60c
permissions -rw-r--r--
modified the code for class instantiations (with help from Florian)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1653
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     1
theory Tacs
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     2
imports Main
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     3
begin
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     4
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     5
(* General not-nominal/quotient functionality useful for proving *)
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     6
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     7
(* A version of case_rule_tac that takes more exhaust rules *)
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     8
ML {*
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
     9
fun case_rules_tac ctxt0 s rules i st =
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    10
let
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    11
  val (_, ctxt) = Variable.focus_subgoal i st ctxt0;
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    12
  val ty = fastype_of (ProofContext.read_term_schematic ctxt s)
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    13
  fun exhaust_ty thm = fastype_of (hd (Induct.vars_of (Thm.term_of (Thm.cprem_of thm 1))));
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    14
  val ty_rules = filter (fn x => exhaust_ty x = ty) rules;
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    15
in
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    16
  InductTacs.case_rule_tac ctxt0 s (hd ty_rules) i st
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    17
end
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    18
*}
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    19
2302
c6db12ddb60c intermediate state
Christian Urban <urbanc@in.tum.de>
parents: 2300
diff changeset
    20
ML {*
c6db12ddb60c intermediate state
Christian Urban <urbanc@in.tum.de>
parents: 2300
diff changeset
    21
fun mk_conjl props =
c6db12ddb60c intermediate state
Christian Urban <urbanc@in.tum.de>
parents: 2300
diff changeset
    22
  fold (fn a => fn b =>
c6db12ddb60c intermediate state
Christian Urban <urbanc@in.tum.de>
parents: 2300
diff changeset
    23
    if a = @{term True} then b else
c6db12ddb60c intermediate state
Christian Urban <urbanc@in.tum.de>
parents: 2300
diff changeset
    24
    if b = @{term True} then a else
c6db12ddb60c intermediate state
Christian Urban <urbanc@in.tum.de>
parents: 2300
diff changeset
    25
    HOLogic.mk_conj (a, b)) (rev props) @{term True};
c6db12ddb60c intermediate state
Christian Urban <urbanc@in.tum.de>
parents: 2300
diff changeset
    26
*}
c6db12ddb60c intermediate state
Christian Urban <urbanc@in.tum.de>
parents: 2300
diff changeset
    27
c6db12ddb60c intermediate state
Christian Urban <urbanc@in.tum.de>
parents: 2300
diff changeset
    28
ML {*
c6db12ddb60c intermediate state
Christian Urban <urbanc@in.tum.de>
parents: 2300
diff changeset
    29
val split_conj_tac = REPEAT o etac conjE THEN' TRY o REPEAT_ALL_NEW (CHANGED o rtac conjI)
c6db12ddb60c intermediate state
Christian Urban <urbanc@in.tum.de>
parents: 2300
diff changeset
    30
*}
1653
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    31
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    32
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    33
ML {*
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    34
fun prove_by_rel_induct alphas build_goal ind utac inputs ctxt =
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    35
let
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    36
  val tys = map (domain_type o fastype_of) alphas;
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    37
  val names = Datatype_Prop.make_tnames tys;
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    38
  val (namesl, ctxt') = Variable.variant_fixes names ctxt;
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    39
  val (namesr, ctxt'') = Variable.variant_fixes names ctxt';
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    40
  val freesl = map Free (namesl ~~ tys);
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    41
  val freesr = map Free (namesr ~~ tys);
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    42
  val (gls_lists, ctxt'') = fold_map (build_goal (tys ~~ (freesl ~~ freesr))) inputs ctxt'';
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    43
  val gls = flat gls_lists;
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    44
  fun trm_gls_map t = filter (exists_subterm (fn s => s = t)) gls;
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    45
  val trm_gl_lists = map trm_gls_map freesl;
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    46
  val trm_gls = map mk_conjl trm_gl_lists;
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    47
  val pgls = map
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    48
    (fn ((alpha, gl), (l, r)) => HOLogic.mk_imp (alpha $ l $ r, gl)) 
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    49
    ((alphas ~~ trm_gls) ~~ (freesl ~~ freesr))
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    50
  val gl = HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj pgls);
2108
c5b7be27f105 Use raw_induct instead of induct
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 2049
diff changeset
    51
  fun tac {context,...} = (rtac ind THEN_ALL_NEW split_conj_tac THEN_ALL_NEW
1653
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    52
    TRY o rtac @{thm TrueI} THEN_ALL_NEW utac context) 1
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    53
  val th_loc = Goal.prove ctxt'' [] [] gl tac
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    54
  val ths_loc = HOLogic.conj_elims th_loc
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    55
  val ths = Variable.export ctxt'' ctxt ths_loc
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    56
in
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    57
  filter (fn x => not (prop_of x = prop_of @{thm TrueI})) ths
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    58
end
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    59
*}
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
    60
1656
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    61
ML {*
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    62
fun repeat_mp thm = repeat_mp (mp OF [thm]) handle THM _ => thm
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    63
*}
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    64
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    65
(* Introduces an implication and immediately eliminates it by cases *)
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    66
ML {*
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    67
fun imp_elim_tac case_rules =
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    68
  Subgoal.FOCUS (fn {concl, context, ...} =>
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    69
    case term_of concl of
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    70
      _ $ (_ $ asm $ _) =>
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    71
        let
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    72
          fun filter_fn case_rule = (
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    73
            case Logic.strip_assums_hyp (prop_of case_rule) of
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    74
              ((_ $ asmc) :: _) =>
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    75
                let
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    76
                  val thy = ProofContext.theory_of context
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    77
                in
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    78
                  Pattern.matches thy (asmc, asm)
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    79
                end
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    80
            | _ => false)
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    81
          val matching_rules = filter filter_fn case_rules
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    82
        in
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    83
         (rtac impI THEN' rotate_tac (~1) THEN' eresolve_tac matching_rules) 1
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    84
        end
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    85
    | _ => no_tac)
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    86
*}
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    87
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    88
ML {*
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    89
fun is_ex (Const ("Ex", _) $ Abs _) = true
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    90
  | is_ex _ = false;
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    91
*}
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    92
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    93
ML {*
2049
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
    94
fun dtyp_no_of_typ _ (TFree (_, _)) = error "dtyp_no_of_typ: Illegal free"
1656
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    95
  | dtyp_no_of_typ _ (TVar _) = error "dtyp_no_of_typ: Illegal schematic"
2049
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1656
diff changeset
    96
  | dtyp_no_of_typ dts (Type (tname, _)) =
1656
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    97
      case try (find_index (curry op = tname o fst)) dts of
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    98
        NONE => error "dtyp_no_of_typ: Illegal recursion"
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
    99
      | SOME i => i
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
   100
*}
c9d3dda79fe3 Removed remaining cheats + some cleaning.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents: 1653
diff changeset
   101
1653
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   102
end
a2142526bb01 Removed another cheat and cleaned the code a bit.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff changeset
   103