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