2Module : Idris.Transforms
3Description : A collection of transformations.
6Maintainer : The Idris Community.
9{-# LANGUAGE PatternGuards #-}
11module Idris.Transforms(
21transformPats :: IState -> [Either Term (Term, Term)] ->
22 [Either Term (Term, Term)]
23transformPats ist ps = map tClause ps where
24 tClause (Left t) = Left t -- not a clause, leave it alone
25 tClause (Right (lhs, rhs)) -- apply transforms on RHS
26 = let rhs' = applyTransRules ist rhs in
29transformPatsWith :: [(Term, Term)] -> [Either Term (Term, Term)] ->
30 [Either Term (Term, Term)]
31transformPatsWith rs ps = map tClause ps where
32 tClause (Left t) = Left t -- not a clause, leave it alone
33 tClause (Right (lhs, rhs)) -- apply transforms on RHS
34 = let rhs' = applyTransRulesWith rs rhs in
37-- | Work on explicitly named terms, so we don't have to manipulate de
39applyTransRules :: IState -> Term -> Term
40applyTransRules ist tm = finalise $ applyAll [] (idris_transforms ist) (vToP tm)
42-- | Work on explicitly named terms, so we don't have to manipulate de
44applyTransRulesWith :: [(Term, Term)] -> Term -> Term
45applyTransRulesWith rules tm
46 = finalise $ applyAll rules emptyContext (vToP tm)
48applyAll :: [(Term, Term)] -> Ctxt [(Term, Term)] -> Term -> Term
49applyAll extra ts ap@(App s f a)
50 | (P _ fn ty, args) <- unApply ap
51 = let rules = case lookupCtxtExact fn ts of
54 ap' = App s (applyAll extra ts f) (applyAll extra ts a) in
57 rs -> case applyFnRules rs ap of
58 Just tm'@(App s f' a') ->
59 App s (applyAll extra ts f')
60 (applyAll extra ts a')
62 _ -> App s (applyAll extra ts f)
64applyAll extra ts (Bind n b sc) = Bind n (fmap (applyAll extra ts) b)
65 (applyAll extra ts sc)
66applyAll extra ts t = t
68applyFnRules :: [(Term, Term)] -> Term -> Maybe Term
69applyFnRules [] tm = Nothing
70applyFnRules (r : rs) tm | Just tm' <- applyRule r tm = Just tm'
71 | otherwise = applyFnRules rs tm
73applyRule :: (Term, Term) -> Term -> Maybe Term
74applyRule (lhs, rhs) tm
75 | Just ms <- matchTerm lhs tm
76-- = trace ("SUCCESS " ++ show ms ++ "\n FROM\n" ++ show lhs ++
78-- ++ "\n" ++ show tm ++ " GIVES\n" ++ show (depat ms rhs)) $
81-- ASSUMPTION: The names in the transformation rule bindings cannot occur
82-- in the term being transformed.
83-- (In general, this would not be true, but when we elaborate transformation
84-- rules we mangle the names so that it is true. While this feels a bit
85-- hacky, it's much easier to think about than mangling de Bruijn indices).
86 where depat ms (Bind n (PVar _ t) sc)
88 Just tm -> depat ms (subst n tm sc)
89 _ -> depat ms sc -- no occurrence? Shouldn't happen
92matchTerm :: Term -> Term -> Maybe [(Name, Term)]
93matchTerm lhs tm = matchVars [] lhs tm
95 matchVars acc (Bind n (PVar _ t) sc) tm
96 = matchVars (n : acc) (instantiate (P Bound n t) sc) tm
98 = -- trace (show acc ++ ": " ++ show (sc, tm)) $
101 doMatch :: [Name] -> Term -> Term -> Maybe [(Name, Term)]
102 doMatch ns (P _ n _) tm
103 | n `elem` ns = return [(n, tm)]
104 doMatch ns (App _ f a) (App _ f' a')
105 = do fm <- doMatch ns f f'
106 am <- doMatch ns a a'
108 doMatch ns x y | vToP x == vToP y = return []
109 | 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