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.Annotation
10import SourceSyntax.Expression
11import SourceSyntax.Literal
12import qualified SourceSyntax.Pattern as P
13import qualified SourceSyntax.Variable as V
14import Transform.Substitute
16toMatch :: [(P.Pattern, Expr)] -> 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 pairs match =
43 Seq ms -> Seq (map (matchSubst pairs) ms)
45 Other . A a $ foldr ($) e $ map (\(x,y) -> subst x (rawVar y)) pairs
47 Match (varSubst n) (map clauseSubst cs) (matchSubst pairs m)
49 varSubst v = fromMaybe v (lookup v pairs)
50 clauseSubst (Clause c vs m) =
51 Clause c (map varSubst vs) (matchSubst pairs m)
59isVar p = not (isCon p)
61match :: [String] -> [([P.Pattern],Expr)] -> Match -> State Int Match
62match [] [] def = return def
63match [] [([],e)] Fail = return $ Other e
64match [] [([],e)] Break = return $ Other e
65match [] cs def = return $ Seq (map (Other . snd) cs ++ [def])
67 | all isVar cs' = matchVar vs cs' def
68 | all isCon cs' = matchCon vs cs' def
69 | otherwise = matchMix vs cs' def
71 cs' = map (dealias v) cs
73dealias :: String -> ([P.Pattern],Expr) -> ([P.Pattern],Expr)
74dealias v c@(p:ps, A a e) =
76 P.Alias x pattern -> (pattern:ps, A a $ subst x (rawVar v) e)
79matchVar :: [String] -> [([P.Pattern],Expr)] -> Match -> State Int Match
80matchVar (v:vs) cs def = match vs (map subVar cs) def
82 subVar (p:ps, (A a e)) = (ps, A a $ subOnePattern p e)
84 subOnePattern pattern e =
86 P.Var x -> subst x (rawVar v) e
89 foldr (\x -> subst x (Access (A a (rawVar v)) x)) e fs
91matchCon :: [String] -> [([P.Pattern],Expr)] -> Match -> State Int Match
92matchCon (v:vs) cs def = (flip (Match v) def) <$> mapM toClause css
94 css = groupBy eq (sortBy cmp cs)
96 cmp (p1:_,_) (p2:_,_) =
98 (P.Data n1 _, P.Data n2 _) -> compare n1 n2
101 eq (p1:_,_) (p2:_,_) =
103 (P.Data n1 _, P.Data n2 _) -> n1 == n2
108 (P.Data name _ : _, _) -> matchClause (Left name) (v:vs) cs Break
109 (P.Literal lit : _, _) -> matchClause (Right lit) (v:vs) cs Break
111matchClause :: Either String Literal
113 -> [([P.Pattern],Expr)]
116matchClause c (_:vs) cs def =
118 Clause c vs' <$> match (vs' ++ vs) (map flatten cs) def
123 P.Data _ ps' -> (ps' ++ ps, e)
124 P.Literal _ -> (ps, e)
128 (P.Data _ ps : _, _) -> forM ps (const newVar)
129 (P.Literal _ : _, _) -> return []
131matchMix :: [String] -> [([P.Pattern],Expr)] -> Match -> State Int Match
132matchMix vs cs def = foldM (flip $ match vs) def (reverse css)
133 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