2Module : Idris.Transforms
3Description : A collection of transformations.
6Maintainer : The Idris Community.
9{-# LANGUAGE PatternGuards #-}
11module Idris.Transforms(
19import Idris.Core.CaseTree
24transformPats :: IState -> [Either Term (Term, Term)] ->
25 [Either Term (Term, Term)]
26transformPats ist ps = map tClause ps where
27 tClause (Left t) = Left t -- not a clause, leave it alone
28 tClause (Right (lhs, rhs)) -- apply transforms on RHS
29 = let rhs' = applyTransRules ist rhs in
32transformPatsWith :: [(Term, Term)] -> [Either Term (Term, Term)] ->
33 [Either Term (Term, Term)]
34transformPatsWith rs ps = map tClause ps where
35 tClause (Left t) = Left t -- not a clause, leave it alone
36 tClause (Right (lhs, rhs)) -- apply transforms on RHS
37 = let rhs' = applyTransRulesWith rs rhs in
40-- | Work on explicitly named terms, so we don't have to manipulate de
42applyTransRules :: IState -> Term -> Term
43applyTransRules ist tm = finalise $ applyAll [] (idris_transforms ist) (vToP tm)
45-- | Work on explicitly named terms, so we don't have to manipulate de
47applyTransRulesWith :: [(Term, Term)] -> Term -> Term
48applyTransRulesWith rules tm
49 = finalise $ applyAll rules emptyContext (vToP tm)
51applyAll :: [(Term, Term)] -> Ctxt [(Term, Term)] -> Term -> Term
52applyAll extra ts ap@(App s f a)
53 | (P _ fn ty, args) <- unApply ap
54 = let rules = case lookupCtxtExact fn ts of
57 ap' = App s (applyAll extra ts f) (applyAll extra ts a) in
60 rs -> case applyFnRules rs ap of
61 Just tm'@(App s f' a') ->
62 App s (applyAll extra ts f')
63 (applyAll extra ts a')
65 _ -> App s (applyAll extra ts f)
67applyAll extra ts (Bind n b sc) = Bind n (fmap (applyAll extra ts) b)
68 (applyAll extra ts sc)
69applyAll extra ts t = t
71applyFnRules :: [(Term, Term)] -> Term -> Maybe Term
72applyFnRules [] tm = Nothing
73applyFnRules (r : rs) tm | Just tm' <- applyRule r tm = Just tm'
74 | otherwise = applyFnRules rs tm
76applyRule :: (Term, Term) -> Term -> Maybe Term
77applyRule (lhs, rhs) tm
78 | Just ms <- matchTerm lhs tm
79-- = trace ("SUCCESS " ++ show ms ++ "\n FROM\n" ++ show lhs ++
81-- ++ "\n" ++ show tm ++ " GIVES\n" ++ show (depat ms rhs)) $
84-- ASSUMPTION: The names in the transformation rule bindings cannot occur
85-- in the term being transformed.
86-- (In general, this would not be true, but when we elaborate transformation
87-- rules we mangle the names so that it is true. While this feels a bit
88-- hacky, it's much easier to think about than mangling de Bruijn indices).
89 where depat ms (Bind n (PVar t) sc)
91 Just tm -> depat ms (subst n tm sc)
92 _ -> depat ms sc -- no occurrence? Shouldn't happen
95matchTerm :: Term -> Term -> Maybe [(Name, Term)]
96matchTerm lhs tm = matchVars [] lhs tm
98 matchVars acc (Bind n (PVar t) sc) tm
99 = matchVars (n : acc) (instantiate (P Bound n t) sc) tm
101 = -- trace (show acc ++ ": " ++ show (sc, tm)) $
104 doMatch :: [Name] -> Term -> Term -> Maybe [(Name, Term)]
105 doMatch ns (P _ n _) tm
106 | n `elem` ns = return [(n, tm)]
107 doMatch ns (App _ f a) (App _ f' a')
108 = do fm <- doMatch ns f f'
109 am <- doMatch ns a a'
111 doMatch ns x y | vToP x == vToP y = return []
112 | otherwise = Nothing
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list