git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #960 from andyarvanitis/preserve_names
10a5416Phil Freeman
src/Language/PureScript/Sugar/CaseDeclarations.hs+24−3
@@ -119,11 +119,32 @@ toTuple _ = error "Not a value declaration"
119
119
120makeCaseDeclaration :: (Functor m, Applicative m, MonadSupply m, MonadError MultipleErrors m) => Ident -> [([Binder], Either [(Guard, Expr)] Expr)] -> m Declaration
120makeCaseDeclaration :: (Functor m, Applicative m, MonadSupply m, MonadError MultipleErrors m) => Ident -> [([Binder], Either [(Guard, Expr)] Expr)] -> m Declaration
121makeCaseDeclaration ident alternatives = do
121makeCaseDeclaration ident alternatives = do
122− let argPattern = length . fst . head $ alternatives
122+ let namedArgs = map findName . fst <$> alternatives
123− args <- map Ident <$> replicateM argPattern freshName
123+ args <- mapM argName $ foldl1 resolveNames namedArgs
124 let
124 let
125 vars = map (Var . Qualified Nothing) args
125 vars = map (Var . Qualified Nothing) args
126 binders = [ CaseAlternative bs result | (bs, result) <- alternatives ]
126 binders = [ CaseAlternative bs result | (bs, result) <- alternatives ]
127 value = foldr (Abs . Left) (Case vars binders) args
127 value = foldr (Abs . Left) (Case vars binders) args
128 return $ ValueDeclaration ident Value [] (Right value)
128 return $ ValueDeclaration ident Value [] (Right value)
129−
129+ where
130+ findName :: Binder -> Maybe Ident
131+ findName (VarBinder name) = Just name
132+ findName (PositionedBinder _ _ binder) = findName binder
133+ findName _ = Nothing
134+
135+ argName :: (MonadSupply m) => Maybe Ident -> m Ident
136+ argName (Just name) = return name
137+ argName Nothing = do
138+ name <- freshName
139+ return (Ident name)
140+
141+ resolveNames :: [Maybe Ident] -> [Maybe Ident] -> [Maybe Ident]
142+ resolveNames = zipWith resolveName
143+
144+ resolveName :: Maybe Ident -> Maybe Ident -> Maybe Ident
145+ resolveName (Just a) (Just b)
146+ | a == b = Just a
147+ | otherwise = Nothing
148+ resolveName Nothing Nothing = Nothing
149+ resolveName (Just a) Nothing = Just a
150+ resolveName Nothing (Just b) = Just b