1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
(* ========================================================================= *)
(* Knuth-Bendix completion done by HOL inference. John Harrison 2005 *)
(* *)
(* This was written by fairly mechanical modification of the code at *)
(* *)
(* http://www.cl.cam.ac.uk/users/jrh/atp/order.ml *)
(* http://www.cl.cam.ac.uk/users/jrh/atp/completion.ml *)
(* *)
(* for HOL's slightly different term structure, with ad hoc term *)
(* manipulations replaced by inference on equational theorems. We also have *)
(* the optimization of throwing left-reducible rules back into the set of *)
(* critical pairs. However, we don't prioritize smaller critical pairs or *)
(* anything like that; this is still a very naive implementation. *)
(* *)
(* For something very similar done 15 years ago, see Konrad Slind's Master's *)
(* thesis: "An Implementation of Higher Order Logic", U Calgary 1991. *)
(* ========================================================================= *)
let is_realvar w x = is_var x && not(mem x w);;
let rec real_strip w tm =
if mem tm w then tm,[] else
let l,r = dest_comb tm in
let f,args = real_strip w l in f,args@[r];;
(* ------------------------------------------------------------------------- *)
(* Construct a weighting function. *)
(* ------------------------------------------------------------------------- *)
let weight lis (f,n) (g,m) =
let i = index f lis and j = index g lis in
i > j || i = j && n > m;;
(* ------------------------------------------------------------------------- *)
(* Generic lexicographic ordering function. *)
(* ------------------------------------------------------------------------- *)
let rec lexord ord l1 l2 =
match (l1,l2) with
(h1::t1,h2::t2) -> if ord h1 h2 then length t1 = length t2
else h1 = h2 && lexord ord t1 t2
| _ -> false;;
(* ------------------------------------------------------------------------- *)
(* Lexicographic path ordering. Note that we also use the weights *)
(* to define the set of constants, so they don't literally have to be *)
(* constants in the HOL sense. *)
(* ------------------------------------------------------------------------- *)
let rec lpo_gt w s t =
if is_realvar w t then not(s = t) && mem t (frees s)
else if is_realvar w s || is_abs s || is_abs t then false else
let f,fargs = real_strip w s and g,gargs = real_strip w t in
exists (fun si -> lpo_ge w si t) fargs ||
forall (lpo_gt w s) gargs &
(f = g && lexord (lpo_gt w) fargs gargs ||
weight w (f,length fargs) (g,length gargs))
and lpo_ge w s t = (s = t) || lpo_gt w s t;;
(* ------------------------------------------------------------------------- *)
(* Unification. Again we have the weights "w" fixing the set of constants. *)
(* ------------------------------------------------------------------------- *)
let rec istriv w env x t =
if is_realvar w t then t = x || defined env t && istriv w env x (apply env t)
else if is_const t then false else
let f,args = strip_comb t in
exists (istriv w env x) args && failwith "cyclic";;
let rec unify w env tp =
match tp with
((Var(_,_) as x),t) | (t,(Var(_,_) as x)) when not(mem x w) ->
if defined env x then unify w env (apply env x,t)
else if istriv w env x t then env else (x|->t) env
| (Comb(f,x),Comb(g,y)) -> unify w (unify w env (x,y)) (f,g)
| (s,t) -> if s = t then env else failwith "unify: not unifiable";;
(* ------------------------------------------------------------------------- *)
(* Full unification, unravelling graph into HOL-style instantiation list. *)
(* ------------------------------------------------------------------------- *)
let fullunify w (s,t) =
let env = unify w undefined (s,t) in
let th = map (fun (x,t) -> (t,x)) (graph env) in
let rec subs t =
let t' = vsubst th t in
if t' = t then t else subs t' in
map (fun (t,x) -> (subs t,x)) th;;
(* ------------------------------------------------------------------------- *)
(* Construct "overlaps": ways of rewriting subterms using unification. *)
(* ------------------------------------------------------------------------- *)
let LIST_MK_COMB f ths = rev_itlist (fun s t -> MK_COMB(t,s)) ths (REFL f);;
let rec listcases fn rfn lis acc =
match lis with
[] -> acc
| h::t -> fn h (fun i h' -> rfn i (h'::map REFL t)) @
listcases fn (fun i t' -> rfn i (REFL h::t')) t acc;;
let rec overlaps w th tm rfn =
let l,r = dest_eq(concl th) in
if not (is_comb tm) then [] else
let f,args = strip_comb tm in
listcases (overlaps w th) (fun i a -> rfn i (LIST_MK_COMB f a)) args
(try [rfn (fullunify w (l,tm)) th] with Failure _ -> []);;
(* ------------------------------------------------------------------------- *)
(* Rename variables canonically to avoid clashes or remove redundancy. *)
(* ------------------------------------------------------------------------- *)
let fixvariables s th =
let fvs = subtract (frees(concl th)) (freesl(hyp th)) in
let gvs = map2 (fun v n -> mk_var(s^string_of_int n,type_of v))
fvs (1--(length fvs)) in
INST (zip gvs fvs) th;;
let renamepair (th1,th2) = fixvariables "x" th1,fixvariables "y" th2;;
(* ------------------------------------------------------------------------- *)
(* Find all critical pairs. *)
(* ------------------------------------------------------------------------- *)
let crit1 w eq1 eq2 =
let l1,r1 = dest_eq(concl eq1)
and l2,r2 = dest_eq(concl eq2) in
overlaps w eq1 l2 (fun i th -> TRANS (SYM(INST i th)) (INST i eq2));;
let thm_union l1 l2 =
itlist (fun th ths -> let th' = fixvariables "x" th in
let tm = concl th' in
if exists (fun th'' -> concl th'' = tm) ths then ths
else th'::ths)
l1 l2;;
let critical_pairs w tha thb =
let th1,th2 = renamepair (tha,thb) in
if concl th1 = concl th2 then crit1 w th1 th2 else
filter (fun th -> let l,r = dest_eq(concl th) in l <> r)
(thm_union (crit1 w th1 th2) (thm_union (crit1 w th2 th1) []));;
(* ------------------------------------------------------------------------- *)
(* Normalize an equation and try to orient it. *)
(* ------------------------------------------------------------------------- *)
let normalize_and_orient w eqs th =
let th' = GEN_REWRITE_RULE TOP_DEPTH_CONV eqs th in
let s',t' = dest_eq(concl th') in
if lpo_ge w s' t' then th' else if lpo_ge w t' s' then SYM th'
else failwith "Can't orient equation";;
(* ------------------------------------------------------------------------- *)
(* Print out status report to reduce user boredom. *)
(* ------------------------------------------------------------------------- *)
let status(eqs,crs) eqs0 =
if eqs = eqs0 && (length crs) mod 1000 <> 0 then () else
(print_string(string_of_int(length eqs)^" equations and "^
string_of_int(length crs)^" pending critical pairs");
print_newline());;
(* ------------------------------------------------------------------------- *)
(* Basic completion, throwing back left-reducible rules. *)
(* ------------------------------------------------------------------------- *)
let left_reducible eqs eq =
can (CHANGED_CONV(GEN_REWRITE_CONV (LAND_CONV o ONCE_DEPTH_CONV) eqs))
(concl eq);;
let rec complete w (eqs,crits) =
match crits with
(eq::ocrits) ->
let trip =
try let eq' = normalize_and_orient w eqs eq in
let s',t' = dest_eq(concl eq') in
if s' = t' then (eqs,ocrits) else
let crits',eqs' = partition(left_reducible [eq']) eqs in
let eqs'' = eq'::eqs' in
eqs'',
ocrits @ crits' @ itlist ((@) o critical_pairs w eq') eqs'' []
with Failure _ ->
if exists (can (normalize_and_orient w eqs)) ocrits
then (eqs,ocrits@[eq])
else failwith "complete: no orientable equations" in
status trip eqs; complete w trip
| [] -> eqs;;
(* ------------------------------------------------------------------------- *)
(* Overall completion. *)
(* ------------------------------------------------------------------------- *)
let complete_equations wts eqs =
let eqs' = map (normalize_and_orient wts []) eqs in
complete wts ([],eqs');;
(* ------------------------------------------------------------------------- *)
(* Knuth-Bendix example 4: the inverse property. *)
(* ------------------------------------------------------------------------- *)
complete_equations [`1`; `(*):num->num->num`; `i:num->num`]
[SPEC_ALL(ASSUME `!a b. i(a) * a * b = b`)];;
(* ------------------------------------------------------------------------- *)
(* Knuth-Bendix example 6: central groupoids. *)
(* ------------------------------------------------------------------------- *)
complete_equations [`(*):num->num->num`]
[SPEC_ALL(ASSUME `!a b c. (a * b) * (b * c) = b`)];;
(* ------------------------------------------------------------------------- *)
(* Knuth-Bendix example 9: cancellation law. *)
(* ------------------------------------------------------------------------- *)
complete_equations
[`1`; `( * ):num->num->num`; `(+):num->num->num`; `(-):num->num->num`]
(map SPEC_ALL (CONJUNCTS (ASSUME
`(!a b:num. a - a * b = b) /\
(!a b:num. a * b - b = a) /\
(!a. a * 1 = a) /\
(!a. 1 * a = a)`)));;
(* ------------------------------------------------------------------------- *)
(* Another example: pure congruence closure (no variables). *)
(* ------------------------------------------------------------------------- *)
complete_equations [`c:A`; `f:A->A`]
(map SPEC_ALL (CONJUNCTS (ASSUME
`((f(f(f(f(f c))))) = c:A) /\ (f(f(f c)) = c)`)));;
(* ------------------------------------------------------------------------- *)
(* Knuth-Bendix example 1: group theory. *)
(* ------------------------------------------------------------------------- *)
let eqs = map SPEC_ALL (CONJUNCTS (ASSUME
`(!x. 1 * x = x) /\ (!x. i(x) * x = 1) /\
(!x y z. (x * y) * z = x * y * z)`));;
complete_equations [`1`; `(*):num->num->num`; `i:num->num`] eqs;;
(* ------------------------------------------------------------------------- *)
(* Near-rings (from Aichinger's Diplomarbeit). *)
(* ------------------------------------------------------------------------- *)
let eqs = map SPEC_ALL (CONJUNCTS (ASSUME
`(!x. 0 + x = x) /\
(!x. neg x + x = 0) /\
(!x y z. (x + y) + z = x + y + z) /\
(!x y z. (x * y) * z = x * y * z) /\
(!x y z. (x + y) * z = (x * z) + (y * z))`));;
let nreqs =
complete_equations
[`0`; `(+):num->num->num`; `neg:num->num`; `( * ):num->num->num`] eqs;;
(*** This weighting also works OK, though the system is a bit bigger
let nreqs =
complete_equations
[`0`; `(+):num->num->num`; `( * ):num->num->num`; `INV`] eqs;;
****)
(* ------------------------------------------------------------------------- *)
(* A "completion" tactic. *)
(* ------------------------------------------------------------------------- *)
let COMPLETE_TAC w th =
let eqs = map SPEC_ALL (CONJUNCTS(SPEC_ALL th)) in
let eqs' = complete_equations w eqs in
MAP_EVERY (ASSUME_TAC o GEN_ALL) eqs';;
(* ------------------------------------------------------------------------- *)
(* Solve example problems in groups and near-rings. *)
(* ------------------------------------------------------------------------- *)
g `(!x. 1 * x = x) /\
(!x. i(x) * x = 1) /\
(!x y z. (x * y) * z = x * y * z)
==> !x y. i(y) * i(i(i(x * i(y)))) * x = 1`;;
e (DISCH_THEN(COMPLETE_TAC [`1`; `(*):num->num->num`; `i:num->num`]));;
e (ASM_REWRITE_TAC[]);;
g `(!x. 0 + x = x) /\
(!x. neg x + x = 0) /\
(!x y z. (x + y) + z = x + y + z) /\
(!x y z. (x * y) * z = x * y * z) /\
(!x y z. (x + y) * z = (x * z) + (y * z))
==> (neg 0 * (x * y + z + neg(neg(w + z))) + neg(neg b + neg a) =
a + b)`;;
e (DISCH_THEN(COMPLETE_TAC
[`0`; `(+):num->num->num`; `neg:num->num`; `( * ):num->num->num`]));;
e (ASM_REWRITE_TAC[]);;
|