src/HOL/Subst/Unify.ML
 author paulson Tue May 20 11:42:59 1997 +0200 (1997-05-20) changeset 3241 91b543ab091b parent 3209 ccb55f3121d1 child 3266 89e5f4163663 permissions -rw-r--r--
Removal of duplicate code from TFL
```     1 (*  Title:      Subst/Unify
```
```     2     Author:     Konrad Slind, Cambridge University Computer Laboratory
```
```     3     Copyright   1997  University of Cambridge
```
```     4
```
```     5 Unification algorithm
```
```     6 *)
```
```     7
```
```     8 (*---------------------------------------------------------------------------
```
```     9  * This file defines a nested unification algorithm, then proves that it
```
```    10  * terminates, then proves 2 correctness theorems: that when the algorithm
```
```    11  * succeeds, it 1) returns an MGU; and 2) returns an idempotent substitution.
```
```    12  * Although the proofs may seem long, they are actually quite direct, in that
```
```    13  * the correctness and termination properties are not mingled as much as in
```
```    14  * previous proofs of this algorithm.
```
```    15  *
```
```    16  * Our approach for nested recursive functions is as follows:
```
```    17  *
```
```    18  *    0. Prove the wellfoundedness of the termination relation.
```
```    19  *    1. Prove the non-nested termination conditions.
```
```    20  *    2. Eliminate (0) and (1) from the recursion equations and the
```
```    21  *       induction theorem.
```
```    22  *    3. Prove the nested termination conditions by using the induction
```
```    23  *       theorem from (2) and by using the recursion equations from (2).
```
```    24  *       These are constrained by the nested termination conditions, but
```
```    25  *       things work out magically (by wellfoundedness of the termination
```
```    26  *       relation).
```
```    27  *    4. Eliminate the nested TCs from the results of (2).
```
```    28  *    5. Prove further correctness properties using the results of (4).
```
```    29  *
```
```    30  * Deeper nestings require iteration of steps (3) and (4).
```
```    31  *---------------------------------------------------------------------------*)
```
```    32
```
```    33 open Unify;
```
```    34
```
```    35
```
```    36
```
```    37 (*---------------------------------------------------------------------------
```
```    38  * The non-nested TC plus the wellfoundedness of unifyRel.
```
```    39  *---------------------------------------------------------------------------*)
```
```    40 Tfl.tgoalw Unify.thy [] unify.rules;
```
```    41 (* Wellfoundedness of unifyRel *)
```
```    42 by (simp_tac (!simpset addsimps [unifyRel_def, uterm_less_def,
```
```    43 				 wf_inv_image, wf_lex_prod, wf_finite_psubset,
```
```    44 				 wf_rel_prod, wf_measure]) 1);
```
```    45 (* TC *)
```
```    46 by (Step_tac 1);
```
```    47 by (simp_tac (!simpset addsimps [finite_psubset_def, finite_vars_of,
```
```    48 				 lex_prod_def, measure_def,
```
```    49 				 inv_image_def]) 1);
```
```    50 by (rtac (monotone_vars_of RS (subset_iff_psubset_eq RS iffD1) RS disjE) 1);
```
```    51 by (Blast_tac 1);
```
```    52 by (asm_simp_tac (!simpset addsimps [rprod_def, less_eq, less_add_Suc1]) 1);
```
```    53 qed "tc0";
```
```    54
```
```    55
```
```    56 (*---------------------------------------------------------------------------
```
```    57  * Eliminate tc0 from the recursion equations and the induction theorem.
```
```    58  *---------------------------------------------------------------------------*)
```
```    59 val [wfr,tc] = Prim.Rules.CONJUNCTS tc0;
```
```    60 val unifyRules0 = map (normalize_thm [fn th => wfr RS th, fn th => tc RS th])
```
```    61                      unify.rules;
```
```    62
```
```    63 val unifyInduct0 = [wfr,tc] MRS unify.induct
```
```    64                   |> read_instantiate [("P","split Q")]
```
```    65                   |> rewrite_rule [split RS eq_reflection]
```
```    66                   |> standard;
```
```    67
```
```    68
```
```    69 (*---------------------------------------------------------------------------
```
```    70  * Termination proof.
```
```    71  *---------------------------------------------------------------------------*)
```
```    72
```
```    73 goalw Unify.thy [unifyRel_def,uterm_less_def,measure_def] "trans unifyRel";
```
```    74 by (REPEAT (resolve_tac [trans_inv_image, trans_lex_prod,
```
```    75 			 trans_finite_psubset, trans_less_than,
```
```    76 			 trans_rprod, trans_inv_image] 1));
```
```    77 qed "trans_unifyRel";
```
```    78
```
```    79
```
```    80 (*---------------------------------------------------------------------------
```
```    81  * The following lemma is used in the last step of the termination proof for
```
```    82  * the nested call in Unify.  Loosely, it says that unifyRel doesn't care
```
```    83  * about term structure.
```
```    84  *---------------------------------------------------------------------------*)
```
```    85 goalw Unify.thy [unifyRel_def,lex_prod_def, inv_image_def]
```
```    86      "!!x. ((X,Y), (Comb A (Comb B C), Comb D (Comb E F))) : unifyRel  ==> \
```
```    87     \      ((X,Y), (Comb (Comb A B) C, Comb (Comb D E) F)) : unifyRel";
```
```    88 by (asm_full_simp_tac (!simpset addsimps [uterm_less_def, measure_def, rprod_def,
```
```    89                           less_eq, inv_image_def,add_assoc]) 1);
```
```    90 by (subgoal_tac "(vars_of A Un vars_of B Un vars_of C Un \
```
```    91                 \  (vars_of D Un vars_of E Un vars_of F)) = \
```
```    92                 \ (vars_of A Un (vars_of B Un vars_of C) Un \
```
```    93                 \  (vars_of D Un (vars_of E Un vars_of F)))" 1);
```
```    94 by (Blast_tac 2);
```
```    95 by (Asm_simp_tac 1);
```
```    96 qed "Rassoc";
```
```    97
```
```    98
```
```    99 (*---------------------------------------------------------------------------
```
```   100  * This lemma proves the nested termination condition for the base cases
```
```   101  * 3, 4, and 6.
```
```   102  *---------------------------------------------------------------------------*)
```
```   103 goal Unify.thy
```
```   104  "!!x. ~(Var x <: M) ==>        \
```
```   105 \   ((N1 <| [(x,M)], N2 <| [(x,M)]), (Comb M N1, Comb(Var x) N2)) : unifyRel \
```
```   106 \ & ((N1 <| [(x,M)], N2 <| [(x,M)]), (Comb(Var x) N1, Comb M N2)) : unifyRel";
```
```   107 by (case_tac "Var x = M" 1);
```
```   108 by (hyp_subst_tac 1);
```
```   109 by (Simp_tac 1);
```
```   110 by (case_tac "x: (vars_of N1 Un vars_of N2)" 1);
```
```   111 (*uterm_less case*)
```
```   112 by (asm_simp_tac
```
```   113     (!simpset addsimps [less_eq, unifyRel_def, uterm_less_def,
```
```   114 			rprod_def, lex_prod_def,
```
```   115 			measure_def, inv_image_def]) 1);
```
```   116 by (Blast_tac 1);
```
```   117 (*finite_psubset case*)
```
```   118 by (simp_tac
```
```   119     (!simpset addsimps [unifyRel_def, lex_prod_def,
```
```   120 			measure_def, inv_image_def]) 1);
```
```   121 by (simp_tac (!simpset addsimps [finite_psubset_def, finite_vars_of,
```
```   122 				 psubset_def, set_eq_subset]) 1);
```
```   123 by (Blast_tac 1);
```
```   124 (** LEVEL 9 **)
```
```   125 (*Final case, also finite_psubset*)
```
```   126 by (simp_tac
```
```   127     (!simpset addsimps [finite_vars_of, unifyRel_def, finite_psubset_def,
```
```   128 			lex_prod_def, measure_def, inv_image_def]) 1);
```
```   129 by (cut_inst_tac [("s","[(x,M)]"), ("v", "x"), ("t","N2")] Var_elim 1);
```
```   130 by (cut_inst_tac [("s","[(x,M)]"), ("v", "x"), ("t","N1")] Var_elim 3);
```
```   131 by (ALLGOALS (asm_simp_tac(!simpset addsimps [srange_iff, vars_iff_occseq])));
```
```   132 by (REPEAT_FIRST (resolve_tac [conjI, disjI1, psubsetI]));
```
```   133 by (ALLGOALS (asm_full_simp_tac
```
```   134 	      (!simpset addsimps [srange_iff, set_eq_subset])));
```
```   135 by (ALLGOALS
```
```   136     (fast_tac (!claset addEs [Var_intro RS disjE]
```
```   137 	               addss (!simpset addsimps [srange_iff]))));
```
```   138 qed "var_elimR";
```
```   139
```
```   140
```
```   141 val Some{nchotomy = subst_nchotomy,...} = assoc(!datatypes,"subst");
```
```   142
```
```   143 (*---------------------------------------------------------------------------
```
```   144  * Do a case analysis on something of type 'a subst.
```
```   145  *---------------------------------------------------------------------------*)
```
```   146
```
```   147 fun subst_case_tac t =
```
```   148 (cut_inst_tac [("x",t)] (subst_nchotomy RS spec) 1
```
```   149   THEN etac disjE 1
```
```   150   THEN rotate_tac ~1 1
```
```   151   THEN Asm_full_simp_tac 1
```
```   152   THEN etac exE 1
```
```   153   THEN rotate_tac ~1 1
```
```   154   THEN Asm_full_simp_tac 1);
```
```   155
```
```   156
```
```   157 (*---------------------------------------------------------------------------
```
```   158  * The nested TC. Proved by recursion induction.
```
```   159  *---------------------------------------------------------------------------*)
```
```   160 val [tc1,tc2,tc3] = unify.tcs;
```
```   161 goalw_cterm [] (cterm_of (sign_of Unify.thy) (HOLogic.mk_Trueprop tc3));
```
```   162 (*---------------------------------------------------------------------------
```
```   163  * The extracted TC needs the scope of its quantifiers adjusted, so our
```
```   164  * first step is to restrict the scopes of N1 and N2.
```
```   165  *---------------------------------------------------------------------------*)
```
```   166 by (subgoal_tac "!M1 M2 theta.  \
```
```   167  \   unify(M1, M2) = Subst theta --> \
```
```   168  \   (!N1 N2. ((N1<|theta, N2<|theta), Comb M1 N1, Comb M2 N2) : unifyRel)" 1);
```
```   169 by (Blast_tac 1);
```
```   170 by (rtac allI 1);
```
```   171 by (rtac allI 1);
```
```   172 (* Apply induction *)
```
```   173 by (res_inst_tac [("v","M1"),("v1.0","M2")] unifyInduct0 1);
```
```   174 by (ALLGOALS
```
```   175     (asm_simp_tac (!simpset addsimps (var_elimR::unifyRules0)
```
```   176 			    setloop (split_tac [expand_if]))));
```
```   177 (*Const-Const case*)
```
```   178 by (simp_tac
```
```   179     (!simpset addsimps [unifyRel_def, lex_prod_def, measure_def,
```
```   180 			inv_image_def, less_eq, uterm_less_def, rprod_def]) 1);
```
```   181 (** LEVEL 7 **)
```
```   182 (*Comb-Comb case*)
```
```   183 by (subst_case_tac "unify(M1a, M2a)");
```
```   184 by (rename_tac "theta" 1);
```
```   185 by (subst_case_tac "unify(N1 <| theta, N2 <| theta)");
```
```   186 by (rename_tac "sigma" 1);
```
```   187 by (REPEAT (rtac allI 1));
```
```   188 by (rename_tac "P Q" 1);
```
```   189 by (rtac (trans_unifyRel RS transD) 1);
```
```   190 by (Blast_tac 1);
```
```   191 by (simp_tac (HOL_ss addsimps [subst_Comb RS sym]) 1);
```
```   192 by (subgoal_tac "((Comb N1 P <| theta, Comb N2 Q <| theta), \
```
```   193                 \ (Comb M1a (Comb N1 P), Comb M2a (Comb N2 Q))) :unifyRel" 1);
```
```   194 by (asm_simp_tac HOL_ss 2);
```
```   195 by (rtac Rassoc 1);
```
```   196 by (Blast_tac 1);
```
```   197 qed_spec_mp "unify_TC2";
```
```   198
```
```   199
```
```   200 (*---------------------------------------------------------------------------
```
```   201  * Now for elimination of nested TC from unify.rules and induction.
```
```   202  *---------------------------------------------------------------------------*)
```
```   203
```
```   204 (*Desired rule, copied from the theory file.  Could it be made available?*)
```
```   205 goal Unify.thy
```
```   206   "unify(Comb M1 N1, Comb M2 N2) =      \
```
```   207 \      (case unify(M1,M2)               \
```
```   208 \        of Fail => Fail                \
```
```   209 \         | Subst theta => (case unify(N1 <| theta, N2 <| theta)        \
```
```   210 \                            of Fail => Fail    \
```
```   211 \                             | Subst sigma => Subst (theta <> sigma)))";
```
```   212 by (asm_simp_tac (!simpset addsimps unifyRules0) 1);
```
```   213 by (subst_case_tac "unify(M1, M2)");
```
```   214 by (asm_simp_tac (!simpset addsimps [unify_TC2]) 1);
```
```   215 qed "unifyCombComb";
```
```   216
```
```   217
```
```   218 val unifyRules = rev (unifyCombComb :: tl (rev unifyRules0));
```
```   219
```
```   220 Addsimps unifyRules;
```
```   221
```
```   222 val prems = goal Unify.thy
```
```   223 "   [| !!m n. Q (Const m) (Const n);      \
```
```   224 \      !!m M N. Q (Const m) (Comb M N); !!m x. Q (Const m) (Var x);     \
```
```   225 \      !!x M. Q (Var x) M; !!M N x. Q (Comb M N) (Const x);     \
```
```   226 \      !!M N x. Q (Comb M N) (Var x);   \
```
```   227 \      !!M1 N1 M2 N2.   \
```
```   228 \         (! theta.     \
```
```   229 \             unify (M1, M2) = Subst theta -->  \
```
```   230 \             Q (N1 <| theta) (N2 <| theta)) & Q M1 M2   \
```
```   231 \         ==> Q (Comb M1 N1) (Comb M2 N2) |] ==> Q M1 M2";
```
```   232 by (res_inst_tac [("v","M1"),("v1.0","M2")] unifyInduct0 1);
```
```   233 by (ALLGOALS (asm_simp_tac (!simpset addsimps (unify_TC2::prems))));
```
```   234 qed "unifyInduct";
```
```   235
```
```   236
```
```   237
```
```   238 (*---------------------------------------------------------------------------
```
```   239  * Correctness. Notice that idempotence is not needed to prove that the
```
```   240  * algorithm terminates and is not needed to prove the algorithm correct,
```
```   241  * if you are only interested in an MGU.  This is in contrast to the
```
```   242  * approach of M&W, who used idempotence and MGU-ness in the termination proof.
```
```   243  *---------------------------------------------------------------------------*)
```
```   244
```
```   245 goal Unify.thy "!theta. unify(P,Q) = Subst theta --> MGUnifier theta P Q";
```
```   246 by (res_inst_tac [("M1.0","P"),("M2.0","Q")] unifyInduct 1);
```
```   247 by (ALLGOALS (asm_simp_tac (!simpset setloop split_tac [expand_if])));
```
```   248 (*Const-Const case*)
```
```   249 by (simp_tac (!simpset addsimps [MGUnifier_def,Unifier_def]) 1);
```
```   250 (*Const-Var case*)
```
```   251 by (stac mgu_sym 1);
```
```   252 by (simp_tac (!simpset addsimps [MGUnifier_Var]) 1);
```
```   253 (*Var-M case*)
```
```   254 by (simp_tac (!simpset addsimps [MGUnifier_Var]) 1);
```
```   255 (*Comb-Var case*)
```
```   256 by (stac mgu_sym 1);
```
```   257 by (simp_tac (!simpset addsimps [MGUnifier_Var]) 1);
```
```   258 (*Comb-Comb case*)
```
```   259 by (safe_tac (!claset));
```
```   260 by (subst_case_tac "unify(M1, M2)");
```
```   261 by (subst_case_tac "unify(N1<|list, N2<|list)");
```
```   262 by (hyp_subst_tac 1);
```
```   263 by (asm_full_simp_tac (!simpset addsimps [MGUnifier_def, Unifier_def])1);
```
```   264 (** LEVEL 13 **)
```
```   265 by (safe_tac (!claset));
```
```   266 by (rename_tac "theta sigma gamma" 1);
```
```   267 by (rewrite_goals_tac [MoreGeneral_def]);
```
```   268 by (rotate_tac ~3 1);
```
```   269 by (eres_inst_tac [("x","gamma")] allE 1);
```
```   270 by (Asm_full_simp_tac 1);
```
```   271 by (etac exE 1);
```
```   272 by (rename_tac "delta" 1);
```
```   273 by (eres_inst_tac [("x","delta")] allE 1);
```
```   274 by (subgoal_tac "N1 <| theta <| delta = N2 <| theta <| delta" 1);
```
```   275 (*Proving the subgoal*)
```
```   276 by (full_simp_tac (!simpset addsimps [subst_eq_iff]) 2);
```
```   277 by (blast_tac (!claset addIs [trans,sym] delrules [impCE]) 2);
```
```   278 by (blast_tac (!claset addIs [subst_trans, subst_cong,
```
```   279 			      comp_assoc RS subst_sym]) 1);
```
```   280 qed_spec_mp "unify_gives_MGU";
```
```   281
```
```   282
```
```   283 (*---------------------------------------------------------------------------
```
```   284  * Unify returns idempotent substitutions, when it succeeds.
```
```   285  *---------------------------------------------------------------------------*)
```
```   286 goal Unify.thy "!theta. unify(P,Q) = Subst theta --> Idem theta";
```
```   287 by (res_inst_tac [("M1.0","P"),("M2.0","Q")] unifyInduct 1);
```
```   288 by (ALLGOALS (asm_simp_tac (!simpset addsimps [Var_Idem]
```
```   289 			             setloop split_tac[expand_if])));
```
```   290 (*Comb-Comb case*)
```
```   291 by (safe_tac (!claset));
```
```   292 by (subst_case_tac "unify(M1, M2)");
```
```   293 by (subst_case_tac "unify(N1 <| list, N2 <| list)");
```
```   294 by (hyp_subst_tac 1);
```
```   295 by prune_params_tac;
```
```   296 by (rename_tac "theta sigma" 1);
```
```   297 (** LEVEL 8 **)
```
```   298 by (dtac unify_gives_MGU 1);
```
```   299 by (dtac unify_gives_MGU 1);
```
```   300 by (rewrite_tac [MGUnifier_def]);
```
```   301 by (safe_tac (!claset));
```
```   302 by (rtac Idem_comp 1);
```
```   303 by (atac 1);
```
```   304 by (atac 1);
```
```   305
```
```   306 by (eres_inst_tac [("x","q")] allE 1);
```
```   307 by (asm_full_simp_tac (!simpset addsimps [MoreGeneral_def]) 1);
```
```   308 by (safe_tac (!claset));
```
```   309 by (asm_full_simp_tac
```
```   310     (!simpset addsimps [srange_iff, subst_eq_iff, Idem_def]) 1);
```
```   311 qed_spec_mp "unify_gives_Idem";
```
```   312
```