Presentation is loading. Please wait.

Presentation is loading. Please wait.

Narrowing: the scope of this project Steven Libby.

Similar presentations


Presentation on theme: "Narrowing: the scope of this project Steven Libby."— Presentation transcript:

1 Narrowing: the scope of this project Steven Libby

2 What is Narrowing? Narrowing is a method for finding solutions in Term Rewriting Systems It combines unification and evaluation If t narrows to r with rule l →r  u=mgu(t,l)  t → r u Each step has a unification and an evaluation

3 What is Term Rewriting? This is a better question l→r is a rewrite rule T is a term t| p is the subterm at position p of t Then t→ p,l→r t' replaces t| p with r to get t'

4 Umm... How about a picture

5 t = f x : map f xs p = 1. 1 l→r = f→id t' = f x : map id x t → 1. 1,f→id t'

6

7 Cool, how do we do it? The general problem is hard (undecidable) Limit ourselves to orthogonal systems We still need a narrowing strategy

8 Needed Narrowing Normal form: no more rewrite rules apply A step is “needed” if every derivation of a normal form includes that step Computing only needed steps would be optimal It turns out there is a strategy that does this

9 Detour through the forrest To compute the narrowing strategy we need a Definitional Tree Definitional Tree = Branch pattern [subtree] | Rule pattern l → r | Error pattern A definitional tree lets us pattern match to find the rule we're looking for

10 Example Definitional tree for  0 <= X → true  S(X) <= 0 → false  S(X) <= S(Y) → X <= Y

11 Example (Cont)

12 How do we make them > pdt :: Term Name -> Pos -> [Rule Name] -> PDT Name > pdt pat p [] = Error pat p > pdt pat p rs > | not (null rules) = renameRule (head rules) > | null cons = pdt pat [head p + 1] (vRules ++ tooShort) > | otherwise = Branch pat p $ branches ++ varBranch > where branches = map branch cons > branch cs = pdt (sub cs) (p++[0]) cs > rules = [Rule pat (init p) l r | (l:=>r) <- rs, pat ~= l] > vRules = filter unifies rs > tooShort = filter ends rs > cons = groupBy (leftEq p) (filter differs rs) > con l = Con (name l) (take (arity l) (newVars v p)) > sub ((l:=>_):_) = pat >>= ((v |-> con (l!>p))!) > (Var v) = pat!>p > exists l = arity (l!>(init p)) > last p > ends (l:=>r) = not (exists l) > unifies (l:=>r) = exists l && (l!>p) ~= (pat!>p) > differs (l:=>r) = exists l && not ((l!>p) ~= (pat!>p)) > varBranch = if not (null vRules) then [pdt pat [head p + 1] vRules] else []

13 Eww, can we Narrow things Now? Narrowing is actually a lot easier Walk the tree until we hit a rule or an error  At each branch we walk down one that unifies with t  If none of the branches unify we unify the pattern with t and try again If we hit a rule, then we replace t| p with r If we hit an error, then that's a failure

14 Narrowing Algorithm > narrow :: (Show v, Ord v) => Term v -> PDT v -> [PDT v] -> Step v > narrow t (Rule pat p l r) _ = [(p, l :=> r, u) | (Just u) <- [unify pat t]] > narrow t (Error pat p) _ = [(p, E, u) | (Just u) <- [unify pat t]] > narrow t (Branch pat p ts) dt > | not (null us) = [(p',r,s) | (ti,u) <- us, (p',r,s) <- narrow t ti dt] > | otherwise = [(p',r,s |=> tau) | (p',r,s) <- narrow t t' dt] > where us = [(ti,u) | ti <- ts, (Just u) <- [unify (pattern ti) t ]] > (Just tau) = unify pat t > t' = getTree ((t !> p) >>= (tau!)) dt

15 Conclusion Narrowing is a useful strategy It can be used to solve many/all of the problem we've looked at Implementing narrowing is doable  Make the definitional tree  Walk the tree and unify


Download ppt "Narrowing: the scope of this project Steven Libby."

Similar presentations


Ads by Google