@@ -3,27 +3,28 @@ module Generate.Cases (toMatch, Match (..), Clause (..), matchSubst, newVar) whe
33
44import Control.Applicative ((<$>))
55import Control.Arrow (first)
6−import Control.Monad.State
6+import qualified Control.Monad.State as State
77import Data.List (groupBy,sortBy)
88import Data.Maybe (fromMaybe)
99
10−import AST.Annotation
11−import AST.Expression.General
10+import qualified AST.Annotation as A
11+import qualified AST.Expression.General as Expr
1212import AST.Expression.Canonical as Canonical
13−import AST.Literal
13+import qualified AST.Literal as Literal
1414import qualified AST.Pattern as P
1515import qualified AST.Variable as Var
16−import Transform.Substitute
16+import Transform.Substitute (subst)
1717
18−toMatch :: [(P.CanonicalPattern, Canonical.Expr)] -> State Int (String, Match)
18+toMatch :: [(P.CanonicalPattern, Canonical.Expr)] -> State.State Int (String, Match)
1919toMatch patterns = do
2020 v <- newVar
2121 (,) v <$> match [v] (map (first (:[])) patterns) Fail
2222
23−newVar :: State Int String
24−newVar = do n <- get
25− modify (+1)
26− return $ "_v" ++ show n
23+newVar :: State.State Int String
24+newVar =
25+ do n <- State.get
26+ State.modify (+1)
27+ return $ "_v" ++ show n
2728
2829data Match
2930 = Match String [Clause] Match
@@ -34,7 +35,7 @@ data Match
3435 deriving Show
3536
3637data Clause =
37− Clause (Either Var.Canonical Literal) [String] Match
38+ Clause (Either Var.Canonical Literal.Literal) [String] Match
3839 deriving Show
3940
4041matchSubst :: [(String,String)] -> Match -> Match
@@ -43,8 +44,8 @@ matchSubst pairs match =
4344 Break -> Break
4445 Fail -> Fail
4546 Seq ms -> Seq (map (matchSubst pairs) ms)
46− Other (A a e) ->
47− Other . A a $ foldr ($) e $ map (\(x,y) -> subst x (localVar y)) pairs
47+ Other (A.A a e) ->
48+ Other . A.A a $ foldr ($) e $ map (\(x,y) -> subst x (Expr.localVar y)) pairs
4849 Match n cs m ->
4950 Match (varSubst n) (map clauseSubst cs) (matchSubst pairs m)
5051 where
@@ -63,7 +64,7 @@ isCon (p:_, _) =
6364isVar :: ([P.Pattern var], expr) -> Bool
6465isVar p = not (isCon p)
6566
66−match :: [String] -> [([P.CanonicalPattern], Canonical.Expr)] -> Match -> State Int Match
67+match :: [String] -> [([P.CanonicalPattern], Canonical.Expr)] -> Match -> State.State Int Match
6768match [] [] def = return def
6869match [] [([],e)] Fail = return $ Other e
6970match [] [([],e)] Break = return $ Other e
@@ -77,27 +78,27 @@ match vs@(v:_) cs def
7778
7879dealias :: String -> ([P.CanonicalPattern], Canonical.Expr) -> ([P.CanonicalPattern], Canonical.Expr)
7980dealias _ ([], _) = noMatch "dealias"
80−dealias v c@(p:ps, A a e) =
81+dealias v c@(p:ps, A.A a e) =
8182 case p of
82− P.Alias x pattern -> (pattern:ps, A a $ subst x (localVar v) e)
83+ P.Alias x pattern -> (pattern:ps, A.A a $ subst x (Expr.localVar v) e)
8384 _ -> c
8485
85−matchVar :: [String] -> [([P.CanonicalPattern], Canonical.Expr)] -> Match -> State Int Match
86+matchVar :: [String] -> [([P.CanonicalPattern], Canonical.Expr)] -> Match -> State.State Int Match
8687matchVar [] _ _ = noMatch "matchVar"
8788matchVar (v:vs) cs def = match vs (map subVar cs) def
8889 where
8990 subVar ([], _) = noMatch "matchVar.subVar"
90− subVar (p:ps, (A a e)) = (ps, A a $ subOnePattern p e)
91+ subVar (p:ps, (A.A a e)) = (ps, A.A a $ subOnePattern p e)
9192 where
9293 subOnePattern pattern e =
9394 case pattern of
94− P.Var x -> subst x (localVar v) e
95+ P.Var x -> subst x (Expr.localVar v) e
9596 P.Anything -> e
9697 P.Record fs ->
97− foldr (\x -> subst x (Access (A a (localVar v)) x)) e fs
98+ foldr (\x -> subst x (Expr.Access (A.A a (Expr.localVar v)) x)) e fs
9899 _ -> noMatch "matchVar.subVar"
99100
100−matchCon :: [String] -> [([P.CanonicalPattern], Canonical.Expr)] -> Match -> State Int Match
101+matchCon :: [String] -> [([P.CanonicalPattern], Canonical.Expr)] -> Match -> State.State Int Match
101102matchCon [] _ _ = noMatch "matchCon"
102103matchCon (v:vs) cs def = (flip (Match v) def) <$> mapM toClause css
103104 where
@@ -121,11 +122,11 @@ matchCon (v:vs) cs def = (flip (Match v) def) <$> mapM toClause css
121122 (P.Literal lit : _, _) -> matchClause (Right lit) (v:vs) cs Break
122123 _ -> noMatch "matchCon"
123124
124−matchClause :: Either Var.Canonical Literal
125+matchClause :: Either Var.Canonical Literal.Literal
125126 -> [String]
126127 -> [([P.CanonicalPattern], Canonical.Expr)]
127128 -> Match
128− -> State Int Clause
129+ -> State.State Int Clause
129130matchClause _ [] _ _ = noMatch "matchClause"
130131matchClause c (_:vs) cs def =
131132 do vs' <- getVars
@@ -140,13 +141,15 @@ matchClause c (_:vs) cs def =
140141
141142 getVars =
142143 case head cs of
143− (P.Data _ ps : _, _) -> forM ps (const newVar)
144+ (P.Data _ ps : _, _) -> State.forM ps (const newVar)
144145 (P.Literal _ : _, _) -> return []
145146 _ -> noMatch "matchClause.getVars"
146147
147−matchMix :: [String] -> [([P.CanonicalPattern], Canonical.Expr)] -> Match -> State Int Match
148−matchMix vs cs def = foldM (flip $ match vs) def (reverse css)
149− where css = groupBy (\p1 p2 -> isCon p1 == isCon p2) cs
148+matchMix :: [String] -> [([P.CanonicalPattern], Canonical.Expr)] -> Match -> State.State Int Match
149+matchMix vs cs def =
150+ State.foldM (flip $ match vs) def (reverse css)
151+ where
152+ css = groupBy (\p1 p2 -> isCon p1 == isCon p2) cs
150153
151154noMatch :: String -> a
152155noMatch name =