src/HOLCF/Tools/adm_tac.ML
author wenzelm
Sun, 11 Jan 2009 21:49:59 +0100
changeset 29450 ac7f67be7f1f
parent 29355 642cac18e155
child 30603 71180005f251
permissions -rw-r--r--
tuned categories;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29355
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
     1
(*  Author:     Stefan Berghofer, TU Muenchen
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     2
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     3
Admissibility tactic.
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     4
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     5
Checks whether adm_subst theorem is applicable to the current proof
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     6
state:
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     7
29355
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
     8
  cont t ==> adm P ==> adm (%x. P (t x))
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
     9
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    10
"t" is instantiated with a term of chain-finite type, so that
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    11
adm_chfin can be applied:
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    12
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    13
  adm (P::'a::{chfin,pcpo} => bool)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    14
*)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    15
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    16
signature ADM =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    17
sig
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    18
  val adm_tac: (int -> tactic) -> int -> tactic
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    19
end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    20
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    21
structure Adm: ADM =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    22
struct
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    23
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    24
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    25
(*** find_subterms t 0 []
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    26
     returns lists of terms with the following properties:
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    27
       1. all terms in the list are disjoint subterms of t
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    28
       2. all terms contain the variable which is bound at level 0
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    29
       3. all occurences of the variable which is bound at level 0
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    30
          are "covered" by a term in the list
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    31
     a list of integers is associated with every term which describes
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    32
     the "path" leading to the subterm (required for instantiation of
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    33
     the adm_subst theorem (see functions mk_term, inst_adm_subst_thm))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    34
***)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    35
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    36
fun find_subterms (Bound i) lev path =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    37
      if i = lev then [[(Bound 0, path)]]
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    38
      else []
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    39
  | find_subterms (t as (Abs (_, _, t2))) lev path =
29355
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
    40
      if filter (fn x => x <= lev) (add_loose_bnos (t, 0, [])) = [lev]
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
    41
      then
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
    42
        [(incr_bv (~lev, 0, t), path)] ::
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    43
        (find_subterms t2 (lev+1) (0::path))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    44
      else find_subterms t2 (lev+1) (0::path)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    45
  | find_subterms (t as (t1 $ t2)) lev path =
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    46
      let val ts1 = find_subterms t1 lev (0::path);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    47
          val ts2 = find_subterms t2 lev (1::path);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    48
          fun combine [] y = []
29355
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
    49
            | combine (x::xs) ys = map (fn z => x @ z) ys @ combine xs ys
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    50
      in
29355
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
    51
        (if filter (fn x => x <= lev) (add_loose_bnos (t, 0, [])) = [lev]
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
    52
         then [[(incr_bv (~lev, 0, t), path)]]
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    53
         else []) @
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    54
        (if ts1 = [] then ts2
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    55
         else if ts2 = [] then ts1
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    56
         else combine ts1 ts2)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    57
      end
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    58
  | find_subterms _ _ _ = [];
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    59
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    60
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    61
(*** make term for instantiation of predicate "P" in adm_subst theorem ***)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    62
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    63
fun make_term t path paths lev =
29355
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
    64
  if member (op =) paths path then Bound lev
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    65
  else case t of
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    66
      (Abs (s, T, t1)) => Abs (s, T, make_term t1 (0::path) paths (lev+1))
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    67
    | (t1 $ t2) => (make_term t1 (0::path) paths lev) $
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    68
                   (make_term t2 (1::path) paths lev)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    69
    | t1 => t1;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    70
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    71
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    72
(*** check whether all terms in list are equal ***)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    73
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    74
fun eq_terms [] = true
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    75
  | eq_terms (ts as (t, _) :: _) = forall (fn (t2, _) => t2 aconv t) ts;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    76
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    77
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    78
(*** check whether type of terms in list is chain finite ***)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    79
29355
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
    80
fun is_chfin thy T params ((t, _)::_) =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    81
  let val parTs = map snd (rev params)
29355
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
    82
  in Sign.of_sort thy (fastype_of1 (T::parTs, t), @{sort "{chfin,pcpo}"}) end;
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    83
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    84
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    85
(*** try to prove that terms in list are continuous
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    86
     if successful, add continuity theorem to list l ***)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    87
29355
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
    88
fun prove_cont tac thy s T prems params (ts as ((t, _)::_)) l =
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    89
  let val parTs = map snd (rev params);
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    90
       val contT = (T --> (fastype_of1 (T::parTs, t))) --> HOLogic.boolT;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    91
       fun mk_all [] t = t
27331
5c66afff695e Term.all;
wenzelm
parents: 27155
diff changeset
    92
         | mk_all ((a,T)::Ts) t = Term.all T $ (Abs (a, T, mk_all Ts t));
29355
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
    93
       val t = HOLogic.mk_Trueprop (Const (@{const_name cont}, contT) $ Abs (s, T, t));
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    94
       val t' = mk_all params (Logic.list_implies (prems, t));
29355
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
    95
       val thm = Goal.prove (ProofContext.init thy) [] [] t' (K (tac 1));
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    96
  in (ts, thm)::l end
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    97
  handle ERROR _ => l;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    98
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
    99
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   100
(*** instantiation of adm_subst theorem (a bit tricky) ***)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   101
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   102
fun inst_adm_subst_thm state i params s T subt t paths =
26626
c6231d64d264 rep_cterm/rep_thm: no longer dereference theory_ref;
wenzelm
parents: 25804
diff changeset
   103
  let
29355
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   104
    val thy = Thm.theory_of_thm state;
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   105
    val j = Thm.maxidx_of state + 1;
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   106
    val parTs = map snd (rev params);
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   107
    val rule = Thm.lift_rule (Thm.cprem_of state i) @{thm adm_subst};
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   108
    val types = the o fst (Drule.types_sorts rule);
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   109
    val tT = types ("t", j);
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   110
    val PT = types ("P", j);
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   111
    fun mk_abs [] t = t
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   112
      | mk_abs ((a,T)::Ts) t = Abs (a, T, mk_abs Ts t);
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   113
    val tt = cterm_of thy (mk_abs (params @ [(s, T)]) subt);
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   114
    val Pt = cterm_of thy (mk_abs (params @ [(s, fastype_of1 (T::parTs, subt))])
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   115
                   (make_term t [] paths 0));
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   116
    val tye = Sign.typ_match thy (tT, #T (rep_cterm tt)) Vartab.empty;
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   117
    val tye' = Sign.typ_match thy (PT, #T (rep_cterm Pt)) tye;
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   118
    val ctye = map (fn (ixn, (S, T)) =>
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   119
      (ctyp_of thy (TVar (ixn, S)), ctyp_of thy T)) (Vartab.dest tye');
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   120
    val tv = cterm_of thy (Var (("t", j), Envir.typ_subst_TVars tye' tT));
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   121
    val Pv = cterm_of thy (Var (("P", j), Envir.typ_subst_TVars tye' PT));
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   122
    val rule' = instantiate (ctye, [(tv, tt), (Pv, Pt)]) rule
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   123
  in rule' end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   124
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   125
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   126
(*** the admissibility tactic ***)
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   127
29355
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   128
fun try_dest_adm (Const _ $ (Const (@{const_name adm}, _) $ Abs abs)) = SOME abs
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   129
  | try_dest_adm _ = NONE;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   130
29355
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   131
fun adm_tac tac i state = (i, state) |-> SUBGOAL (fn (goali, _) =>
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   132
  (case try_dest_adm (Logic.strip_assums_concl goali) of
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   133
    NONE => no_tac
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   134
  | SOME (s, T, t) =>
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   135
      let
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   136
        val thy = Thm.theory_of_thm state;
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   137
        val prems = Logic.strip_assums_hyp goali;
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   138
        val params = Logic.strip_params goali;
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   139
        val ts = find_subterms t 0 [];
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   140
        val ts' = filter eq_terms ts;
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   141
        val ts'' = filter (is_chfin thy T params) ts';
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   142
        val thms = fold (prove_cont tac thy s T prems params) ts'' [];
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   143
      in
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   144
        (case thms of
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   145
          ((ts as ((t', _)::_), cont_thm) :: _) =>
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   146
            let
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   147
              val paths = map snd ts;
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   148
              val rule = inst_adm_subst_thm state i params s T t' t paths;
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   149
            in
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   150
              compose_tac (false, rule, 2) i THEN
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   151
              resolve_tac [cont_thm] i THEN
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   152
              REPEAT (assume_tac i) THEN
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   153
              resolve_tac [@{thm adm_chfin}] i
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   154
            end
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   155
        | [] => no_tac)
642cac18e155 misc tuning and modernization;
wenzelm
parents: 27331
diff changeset
   156
      end));
23152
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   157
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   158
end;
9497234a2743 moved HOLCF tools to canonical place;
wenzelm
parents:
diff changeset
   159