git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Named type class instances
commit8b6c26ec6450f6fbedc6008a83ab0ef60bab08bb authorPhil Freeman <paf31@cantab.net> dateTue 4 Mar 2014, 16:26 (13 years ago) parent17ccc92
21 files changed+161−180
RELEASE-0.5.md+5 docgen/Main.hs+3−3 examples/passing/Applicative.purs+1−1 examples/passing/ArrayType.purs+1−1 examples/passing/Do.purs+1−1 examples/passing/DottedModules.purs+1−1 examples/passing/EmptyTypeClass.purs+1−1 examples/passing/MPTCs.purs+4−4 examples/passing/MonadState.purs+2−2 examples/passing/RuntimeScopeIssue.purs+2−2 examples/passing/TypeClasses.purs+5−5 examples/passing/TypeClassesInOrder.purs+1−1 examples/passing/TypeClassesWithOverlappingTypeVariables.purs+1−1 prelude/prelude.purs+68−64 src/Language/PureScript/CodeGen/Optimize.hs+38−48 src/Language/PureScript/Declarations.hs+1−1 src/Language/PureScript/Parser/Declarations.hs+2−1 src/Language/PureScript/Sugar/CaseDeclarations.hs+2−2 src/Language/PureScript/Sugar/Names.hs+2−2 src/Language/PureScript/Sugar/TypeClasses.hs+19−37 src/Language/PureScript/TypeChecker.hs+1−2
RELEASE-0.5.md+5
@@ -3,6 +3,10 @@
33## Breaking Changes
44
55- Support for blocks has been removed. (paf31)
6+- Type class instances must now be named (paf31)
7+
8+ instance showNumber :: Show Number where
9+ ...
610
711### New Features
812
@@ -24,6 +28,7 @@
2428- PSCI history is in XDG config (joneshf)
2529- PSCI allows loading of modules from ~ paths (joneshf)
2630- PSCI can accept a list of modules to load on start from the command line (paf31)
31+- Type class instances are now named, to enable easier interop with Javascript (paf31)
2732
2833### Bug Fixes
2934
docgen/Main.hs+3−3
@@ -102,11 +102,11 @@ renderDeclaration n (P.TypeSynonymDeclaration name args ty) = do
102102renderDeclaration n (P.TypeClassDeclaration name args ds) = do
103103 atIndent n $ "class " ++ P.runProperName name ++ " " ++ unwords args ++ " where"
104104 mapM_ (renderDeclaration (n + 2)) ds
105−renderDeclaration n (P.TypeInstanceDeclaration constraints name tys _) = do
105+renderDeclaration n (P.TypeInstanceDeclaration name constraints className tys _) = do
106106 let constraintsText = case constraints of
107107 [] -> ""
108108 cs -> "(" ++ intercalate "," (map (\(pn, tys') -> show pn ++ " (" ++ unwords (map (("(" ++) . (++ ")") . P.prettyPrintType) tys') ++ ")") cs) ++ ") => "
109− atIndent n $ constraintsText ++ "instance " ++ show name ++ " " ++ unwords (map (("(" ++) . (++ ")") . P.prettyPrintType) tys)
109+ atIndent n $ constraintsText ++ "instance " ++ show name ++ " :: " ++ show className ++ " " ++ unwords (map (("(" ++) . (++ ")") . P.prettyPrintType) tys)
110110renderDeclaration _ _ = return ()
111111
112112getName :: P.Declaration -> String
@@ -116,7 +116,7 @@ getName (P.DataDeclaration name _ _) = P.runProperName name
116116getName (P.ExternDataDeclaration name _) = P.runProperName name
117117getName (P.TypeSynonymDeclaration name _ _) = P.runProperName name
118118getName (P.TypeClassDeclaration name _ _) = P.runProperName name
119−getName (P.TypeInstanceDeclaration _ name _ _) = show name
119+getName (P.TypeInstanceDeclaration name _ _ _ _) = show name
120120getName _ = error "Invalid argument to getName"
121121
122122isValueDeclaration :: P.Declaration -> Bool
examples/passing/Applicative.purs+1−1
@@ -6,7 +6,7 @@ class Applicative f where
66
77data Maybe a = Nothing | Just a
88
9−instance Applicative Maybe where
9+instance applicativeMaybe :: Applicative Maybe where
1010 pure = Just
1111 (<*>) (Just f) (Just a) = Just (f a)
1212 (<*>) _ _ = Nothing
examples/passing/ArrayType.purs+1−1
@@ -8,7 +8,7 @@ class Functor f where
88
99foreign import (:) :: forall a. a -> [a] -> [a]
1010
11−instance Functor [] where
11+instance functorArray :: Functor [] where
1212 fmap _ [] = []
1313 fmap f (x:xs) = f x : fmap f xs
1414
examples/passing/Do.purs+1−1
@@ -4,7 +4,7 @@ import Prelude
44
55data Maybe a = Nothing | Just a
66
7−instance Prelude.Monad Maybe where
7+instance monadMaybe :: Prelude.Monad Maybe where
88 return = Just
99 (>>=) Nothing _ = Nothing
1010 (>>=) (Just a) f = f a
examples/passing/DottedModules.purs+1−1
@@ -12,7 +12,7 @@ module Main where
1212 import Prelude
1313 import Some.Module.Name
1414
15− instance Some.Module.Name.Foo String where
15+ instance fooString :: Some.Module.Name.Foo String where
1616 foo s = s
1717
1818 unwrap (Text x) = x
examples/passing/EmptyTypeClass.purs+1−1
@@ -7,7 +7,7 @@ head (x:xs) = x
77
88module AllowPartialFns where
99
10−instance EmptyTypeClass.Partial
10+instance allowPartials :: EmptyTypeClass.Partial
1111
1212module Main where
1313
examples/passing/MPTCs.purs+4−4
@@ -6,19 +6,19 @@ import Data.Array
66class NullaryTypeClass where
77 greeting :: String
88
9−instance NullaryTypeClass where
9+instance nullaryTypeClass :: NullaryTypeClass where
1010 greeting = "Hello, World!"
1111
1212class Coerce a b where
1313 coerce :: a -> b
1414
15−instance Coerce a a where
15+instance coerceRefl :: Coerce a a where
1616 coerce a = a
1717
18−instance (Prelude.Show a) => Coerce a String where
18+instance coerceShow :: (Prelude.Show a) => Coerce a String where
1919 coerce = show
2020
21−instance (Coerce a b) => Coerce [a] [b] where
21+instance coerceArray :: (Coerce a b) => Coerce [a] [b] where
2222 coerce = map coerce
2323
2424main = Debug.Trace.print $ coerce [greeting] :: [String]
examples/passing/MonadState.purs+2−2
@@ -11,12 +11,12 @@ data State s a = State (s -> Tuple s a)
1111
1212runState s (State f) = f s
1313
14−instance Prelude.Monad (State s) where
14+instance monadState :: Prelude.Monad (State s) where
1515 return a = State $ \s -> Tuple s a
1616 (>>=) f g = State $ \s -> let (Tuple s1 a) = runState s f in
1717 runState s1 (g a)
1818
19−instance MonadState s (State s) where
19+instance monadStateState :: MonadState s (State s) where
2020 get = State (\s -> Tuple s s)
2121 put s = State (\_ -> Tuple s {})
2222
examples/passing/RuntimeScopeIssue.purs+2−2
@@ -8,11 +8,11 @@ class A a where
88class B a where
99 b :: a -> Boolean
1010
11−instance A Number where
11+instance aNumber :: A Number where
1212 a 0 = true
1313 a n = b (n - 1)
1414
15−instance B Number where
15+instance bNumber :: B Number where
1616 b 0 = false
1717 b n = a (n - 1)
1818
examples/passing/TypeClasses.purs+5−5
@@ -16,18 +16,18 @@ test8 = \_ -> show $ "testing"
1616
1717data Data a = Data a
1818
19−instance (Prelude.Show a) => Prelude.Show (Data a) where
19+instance showData :: (Prelude.Show a) => Prelude.Show (Data a) where
2020 show (Data a) = "Data (" ++ show a ++ ")"
2121
2222test3 = \_ -> show (Data "testing")
2323
24−instance Prelude.Monad Data where
24+instance monadData :: Prelude.Monad Data where
2525 return = Data
2626 (>>=) (Data a) f = f a
2727
2828data Maybe a = Nothing | Just a
2929
30−instance Prelude.Monad Maybe where
30+instance monadMaybe :: Prelude.Monad Maybe where
3131 return = Just
3232 (>>=) Nothing _ = Nothing
3333 (>>=) (Just a) f = f a
@@ -42,13 +42,13 @@ module TypeClasses2 where
4242import Prelude
4343import TypeClasses
4444
45−instance (Prelude.Show a) => Prelude.Show [a] where
45+instance showArray :: (Prelude.Show a) => Prelude.Show [a] where
4646 show [] = "[]"
4747 show (x:xs) = show x ++ ", " ++ show xs
4848
4949test6 = \_ -> show ["testing"]
5050
51−instance Prelude.Monad ((->) r) where
51+instance monadFunction :: Prelude.Monad ((->) r) where
5252 return a r = a
5353 (>>=) f g r = g (f r) r
5454
examples/passing/TypeClassesInOrder.purs+1−1
@@ -5,7 +5,7 @@ import Prelude
55class Foo a where
66 foo :: a -> String
77
8−instance Foo String where
8+instance fooString :: Foo String where
99 foo s = s
1010
1111main = Debug.Trace.trace $ foo "Done"
examples/passing/TypeClassesWithOverlappingTypeVariables.purs+1−1
@@ -3,7 +3,7 @@ module Main where
33 import Prelude
44 import Data.Either
55
6− instance Prelude.Functor (Either a) where
6+ instance functorEither :: Prelude.Functor (Either a) where
77 (<$>) _ (Left x) = Left x
88 (<$>) f (Right y) = Right (f y)
99
prelude/prelude.purs+68−64
@@ -14,7 +14,7 @@ module Prelude where
1414 (<<<) :: forall b c d. a c d -> a b c -> a b d
1515 (>>>) :: forall b c d. a b c -> a c d -> a b d
1616
17− instance Category (->) where
17+ instance categoryArr :: Category (->) where
1818 id x = x
1919 (<<<) f g x = f (g x)
2020 (>>>) f g x = g (f x)
@@ -31,36 +31,36 @@ module Prelude where
3131 class Show a where
3232 show :: a -> String
3333
34− instance Show String where
34+ instance showString :: Show String where
3535 show s = s
3636
37− instance Show Boolean where
37+ instance showBoolean :: Show Boolean where
3838 show true = "true"
3939 show false = "false"
4040
41− foreign import showNumber "function showNumber(n) {\
42− \ return n.toString();\
43− \}" :: Number -> String
41+ foreign import showNumberImpl "function showNumberImpl(n) {\
42+ \ return n.toString();\
43+ \}" :: Number -> String
4444
45− instance Show Number where
46− show = showNumber
45+ instance showNumber :: Show Number where
46+ show = showNumberImpl
4747
4848 class Read a where
4949 read :: String -> a
5050
51− instance Read String where
51+ instance readString :: Read String where
5252 read s = s
5353
54− instance Read Boolean where
54+ instance readBoolean :: Read Boolean where
5555 read "true" = true
5656 read _ = false
5757
58− foreign import readNumber "function readNumber(n) {\
59− \ return parseFloat(n);\
60− \}" :: String -> Number
58+ foreign import readNumberImpl "function readNumber(n) {\
59+ \ return parseFloat(n);\
60+ \}" :: String -> Number
6161
62− instance Read Number where
63− read = readNumber
62+ instance readNumber :: Read Number where
63+ read = readNumberImpl
6464
6565 infixl 4 <$>
6666
@@ -73,7 +73,7 @@ module Prelude where
7373 pure :: forall a. a -> f a
7474 (<*>) :: forall a b. f (a -> b) -> f a -> f b
7575
76− instance (Applicative f) => Functor f where
76+ instance functorFromApplicative :: (Applicative f) => Functor f where
7777 (<$>) f a = pure f <*> a
7878
7979 infixl 3 <|>
@@ -88,7 +88,7 @@ module Prelude where
8888 return :: forall a. a -> m a
8989 (>>=) :: forall a b. m a -> (a -> m b) -> m b
9090
91− instance (Monad m) => Applicative m where
91+ instance applicativeFromMonad :: (Monad m) => Applicative m where
9292 pure = return
9393 (<*>) f a = do
9494 f' <- f
@@ -144,7 +144,7 @@ module Prelude where
144144 \ return -n;\
145145 \}" :: Number -> Number
146146
147− instance Num Number where
147+ instance numNumber :: Num Number where
148148 (+) = numAdd
149149 (-) = numSub
150150 (*) = numMul
@@ -159,18 +159,6 @@ module Prelude where
159159 (==) :: a -> a -> Boolean
160160 (/=) :: a -> a -> Boolean
161161
162− -- Referential equality
163− data Ref a = Ref a
164−
165− liftRef :: forall a b. (a -> a -> b) -> Ref a -> Ref a -> b
166− liftRef f (Ref x) (Ref y) = f x y
167−
168− refEq :: forall a. Ref a -> Ref a -> Boolean
169− refEq = liftRef unsafeRefEq
170−
171− refIneq :: forall a. Ref a -> Ref a -> Boolean
172− refIneq = liftRef unsafeRefIneq
173−
174162 foreign import unsafeRefEq "function unsafeRefEq(r1) {\
175163 \ return function(r2) {\
176164 \ return r1 === r2;\
@@ -183,23 +171,19 @@ module Prelude where
183171 \ };\
184172 \}" :: forall a. a -> a -> Boolean
185173
186− instance Eq (Ref a) where
187− (==) = refEq
188− (/=) = refIneq
189−
190− instance Eq String where
174+ instance eqString :: Eq String where
191175 (==) = unsafeRefEq
192176 (/=) = unsafeRefIneq
193177
194− instance Eq Number where
178+ instance eqNumber :: Eq Number where
195179 (==) = unsafeRefEq
196180 (/=) = unsafeRefIneq
197181
198− instance Eq Boolean where
182+ instance eqBoolean :: Eq Boolean where
199183 (==) = unsafeRefEq
200184 (/=) = unsafeRefIneq
201185
202− instance (Eq a) => Eq [a] where
186+ instance eqArray :: (Eq a) => Eq [a] where
203187 (==) [] [] = true
204188 (==) (x:xs) (y:ys) = x == y && xs == ys
205189 (==) _ _ = false
@@ -240,7 +224,7 @@ module Prelude where
240224 \ };\
241225 \}" :: Number -> Number -> Boolean
242226
243− instance Ord Number where
227+ instance ordNumber :: Ord Number where
244228 (<) = numLess
245229 (>) = numGreater
246230 (<=) = numLessEq
@@ -299,7 +283,7 @@ module Prelude where
299283 \ return ~n;\
300284 \}" :: Number -> Number
301285
302− instance Bits Number where
286+ instance bitsNumber :: Bits Number where
303287 (&) = numAnd
304288 (|) = numOr
305289 (^) = numXor
@@ -340,7 +324,7 @@ module Prelude where
340324 \ return !b;\
341325 \}" :: Boolean -> Boolean
342326
343− instance BoolLike Boolean where
327+ instance boolLikeBoolean :: BoolLike Boolean where
344328 (&&) = boolAnd
345329 (||) = boolOr
346330 not = boolNot
@@ -364,11 +348,11 @@ module Data.Monoid where
364348 mempty :: m
365349 (<>) :: m -> m -> m
366350
367− instance Monoid String where
351+ instance monoidString :: Monoid String where
368352 mempty = ""
369353 (<>) = (++)
370354
371− instance Monoid [a] where
355+ instance monoidArray :: Monoid [a] where
372356 mempty = []
373357 (<>) = Data.Array.concat
374358
@@ -441,20 +425,20 @@ module Data.Maybe where
441425 fromMaybe :: forall a. a -> Maybe a -> a
442426 fromMaybe a = maybe a (id :: forall a. a -> a)
443427
444− instance Monad Maybe where
428+ instance monadMaybe :: Monad Maybe where
445429 return = Just
446430 (>>=) m f = maybe Nothing f m
447431
448− instance Applicative Maybe where
432+ instance applicativeMaybe :: Applicative Maybe where
449433 pure = Just
450434 (<*>) (Just fn) x = fn <$> x
451435 (<*>) Nothing _ = Nothing
452436
453− instance Functor Maybe where
437+ instance functorMaybe :: Functor Maybe where
454438 (<$>) fn (Just x) = Just (fn x)
455439 (<$>) _ _ = Nothing
456440
457− instance (Show a) => Show (Maybe a) where
441+ instance showMaybe :: (Show a) => Show (Maybe a) where
458442 show (Just x) = "Just " ++ (show x)
459443 show Nothing = "Nothing"
460444
@@ -468,20 +452,20 @@ module Data.Either where
468452 either f _ (Left a) = f a
469453 either _ g (Right b) = g b
470454
471− instance Monad (Either e) where
455+ instance monadEither :: Monad (Either e) where
472456 return = Right
473457 (>>=) = either (\e _ -> Left e) (\a f -> f a)
474458
475− instance Applicative (Either e) where
459+ instance applicativeEither :: Applicative (Either e) where
476460 pure = Right
477461 (<*>) (Left e) _ = Left e
478462 (<*>) (Right f) r = f <$> r
479463
480− instance Functor (Either a) where
464+ instance functorEither :: Functor (Either a) where
481465 (<$>) _ (Left x) = Left x
482466 (<$>) f (Right y) = Right (f y)
483467
484− instance (Show a, Show b) => Show (Either a b) where
468+ instance showEither :: (Show a, Show b) => Show (Either a b) where
485469 show (Left x) = "Left " ++ (show x)
486470 show (Right y) = "Right " ++ (show y)
487471
@@ -576,7 +560,7 @@ module Data.Array where
576560 \ return l1;\
577561 \}" :: forall a. [a] -> [a]
578562
579− foreign import insertAt
563+ foreign import insertAt
580564 "function insertAt(index) {\
581565 \ return function(a) {\
582566 \ return function(l) {\
@@ -587,7 +571,7 @@ module Data.Array where
587571 \ };\
588572 \}":: forall a. Number -> a -> [a] -> [a]
589573
590− foreign import deleteAt
574+ foreign import deleteAt
591575 "function deleteAt(index) {\
592576 \ return function(n) {\
593577 \ return function(l) {\
@@ -598,7 +582,7 @@ module Data.Array where
598582 \ };\
599583 \}":: forall a. Number -> Number -> [a] -> [a]
600584
601− foreign import updateAt
585+ foreign import updateAt
602586 "function updateAt(index) {\
603587 \ return function(a) {\
604588 \ return function(l) {\
@@ -625,7 +609,7 @@ module Data.Array where
625609 filter _ [] = []
626610 filter p (x:xs) | p x = x : filter p xs
627611 filter p (_:xs) = filter p xs
628−
612+
629613 find :: forall a. (a -> Boolean) -> [a] -> Maybe a
630614 find _ [] = Nothing
631615 find p (x:xs) | p x = Just x
@@ -661,20 +645,40 @@ module Data.Array where
661645 take _ [] = []
662646 take n (x:xs) = x : take (n - 1) xs
663647
664− instance (Show a) => Show [a] where
648+ instance showArray :: (Show a) => Show [a] where
665649 show xs = "[" ++ joinWith (map show xs) "," ++ "]"
666650
667− instance Functor [] where
668− (<$>) = map
669−
670− instance Monad [] where
651+ instance monadArray :: Monad [] where
671652 return = singleton
672653 (>>=) = concatMap
673654
674− instance Alternative [] where
655+ instance functorArray :: Functor [] where
656+ (<$>) = map
657+
658+ instance alternativeArray :: Alternative [] where
675659 empty = []
676660 (<|>) = concat
677−
661+
662+module Data.Eq where
663+
664+ import Prelude
665+
666+ -- Referential equality
667+ data Ref a = Ref a
668+
669+ liftRef :: forall a b. (a -> a -> b) -> Ref a -> Ref a -> b
670+ liftRef f (Ref x) (Ref y) = f x y
671+
672+ refEq :: forall a. Ref a -> Ref a -> Boolean
673+ refEq = liftRef unsafeRefEq
674+
675+ refIneq :: forall a. Ref a -> Ref a -> Boolean
676+ refIneq = liftRef unsafeRefIneq
677+
678+ instance eqRef :: Eq (Ref a) where
679+ (==) = refEq
680+ (/=) = refIneq
681+
678682module Data.Array.Unsafe where
679683
680684 head :: forall a. [a] -> a
@@ -690,7 +694,7 @@ module Data.Tuple where
690694
691695 data Tuple a b = Tuple a b
692696
693− instance (Show a, Show b) => Show (Tuple a b) where
697+ instance showTuple :: (Show a, Show b) => Show (Tuple a b) where
694698 show (Tuple a b) = "Tuple(" ++ show a ++ ", " ++ show b ++ ")"
695699
696700 curry :: forall a b c. (Tuple a b -> c) -> a -> b -> c
@@ -968,7 +972,7 @@ module Control.Monad.Eff where
968972 \ return f();\
969973 \}" :: forall a. Pure a -> a
970974
971− instance Monad (Eff e) where
975+ instance monadEff :: Monad (Eff e) where
972976 return = retEff
973977 (>>=) = bindEff
974978
src/Language/PureScript/CodeGen/Optimize.hs+38−48
@@ -46,8 +46,6 @@ import Language.PureScript.Names
4646import Language.PureScript.CodeGen.JS.AST
4747import Language.PureScript.Options
4848import Language.PureScript.CodeGen.Common (identToJs)
49−import Language.PureScript.Sugar.TypeClasses
50− (mkDictionaryValueName)
5149import Language.PureScript.Types
5250
5351-- |
@@ -316,10 +314,7 @@ magicDo' = everywhere (mkT undo) . everywhere' (mkT convert)
316314 prelude = ModuleName [ProperName "Prelude"]
317315 effModule = ModuleName [ProperName "Control", ProperName "Monad", ProperName "Eff"]
318316 -- The name of the type class dictionary for the Monad Eff instance
319− Right (Ident effDictName) = mkDictionaryValueName
320− effModule
321− (Qualified (Just prelude) (ProperName "Monad"))
322− [TypeConstructor (Qualified (Just effModule) (ProperName "Eff"))]
317+ effDictName = "monadEff"
323318 -- Check if an expression represents the Monad Eff dictionary
324319 isEffDict (JSApp (JSVar ident) [JSObjectLiteral []]) | ident == effDictName = True
325320 isEffDict (JSApp (JSAccessor prop (JSAccessor "Control_Monad_Eff" (JSVar "_ps"))) [JSObjectLiteral []]) | prop == effDictName = True
@@ -406,67 +401,62 @@ inlineOperator op f = everywhere (mkT convert)
406401
407402inlineCommonOperators :: JS -> JS
408403inlineCommonOperators = applyAll
409− [ binary "+" "Num" tyNumber Add
410− , binary "-" "Num" tyNumber Subtract
411− , binary "*" "Num" tyNumber Multiply
412− , binary "/" "Num" tyNumber Divide
413− , binary "%" "Num" tyNumber Modulus
414− , unary "negate" "Num" tyNumber Negate
404+ [ binary "numNumber" "+" "Num" tyNumber Add
405+ , binary "numNumber" "-" "Num" tyNumber Subtract
406+ , binary "numNumber" "*" "Num" tyNumber Multiply
407+ , binary "numNumber" "/" "Num" tyNumber Divide
408+ , binary "numNumber" "%" "Num" tyNumber Modulus
409+ , unary "numNumber" "negate" "Num" tyNumber Negate
415410
416− , binary "<" "Ord" tyNumber LessThan
417− , binary ">" "Ord" tyNumber GreaterThan
418− , binary "<=" "Ord" tyNumber LessThanOrEqualTo
419− , binary ">=" "Ord" tyNumber GreaterThanOrEqualTo
411+ , binary "ordNumber" "<" "Ord" tyNumber LessThan
412+ , binary "ordNumber" ">" "Ord" tyNumber GreaterThan
413+ , binary "ordNumber" "<=" "Ord" tyNumber LessThanOrEqualTo
414+ , binary "ordNumber" ">=" "Ord" tyNumber GreaterThanOrEqualTo
420415
421− , binary "==" "Eq" tyNumber EqualTo
422− , binary "/=" "Eq" tyNumber NotEqualTo
423− , binary "==" "Eq" tyString EqualTo
424− , binary "/=" "Eq" tyString NotEqualTo
425− , binary "==" "Eq" tyBoolean EqualTo
426− , binary "/=" "Eq" tyBoolean NotEqualTo
416+ , binary "eqNumber" "==" "Eq" tyNumber EqualTo
417+ , binary "eqNumber" "/=" "Eq" tyNumber NotEqualTo
418+ , binary "eqString" "==" "Eq" tyString EqualTo
419+ , binary "eqString" "/=" "Eq" tyString NotEqualTo
420+ , binary "eqBoolean" "==" "Eq" tyBoolean EqualTo
421+ , binary "eqBoolean" "/=" "Eq" tyBoolean NotEqualTo
427422
428− , binaryFunction "shl" "Bits" tyNumber ShiftLeft
429− , binaryFunction "shr" "Bits" tyNumber ShiftRight
430− , binaryFunction "zshr" "Bits" tyNumber ZeroFillShiftRight
431− , binary "&" "Bits" tyNumber BitwiseAnd
432− , binary "|" "Bits" tyNumber BitwiseOr
433− , binary "^" "Bits" tyNumber BitwiseXor
434− , unary "complement" "Bits" tyNumber BitwiseNot
423+ , binaryFunction "bitsNumber" "shl" "Bits" tyNumber ShiftLeft
424+ , binaryFunction "bitsNumber" "shr" "Bits" tyNumber ShiftRight
425+ , binaryFunction "bitsNumber" "zshr" "Bits" tyNumber ZeroFillShiftRight
426+ , binary "bitsNumber" "&" "Bits" tyNumber BitwiseAnd
427+ , binary "bitsNumber" "|" "Bits" tyNumber BitwiseOr
428+ , binary "bitsNumber" "^" "Bits" tyNumber BitwiseXor
429+ , unary "bitsNumber" "complement" "Bits" tyNumber BitwiseNot
435430
436− , binary "&&" "BoolLike" tyBoolean And
437− , binary "||" "BoolLike" tyBoolean Or
438− , unary "not" "BoolLike" tyBoolean Not
431+ , binary "boolLikeBoolean" "&&" "BoolLike" tyBoolean And
432+ , binary "boolLikeBoolean" "||" "BoolLike" tyBoolean Or
433+ , unary "boolLikeBoolean" "not" "BoolLike" tyBoolean Not
439434 ]
440435 where
441− binary :: String -> String -> Type -> BinaryOperator -> JS -> JS
442− binary opString className classTy op = everywhere (mkT convert)
436+ binary :: String -> String -> String -> Type -> BinaryOperator -> JS -> JS
437+ binary dictName opString className classTy op = everywhere (mkT convert)
443438 where
444439 convert :: JS -> JS
445− convert (JSApp (JSApp (JSApp fn [dict]) [x]) [y]) | isOp fn && isOpDict className classTy dict = JSBinary op x y
440+ convert (JSApp (JSApp (JSApp fn [dict]) [x]) [y]) | isOp fn && isOpDict dictName className classTy dict = JSBinary op x y
446441 convert other = other
447442 isOp (JSAccessor longForm (JSAccessor "Prelude" (JSVar _))) | longForm == identToJs (Op opString) = True
448443 isOp (JSIndexer (JSStringLiteral op') (JSAccessor "Prelude" (JSVar "_ps"))) | opString == op' = True
449444 isOp _ = False
450− binaryFunction :: String -> String -> Type -> BinaryOperator -> JS -> JS
451− binaryFunction fnName className classTy op = everywhere (mkT convert)
445+ binaryFunction :: String -> String -> String -> Type -> BinaryOperator -> JS -> JS
446+ binaryFunction dictName fnName className classTy op = everywhere (mkT convert)
452447 where
453448 convert :: JS -> JS
454− convert (JSApp (JSApp (JSApp fn [dict]) [x]) [y]) | isOp fn && isOpDict className classTy dict = JSBinary op x y
449+ convert (JSApp (JSApp (JSApp fn [dict]) [x]) [y]) | isOp fn && isOpDict dictName className classTy dict = JSBinary op x y
455450 convert other = other
456451 isOp (JSAccessor fnName' (JSAccessor "Prelude" (JSVar "_ps"))) | fnName == fnName' = True
457452 isOp _ = False
458− unary :: String -> String -> Type -> UnaryOperator -> JS -> JS
459− unary fnName className classTy op = everywhere (mkT convert)
453+ unary :: String -> String -> String -> Type -> UnaryOperator -> JS -> JS
454+ unary dictName fnName className classTy op = everywhere (mkT convert)
460455 where
461456 convert :: JS -> JS
462− convert (JSApp (JSApp fn [dict]) [x]) | isOp fn && isOpDict className classTy dict = JSUnary op x
457+ convert (JSApp (JSApp fn [dict]) [x]) | isOp fn && isOpDict dictName className classTy dict = JSUnary op x
463458 convert other = other
464459 isOp (JSAccessor fnName' (JSAccessor "Prelude" (JSVar "_ps"))) | fnName' == fnName = True
465460 isOp _ = False
466− isOpDict className ty (JSApp (JSAccessor prop (JSAccessor "Prelude" (JSVar "_ps"))) [JSObjectLiteral []]) | prop == dictName = True
467− where
468− Right (Ident dictName) = mkDictionaryValueName
469− (ModuleName [ProperName "Prim"])
470− (Qualified (Just (ModuleName [ProperName "Prelude"])) (ProperName className))
471− [ty]
472− isOpDict _ _ _ = False
461+ isOpDict dictName className ty (JSApp (JSAccessor prop (JSAccessor "Prelude" (JSVar "_ps"))) [JSObjectLiteral []]) | prop == dictName = True
462+ isOpDict _ _ _ _ = False
src/Language/PureScript/Declarations.hs+1−1
@@ -134,7 +134,7 @@ data Declaration
134134 -- |
135135 -- A type instance declaration (dependencies, class name, instance type, member declarations)
136136 --
137− | TypeInstanceDeclaration [(Qualified ProperName, [Type])] (Qualified ProperName) [Type] [Declaration]
137+ | TypeInstanceDeclaration Ident [(Qualified ProperName, [Type])] (Qualified ProperName) [Type] [Declaration]
138138 deriving (Show, D.Data, D.Typeable)
139139
140140-- |
src/Language/PureScript/Parser/Declarations.hs+2−1
@@ -110,6 +110,7 @@ parseTypeClassDeclaration = do
110110parseTypeInstanceDeclaration :: P.Parsec String ParseState Declaration
111111parseTypeInstanceDeclaration = do
112112 reserved "instance"
113+ name <- parseIdent <* lexeme (indented *> P.string "::")
113114 deps <- P.optionMaybe $ do
114115 deps <- parens (commaSep1 ((,) <$> parseQualified properName <*> P.many parseTypeAtom))
115116 indented
@@ -120,7 +121,7 @@ parseTypeInstanceDeclaration = do
120121 members <- P.option [] . P.try $ do
121122 indented *> reserved "where"
122123 mark (P.many (same *> parseValueDeclaration))
123− return $ TypeInstanceDeclaration (fromMaybe [] deps) className ty members
124+ return $ TypeInstanceDeclaration name (fromMaybe [] deps) className ty members
124125
125126-- |
126127-- Parse a single declaration
src/Language/PureScript/Sugar/CaseDeclarations.hs+2−2
@@ -54,8 +54,8 @@ desugarCases :: [Declaration] -> Either String [Declaration]
5454desugarCases = desugarRest <=< fmap join . mapM toDecls . groupBy inSameGroup
5555 where
5656 desugarRest :: [Declaration] -> Either String [Declaration]
57− desugarRest ((TypeInstanceDeclaration constraints className tys ds) : rest) =
58− (:) <$> (TypeInstanceDeclaration constraints className tys <$> desugarCases ds) <*> desugarRest rest
57+ desugarRest ((TypeInstanceDeclaration name constraints className tys ds) : rest) =
58+ (:) <$> (TypeInstanceDeclaration name constraints className tys <$> desugarCases ds) <*> desugarRest rest
5959 desugarRest (d : ds) = (:) d <$> desugarRest ds
6060 desugarRest [] = pure []
6161
src/Language/PureScript/Sugar/Names.hs+2−2
@@ -152,8 +152,8 @@ renameInModule :: ImportEnvironment -> Module -> Either String Module
152152renameInModule imports (Module mn decls) =
153153 Module mn <$> mapM updateDecl decls >>= everywhereM (mkM updateType `extM` updateValue `extM` updateBinder `extM` updateVars)
154154 where
155− updateDecl (TypeInstanceDeclaration cs (Qualified Nothing cn) ts ds) =
156− TypeInstanceDeclaration <$> updateConstraints cs <*> updateClassName cn <*> pure ts <*> pure ds
155+ updateDecl (TypeInstanceDeclaration name cs (Qualified Nothing cn) ts ds) =
156+ TypeInstanceDeclaration name <$> updateConstraints cs <*> updateClassName cn <*> pure ts <*> pure ds
157157 updateDecl d = return d
158158
159159 updateVars :: Declaration -> Either String Declaration
src/Language/PureScript/Sugar/TypeClasses.hs+19−37
@@ -16,7 +16,6 @@
1616
1717module Language.PureScript.Sugar.TypeClasses (
1818 desugarTypeClasses,
19− mkDictionaryValueName,
2019 mkDictionaryEntryName
2120) where
2221
@@ -93,10 +92,10 @@ desugarDecl mn d@(TypeClassDeclaration name args members) = do
9392 let tys = map memberToNameAndType members
9493 modify (M.insert (mn, name) (args, tys))
9594 return $ d : typeClassDictionaryDeclaration name args members : map (typeClassMemberToDictionaryAccessor mn name args) members
96−desugarDecl mn d@(TypeInstanceDeclaration deps name ty members) = do
95+desugarDecl mn d@(TypeInstanceDeclaration name deps className ty members) = do
9796 desugared <- lift $ desugarCases members
98− entries <- mapM (typeInstanceDictionaryEntryDeclaration mn deps name ty) desugared
99− dictDecl <- typeInstanceDictionaryDeclaration mn deps name ty desugared
97+ entries <- mapM (typeInstanceDictionaryEntryDeclaration name mn deps className ty) desugared
98+ dictDecl <- typeInstanceDictionaryDeclaration name mn deps className ty desugared
10099 return $ d : entries ++ [dictDecl]
101100desugarDecl _ other = return [other]
102101
@@ -115,69 +114,52 @@ typeClassMemberToDictionaryAccessor mn name args (TypeDeclaration ident ty) =
115114 (quantify (ConstrainedType [(Qualified (Just mn) name, map TypeVar args)] ty))
116115typeClassMemberToDictionaryAccessor _ _ _ _ = error "Invalid declaration in type class definition"
117116
118−typeInstanceDictionaryDeclaration :: ModuleName -> [(Qualified ProperName, [Type])] -> Qualified ProperName -> [Type] -> [Declaration] -> Desugar Declaration
119−typeInstanceDictionaryDeclaration mn deps name tys decls = do
117+typeInstanceDictionaryDeclaration :: Ident -> ModuleName -> [(Qualified ProperName, [Type])] -> Qualified ProperName -> [Type] -> [Declaration] -> Desugar Declaration
118+typeInstanceDictionaryDeclaration name mn deps className tys decls = do
120119 m <- get
121− (args, instanceTys) <- lift $ maybe (Left $ "Type class " ++ show name ++ " is undefined. Type class names must be qualified.") Right
122− $ M.lookup (qualify mn name) m
120+ (args, instanceTys) <- lift $ maybe (Left $ "Type class " ++ show className ++ " is undefined. Type class names must be qualified.") Right
121+ $ M.lookup (qualify mn className) m
123122 let memberTypes = map (second (replaceAllTypeVars (zip args tys))) instanceTys
124− entryName <- lift $ mkDictionaryValueName mn name tys
123+ let entryName = Escaped (show name)
125124 memberNames <- mapM (memberToNameAndValue memberTypes) decls
126125 return $ ValueDeclaration entryName [] Nothing
127126 (TypedValue True
128127 (foldr (Abs . (\n -> Left . Ident $ '_' : show n)) (ObjectLiteral memberNames) [1..max 1 (length deps)])
129128 (quantify (if null deps then
130− function unit (foldl TypeApp (TypeConstructor name) tys)
129+ function unit (foldl TypeApp (TypeConstructor className) tys)
131130 else
132− foldr (function . (\(pn, tys') -> foldl TypeApp (TypeConstructor pn) tys')) (foldl TypeApp (TypeConstructor name) tys) deps))
131+ foldr (function . (\(pn, tys') -> foldl TypeApp (TypeConstructor pn) tys')) (foldl TypeApp (TypeConstructor className) tys) deps))
133132 )
134133 where
135134 memberToNameAndValue :: [(String, Type)] -> Declaration -> Desugar (String, Value)
136135 memberToNameAndValue tys' (ValueDeclaration ident _ _ _) = do
137136 memberType <- lift . maybe (Left "Type class member type not found") Right $ lookup (identToJs ident) tys'
138− memberName <- mkDictionaryEntryName mn name tys ident
137+ memberName <- mkDictionaryEntryName name ident
139138 return (identToJs ident, TypedValue False
140139 (foldl App (Var (Qualified Nothing memberName)) (map (\n -> Var (Qualified Nothing (Ident ('_' : show n)))) [1..length deps]))
141140 (quantify memberType))
142141 memberToNameAndValue _ _ = error "Invalid declaration in type instance definition"
143142
144−typeInstanceDictionaryEntryDeclaration :: ModuleName -> [(Qualified ProperName, [Type])] -> Qualified ProperName -> [Type] -> Declaration -> Desugar Declaration
145−typeInstanceDictionaryEntryDeclaration mn deps name tys (ValueDeclaration ident [] _ val) = do
143+typeInstanceDictionaryEntryDeclaration :: Ident -> ModuleName -> [(Qualified ProperName, [Type])] -> Qualified ProperName -> [Type] -> Declaration -> Desugar Declaration
144+typeInstanceDictionaryEntryDeclaration name mn deps className tys (ValueDeclaration ident [] _ val) = do
146145 m <- get
147146 valTy <- lift $ do (args, members) <- lookupTypeClass m
148147 ty' <- lookupIdent members
149148 return $ replaceAllTypeVars (zip args tys) ty'
150− entryName <- mkDictionaryEntryName mn name tys ident
149+ entryName <- mkDictionaryEntryName name ident
151150 return $ ValueDeclaration entryName [] Nothing
152151 (TypedValue True val (quantify (if null deps then valTy else ConstrainedType deps valTy)))
153152 where
154− lookupTypeClass m = maybe (Left $ "Type class " ++ show name ++ " is undefined. Type class names must be qualified.") Right $ M.lookup (qualify mn name) m
155− lookupIdent members = maybe (Left $ "Type class " ++ show name ++ " does not have method " ++ show ident) Right $ lookup (identToJs ident) members
156−typeInstanceDictionaryEntryDeclaration _ _ _ _ _ = error "Invalid declaration in type instance definition"
153+ lookupTypeClass m = maybe (Left $ "Type class " ++ show className ++ " is undefined. Type class names must be qualified.") Right $ M.lookup (qualify mn className) m
154+ lookupIdent members = maybe (Left $ "Type class " ++ show className ++ " does not have method " ++ show ident) Right $ lookup (identToJs ident) members
155+typeInstanceDictionaryEntryDeclaration _ _ _ _ _ _ = error "Invalid declaration in type instance definition"
157156
158157qualifiedToString :: ModuleName -> Qualified ProperName -> String
159158qualifiedToString mn (Qualified _ pn) = moduleNameToJs mn ++ "_" ++ runProperName pn
160159
161−
162−-- |
163−-- Generate a name for a type class dictionary, based on the module name, class name and type name
164−--
165−mkDictionaryValueName :: ModuleName -> Qualified ProperName -> [Type] -> Either String Ident
166−mkDictionaryValueName mn cl tys = do
167− tyStr <- mapM (typeToString mn) tys
168− return $ Ident $ "__" ++ qualifiedToString mn cl ++ "_" ++ intercalate "_" tyStr
169−
170−typeToString :: ModuleName -> Type -> Either String String
171−typeToString _ (TypeVar _) = return "var"
172−typeToString mn (TypeConstructor ty') = return $ qualifiedToString mn ty'
173−typeToString mn (TypeApp ty' (TypeVar _)) = typeToString mn ty'
174−typeToString _ _ = Left "Type class instance must be of the form T a1 ... an"
175−
176160-- |
177161-- Generate a name for a type class dictionary member, based on the module name, class name, type name and
178162-- member name
179163--
180−mkDictionaryEntryName :: ModuleName -> Qualified ProperName -> [Type] -> Ident -> Desugar Ident
181−mkDictionaryEntryName mn name tys ident = do
182− Ident dictName <- lift $ mkDictionaryValueName mn name tys
183− return $ Escaped $ dictName ++ "_" ++ identToJs ident
164+mkDictionaryEntryName :: Ident -> Ident -> Desugar Ident
165+mkDictionaryEntryName dictName ident = return $ Escaped $ show dictName ++ "_" ++ identToJs ident
src/Language/PureScript/TypeChecker.hs+1−2
@@ -184,9 +184,8 @@ typeCheckAll mainModuleName moduleName (d@TypeClassDeclaration{} : rest) = do
184184 env <- getEnv
185185 ds <- typeCheckAll mainModuleName moduleName rest
186186 return $ d : ds
187−typeCheckAll mainModuleName moduleName (d@(TypeInstanceDeclaration deps className tys _) : rest) = do
187+typeCheckAll mainModuleName moduleName (d@(TypeInstanceDeclaration dictName deps className tys _) : rest) = do
188188 env <- getEnv
189− dictName <- Check . lift $ mkDictionaryValueName moduleName className tys
190189 mapM_ (checkTypeClassInstance moduleName) tys
191190 forM_ deps $ mapM_ (checkTypeClassInstance moduleName) . snd
192191 addTypeClassDictionaries [TypeClassDictionaryInScope (Qualified (Just moduleName) dictName) className tys (Just deps) TCDRegular]