1module Generate.Cases (toMatch, Match (..), Clause (..), matchSubst, newVar) where
3import Control.Applicative ((<$>))
4import Control.Arrow (first)
5import Control.Monad.State
6import Data.List (groupBy,sortBy)
7import Data.Maybe (fromMaybe)
9import SourceSyntax.Location
10import SourceSyntax.Literal
11import SourceSyntax.Pattern
12import SourceSyntax.Expression
13import Transform.Substitute
16toMatch :: [(Pattern, LExpr)] -> State Int (String, Match)
19 (,) v <$> match [v] (map (first (:[])) patterns) Fail
21newVar :: State Int String
24 return $ "_v" ++ show n
27 = Match String [Clause] Match
35 Clause (Either String Literal) [String] Match
38matchSubst :: [(String,String)] -> Match -> Match
39matchSubst _ Break = Break
40matchSubst _ Fail = Fail
41matchSubst pairs (Seq ms) = Seq (map (matchSubst pairs) ms)
42matchSubst pairs (Other (L s e)) =
43 Other . L s $ foldr ($) e $ map (\(x,y) -> subst x (Var y)) pairs
44matchSubst pairs (Match n cs m) =
45 Match (varSubst n) (map clauseSubst cs) (matchSubst pairs m)
46 where varSubst v = fromMaybe v (lookup v pairs)
47 clauseSubst (Clause c vs m) =
48 Clause c (map varSubst vs) (matchSubst pairs m)
56isVar p = not (isCon p)
58match :: [String] -> [([Pattern],LExpr)] -> Match -> State Int Match
59match [] [] def = return def
60match [] [([],e)] Fail = return $ Other e
61match [] [([],e)] Break = return $ Other e
62match [] cs def = return $ Seq (map (Other . snd) cs ++ [def])
64 | all isVar cs' = matchVar vs cs' def
65 | all isCon cs' = matchCon vs cs' def
66 | otherwise = matchMix vs cs' def
68 cs' = map (dealias v) cs
70dealias v c@(p:ps, L s e) =
72 PAlias x pattern -> (pattern:ps, L s $ subst x (Var v) e)
75matchVar :: [String] -> [([Pattern],LExpr)] -> Match -> State Int Match
76matchVar (v:vs) cs def = match vs (map subVar cs) def
78 subVar (p:ps, (L s e)) = (ps, L s $ subOnePattern p e)
80 subOnePattern pattern e =
82 PVar x -> subst x (Var v) e
85 foldr (\x -> subst x (Access (L s (Var v)) x)) e fs
87matchCon :: [String] -> [([Pattern],LExpr)] -> Match -> State Int Match
88matchCon (v:vs) cs def = (flip (Match v) def) <$> mapM toClause css
90 css = groupBy eq (sortBy cmp cs)
92 cmp (p1:_,_) (p2:_,_) =
94 (PData n1 _, PData n2 _) -> compare n1 n2
97 eq (p1:_,_) (p2:_,_) =
99 (PData n1 _, PData n2 _) -> n1 == n2
104 (PData name _ : _, _) -> matchClause (Left name) (v:vs) cs Break
105 (PLiteral lit : _, _) -> matchClause (Right lit) (v:vs) cs Break
107matchClause :: Either String Literal
109 -> [([Pattern],LExpr)]
112matchClause c (_:vs) cs def =
114 Clause c vs' <$> match (vs' ++ vs) (map flatten cs) def
119 PData _ ps' -> (ps' ++ ps, e)
120 PLiteral _ -> (ps, e)
124 (PData _ ps : _, _) -> forM ps (const newVar)
125 (PLiteral _ : _, _) -> return []
127matchMix :: [String] -> [([Pattern],LExpr)] -> Match -> State Int Match
128matchMix vs cs def = foldM (flip $ match vs) def (reverse css)
129 where css = groupBy (\p1 p2 -> isCon p1 == isCon p2) cs
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