TFL/examples/Subst/Unifier.ML
author paulson
Fri, 18 Oct 1996 12:54:19 +0200
changeset 2113 21266526ac42
permissions -rw-r--r--
Subst as modified by Konrad Slind
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2113
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
     1
(*  Title:      HOL/Subst/unifier.ML
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
     2
    ID:         $Id$
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
     3
    Author:     Martin Coen, Cambridge University Computer Laboratory
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
     4
    Copyright   1993  University of Cambridge
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
     5
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
     6
For Unifier.thy.
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
     7
Properties of unifiers.
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
     8
*)
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
     9
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    10
open Unifier;
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    11
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    12
val unify_defs = [Unifier_def,MoreGeneral_def,MGUnifier_def];
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    13
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    14
val rews = subst_rews@al_rews@setplus_rews;
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    15
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    16
(*---------------------------------------------------------------------------
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    17
 * Unifiers 
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    18
 *---------------------------------------------------------------------------*)
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    19
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    20
goalw Unifier.thy [Unifier_def] "Unifier s t u = (t <| s = u <| s)";
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    21
by (rtac refl 1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    22
val Unifier_iff = result();
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    23
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    24
goal Unifier.thy
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    25
    "Unifier s (Comb t u) (Comb v w) --> Unifier s t v & Unifier s u w";
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    26
by (simp_tac (!simpset addsimps [Unifier_iff]) 1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    27
val Unifier_Comb  = result() RS mp RS conjE;
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    28
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    29
goal Unifier.thy
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    30
  "~v : vars_of(t) --> ~v : vars_of(u) --> Unifier s t u --> \
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    31
\  Unifier ((v,r)#s) t u";
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    32
by (simp_tac (!simpset addsimps [Unifier_iff,repl_invariance]) 1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    33
val Cons_Unifier  = result() RS mp RS mp RS mp;
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    34
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    35
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    36
(*---------------------------------------------------------------------------
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    37
 * Most General Unifiers 
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    38
 *---------------------------------------------------------------------------*)
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    39
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    40
goalw Unifier.thy unify_defs "MGUnifier s t u = MGUnifier s u t";
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    41
by (safe_tac (HOL_cs addSEs ([sym]@([spec] RL [mp]))));
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    42
val mgu_sym = result();
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    43
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    44
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    45
goalw Unifier.thy [MoreGeneral_def]  "r >> s = (EX q. s =s= r <> q)";
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    46
by (rtac refl 1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    47
val MoreGen_iff = result();
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    48
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    49
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    50
goal Unifier.thy  "[] >> s";
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    51
by (simp_tac (!simpset addsimps (MoreGen_iff::subst_rews)) 1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    52
by (fast_tac (set_cs addIs [refl RS subst_refl]) 1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    53
val MoreGen_Nil = result();
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    54
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    55
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    56
goalw Unifier.thy unify_defs
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    57
    "MGUnifier s t u = (ALL r. Unifier r t u = s >> r)";
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    58
by (REPEAT (ares_tac [iffI,allI] 1 ORELSE 
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    59
            eresolve_tac [conjE,allE,mp,exE,ssubst_subst2] 1));
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    60
by (asm_simp_tac (!simpset addsimps [subst_comp]) 1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    61
by (fast_tac (set_cs addIs [comp_Nil RS sym RS subst_refl]) 1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    62
val MGU_iff = result();
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    63
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    64
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    65
val [prem] = goal Unifier.thy
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    66
     "~ Var(v) <: t ==> MGUnifier [(v,t)] (Var v) t";
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    67
by(simp_tac(HOL_ss addsimps([MGU_iff,MoreGen_iff,Unifier_iff]@rews))1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    68
by (REPEAT_SOME (step_tac set_cs));
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    69
by (etac subst 1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    70
by (etac ssubst_subst2 2);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    71
by (rtac (Cons_trivial RS subst_sym) 1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    72
by (simp_tac (!simpset addsimps ((prem RS Var_not_occs)::rews)) 1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    73
val MGUnifier_Var = result();
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    74
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    75
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    76
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    77
(*---------------------------------------------------------------------------
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    78
 * Idempotence.
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    79
 *---------------------------------------------------------------------------*)
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    80
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    81
goalw Unifier.thy [Idem_def] "Idem([])";
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    82
by (simp_tac (!simpset addsimps (refl RS subst_refl)::rews) 1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    83
qed "Idem_Nil";
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    84
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    85
goalw Unifier.thy [Idem_def] "Idem(s) = (sdom(s) Int srange(s) = {})";
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    86
by (simp_tac (!simpset addsimps [subst_eq_iff,subst_comp,
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    87
                                 invariance,dom_range_disjoint])1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    88
qed "Idem_iff";
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    89
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    90
val rews = subst_rews@al_rews@setplus_rews;
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    91
goal Unifier.thy "~ (Var(v) <: t) --> Idem([(v,t)])";
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    92
by (simp_tac (!simpset addsimps (vars_iff_occseq::Idem_iff::srange_iff::rews)
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    93
                        setloop (split_tac [expand_if])) 1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    94
by (fast_tac set_cs 1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    95
val Var_Idem  = store_thm("Var_Idem", result() RS mp);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    96
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    97
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    98
val [prem] = goalw Unifier.thy [Idem_def]
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
    99
"Idem(r) ==>  Unifier s (t<|r) (u<|r) --> Unifier (r <> s) (t <| r) (u <| r)";
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
   100
by (simp_tac (!simpset addsimps 
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
   101
              [Unifier_iff,subst_comp,prem RS comp_subst_subst]) 1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
   102
val Unifier_Idem_subst  = store_thm("Unifier_Idem_subst", result() RS mp);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
   103
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
   104
val [p1,p2,p3] = goal Unifier.thy
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
   105
     "[| Idem(r); Unifier s (t <| r) (u <| r); \
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
   106
\        (!q. Unifier q (t <| r) (u <| r) --> s <> q =s= q) \
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
   107
\     |] ==> Idem(r <> s)";
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
   108
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
   109
by (cut_facts_tac [p1,
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
   110
                   p2 RS (p1 RS Unifier_Idem_subst RS (p3 RS spec RS mp))] 1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
   111
by (REPEAT_SOME (etac rev_mp));
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
   112
by (simp_tac (!simpset addsimps [Idem_def,subst_eq_iff,subst_comp]) 1);
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
   113
qed "Idem_comp";
21266526ac42 Subst as modified by Konrad Slind
paulson
parents:
diff changeset
   114