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