@@ -50,6 +50,7 @@ data Tactic = Attack
5050 | Intro Name
5151 | Forall Name Raw
5252 | LetBind Name Raw Raw
53+ | Rewrite Raw
5354 | PatVar Name
5455 | PatBind Name
5556 | Focus Name
@@ -280,6 +281,31 @@ letbind n ty val ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
280281 return $ Bind n (Let tyv valv) (Bind x (Hole t) (P Bound x t))
281282letbind n ty val ctxt env _ = fail "Can't let bind here"
282283
284+rewrite :: Raw -> RunTactic
285+rewrite tm ctxt env (Bind x (Hole t) xp@(P _ x' _)) | x == x' =
286+ do (tmv, tmt) <- lift $ check ctxt env tm
287+ case unApply tmt of
288+ (P _ (UN ["="]) _, [lt,rt,l,r]) ->
289+ do let p = Bind rname (Lam lt) (mkP (P Bound rname lt) r l t)
290+ let newt = mkP l r l t
291+ return (Bind x (Hole newt)
292+ (mkApp (P Ref (UN ["replace"]) (Set 0))
293+ [lt, l, r, p, tmv, xp]))
294+ -- fail $ "Rewrite " ++ show l ++ " to " ++ show r ++ " in " ++
295+ -- show t ++ " with " ++ show (p, newt) ++ " not done yet"
296+ _ -> fail "Not an equality type"
297+ where
298+ -- to make the P for rewrite, replace syntactic occurrences of l in ty with
299+ -- and x, and put \x : lt in front
300+ mkP lt l r ty | l == ty = lt
301+ mkP lt l r (App f a) = let f' = if (r /= f) then mkP lt l r f else f
302+ a' = if (r /= a) then mkP lt l r a else a in
303+ App f' a'
304+ mkP lt l r x = x
305+
306+ rname = MN 0 "replaced"
307+rewrite _ _ _ _ = fail "Can't rewrite here"
308+
283309patbind :: Name -> RunTactic
284310patbind n ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
285311 do let t' = normalise ctxt env t
@@ -390,6 +416,7 @@ process t h = tactic (Just h) (mktac t)
390416 mktac (Intro n) = intro n
391417 mktac (Forall n t) = forall n t
392418 mktac (LetBind n t v) = letbind n t v
419+ mktac (Rewrite t) = rewrite t
393420 mktac (PatVar n) = patvar n
394421 mktac (PatBind n) = patbind n
395422 mktac (CheckIn r) = check_in r