git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Add rename pass to desugar
commit6f1d52ffc18f11914a6eaa634656438fe4ccc535 authorGary Burgess <gary.burgess@gmail.com> dateWed 26 Feb 2014, 20:35 (13 years ago) parent695ab61
4 files changed+103−18
prelude/prelude.purs+19−17 purescript.cabal+1 src/Language/PureScript/Sugar.hs+3−1 src/Language/PureScript/Sugar/Names.hs+80
prelude/prelude.purs+19−17
@@ -42,7 +42,7 @@ module Prelude where
4242 \ return n.toString();\
4343 \}" :: Number -> String
4444
45− instance Prelude.Show Number where
45+ instance Show Number where
4646 show = showNumber
4747
4848 class Read a where
@@ -439,22 +439,22 @@ module Data.Maybe where
439439 maybe _ f (Just a) = f a
440440
441441 fromMaybe :: forall a. a -> Maybe a -> a
442− fromMaybe a = maybe a (Prelude.id :: forall a. a -> a)
442+ fromMaybe a = maybe a (id :: forall a. a -> a)
443443
444− instance Prelude.Monad Maybe where
444+ instance Monad Maybe where
445445 return = Just
446446 (>>=) m f = maybe Nothing f m
447447
448− instance Prelude.Applicative Maybe where
448+ instance Applicative Maybe where
449449 pure = Just
450450 (<*>) (Just fn) x = fn <$> x
451451 (<*>) Nothing _ = Nothing
452452
453− instance Prelude.Functor Maybe where
453+ instance Functor Maybe where
454454 (<$>) fn (Just x) = Just (fn x)
455455 (<$>) _ _ = Nothing
456456
457− instance (Show a) => Prelude.Show (Maybe a) where
457+ instance (Show a) => Show (Maybe a) where
458458 show (Just x) = "Just " ++ (show x)
459459 show Nothing = "Nothing"
460460
@@ -468,20 +468,20 @@ module Data.Either where
468468 either f _ (Left a) = f a
469469 either _ g (Right b) = g b
470470
471− instance Prelude.Monad (Either e) where
471+ instance Monad (Either e) where
472472 return = Right
473473 (>>=) = either (\e _ -> Left e) (\a f -> f a)
474474
475− instance Prelude.Applicative (Either e) where
475+ instance Applicative (Either e) where
476476 pure = Right
477477 (<*>) (Left e) _ = Left e
478478 (<*>) (Right f) r = f <$> r
479479
480− instance Prelude.Functor (Either a) where
480+ instance Functor (Either a) where
481481 (<$>) _ (Left x) = Left x
482482 (<$>) f (Right y) = Right (f y)
483483
484− instance (Show a, Show b) => Prelude.Show (Either a b) where
484+ instance (Show a, Show b) => Show (Either a b) where
485485 show (Left x) = "Left " ++ (show x)
486486 show (Right y) = "Right " ++ (show y)
487487
@@ -651,17 +651,17 @@ module Data.Array where
651651 all _ [] = true
652652 all p (a:as) = p a && all p as
653653
654− instance (Prelude.Show a) => Prelude.Show [a] where
654+ instance (Show a) => Show [a] where
655655 show xs = "[" ++ joinWith (map show xs) "," ++ "]"
656656
657− instance Prelude.Functor [] where
657+ instance Functor [] where
658658 (<$>) = map
659659
660− instance Prelude.Monad [] where
660+ instance Monad [] where
661661 return = singleton
662662 (>>=) = concatMap
663663
664− instance Prelude.Alternative [] where
664+ instance Alternative [] where
665665 empty = []
666666 (<|>) = concat
667667
@@ -680,7 +680,7 @@ module Data.Tuple where
680680
681681 data Tuple a b = Tuple a b
682682
683− instance (Prelude.Show a, Prelude.Show b) => Prelude.Show (Tuple a b) where
683+ instance (Show a, Show b) => Show (Tuple a b) where
684684 show (Tuple a b) = "Tuple(" ++ show a ++ ", " ++ show b ++ ")"
685685
686686 curry :: forall a b c. (Tuple a b -> c) -> a -> b -> c
@@ -934,6 +934,8 @@ module Math where
934934
935935module Control.Monad.Eff where
936936
937+ import Prelude
938+
937939 foreign import data Eff :: # ! -> * -> *
938940
939941 foreign import retEff "function retEff(a) {\
@@ -956,7 +958,7 @@ module Control.Monad.Eff where
956958 \ return f();\
957959 \}" :: forall a. Pure a -> a
958960
959− instance Prelude.Monad (Eff e) where
961+ instance Monad (Eff e) where
960962 return = retEff
961963 (>>=) = bindEff
962964
@@ -1091,7 +1093,7 @@ module Debug.Trace where
10911093 \ };\
10921094 \}" :: forall r. String -> Eff (trace :: Trace | r) {}
10931095
1094− print :: forall a r. (Prelude.Show a) => a -> Eff (trace :: Trace | r) {}
1096+ print :: forall a r. (Show a) => a -> Eff (trace :: Trace | r) {}
10951097 print o = trace (show o)
10961098
10971099module Control.Monad.ST where
purescript.cabal+1
@@ -43,6 +43,7 @@ library
4343 Language.PureScript.Sugar.Operators
4444 Language.PureScript.Sugar.TypeClasses
4545 Language.PureScript.Sugar.Let
46+ Language.PureScript.Sugar.Names
4647 Language.PureScript.CodeGen
4748 Language.PureScript.CodeGen.Common
4849 Language.PureScript.CodeGen.Externs
src/Language/PureScript/Sugar.hs+3−1
@@ -27,6 +27,7 @@ import Language.PureScript.Sugar.TypeDeclarations as S
2727import Language.PureScript.Sugar.BindingGroups as S
2828import Language.PureScript.Sugar.TypeClasses as S
2929import Language.PureScript.Sugar.Let as S
30+import Language.PureScript.Sugar.Names as S
3031
3132-- |
3233-- The desugaring pipeline proceeds as follows:
@@ -46,7 +47,8 @@ import Language.PureScript.Sugar.Let as S
4647-- * Group mutually recursive value and data declarations into binding groups.
4748--
4849desugar :: [Module] -> Either String [Module]
49−desugar = desugarTypeClasses
50+desugar = rename
51+ >=> desugarTypeClasses
5052 >=> rebracket
5153 >=> desugarDo
5254 >=> desugarLetBindings
src/Language/PureScript/Sugar/Names.hsnew file+80
@@ -0,0 +1,80 @@
1+-----------------------------------------------------------------------------
2+--
3+-- Module : Language.PureScript.Sugar.Names
4+-- Copyright : (c) 2013-14 Phil Freeman, (c) 2014 Gary Burgess, and other contributors
5+-- License : MIT
6+--
7+-- Maintainer : Phil Freeman <paf31@cantab.net>
8+-- Stability : experimental
9+-- Portability :
10+--
11+-- |
12+--
13+-----------------------------------------------------------------------------
14+
15+module Language.PureScript.Sugar.Names (
16+ rename
17+) where
18+
19+import Control.Applicative ((<$>))
20+import Control.Arrow (first)
21+import Data.Maybe (fromJust)
22+import Data.Generics (mkM, everywhereM)
23+import qualified Data.Map as M
24+import qualified Data.Set as S
25+import Data.Generics.Extras
26+import Language.PureScript.Declarations
27+import Language.PureScript.Names
28+import Language.PureScript.Types
29+import Language.PureScript.Pretty.Types
30+import Debug.Trace
31+
32+data ExportEnvironment = ExportEnvironment { exportedTypeClasses :: M.Map ModuleName (S.Set ProperName) } deriving (Show)
33+data ImportEnvironment = ImportEnvironment { importedTypeClasses :: M.Map ProperName (Qualified ProperName) } deriving (Show)
34+
35+nullEnv = ExportEnvironment M.empty
36+
37+addTypeclass :: ExportEnvironment -> ModuleName -> ProperName -> ExportEnvironment
38+addTypeclass env mn id = env { exportedTypeClasses = M.insertWith S.union mn (S.singleton id) (exportedTypeClasses env) }
39+
40+rename :: [Module] -> Either String [Module]
41+rename modules = mapM (renameInModule $ findExports modules) modules
42+
43+renameInModule :: ExportEnvironment -> Module -> Either String Module
44+renameInModule exports m@(Module mn decls) = Module mn <$> everywhereM (mkM updateDecl) decls >>= everywhereM (mkM updateType)
45+ where
46+ updateDecl (TypeInstanceDeclaration cs cn ts ds) = do
47+ cn' <- updateClassName cn
48+ cs' <- updateConstraints cs
49+ return $ TypeInstanceDeclaration cs' cn' ts ds
50+ updateDecl d = return d
51+ updateType (ConstrainedType cs t) = do
52+ cs' <- updateConstraints cs
53+ return $ ConstrainedType cs' t
54+ updateType t = return t
55+ updateConstraints = mapM updateConstraint
56+ updateConstraint (nm, ts) = do
57+ nm' <- updateClassName nm
58+ return (nm', ts)
59+ updateClassName (Qualified Nothing nm) =
60+ maybe (Left $ "Unknown typeclass '" ++ show nm ++ "'") Right $ M.lookup nm (importedTypeClasses imports)
61+ updateClassName q = return q
62+ imports = resolveImports exports m
63+
64+findExports :: [Module] -> ExportEnvironment
65+findExports = foldl addModule nullEnv
66+ where
67+ addModule env (Module mn ds) = foldl (addDecl mn) env ds
68+ addDecl mn env (TypeClassDeclaration tcn _ _) = addTypeclass env mn tcn
69+ addDecl _ env _ = env
70+
71+findImports :: [Declaration] -> [ModuleName]
72+findImports decls = [ mn | (ImportDeclaration mn Nothing) <- decls ]
73+
74+resolveImports :: ExportEnvironment -> Module -> ImportEnvironment
75+resolveImports env (Module mn decls) = ImportEnvironment $ resolve (mn : findImports decls) (exportedTypeClasses env)
76+ where
77+ resolve imns = M.foldlWithKey (resolveDefs imns) M.empty
78+ resolveDefs imns result mn ids | mn `elem` imns = S.foldl (resolveDef mn) result ids
79+ resolveDefs imns result mn ids = result
80+ resolveDef mn result id = M.insert id (Qualified (Just mn) id) result