@@ -29,7 +29,7 @@ import Language.PureScript.CodeGen.Common (identToJs)
30import Control.Applicative
30import Control.Applicative
31import Control.Monad.State
31import Control.Monad.State
32−import Control.Arrow (second)
32+import Control.Arrow (first, second)
33import Data.Maybe (catMaybes)
33import Data.Maybe (catMaybes)
35import qualified Data.Map as M
35import qualified Data.Map as M
@@ -97,19 +97,23 @@ desugarDecl mn (PositionedDeclaration pos d) = do
97 return (dr, map (PositionedDeclaration pos) ds)
97 return (dr, map (PositionedDeclaration pos) ds)
98desugarDecl _ other = return (Nothing, [other])
98desugarDecl _ other = return (Nothing, [other])
100−memberToNameAndType :: Declaration -> (String, Type)
100+memberToNameAndType :: Declaration -> (Ident, Type)
101−memberToNameAndType (TypeDeclaration ident ty) = (identToJs ident, ty)
101+memberToNameAndType (TypeDeclaration ident ty) = (ident, ty)
102memberToNameAndType (PositionedDeclaration _ d) = memberToNameAndType d
102memberToNameAndType (PositionedDeclaration _ d) = memberToNameAndType d
103memberToNameAndType _ = error "Invalid declaration in type class definition"
103memberToNameAndType _ = error "Invalid declaration in type class definition"
105+identToProperty :: Ident -> String
106+identToProperty (Ident name) = name
107+identToProperty (Op op) = op
105typeClassDictionaryDeclaration :: ProperName -> [String] -> [Declaration] -> Declaration
109typeClassDictionaryDeclaration :: ProperName -> [String] -> [Declaration] -> Declaration
106typeClassDictionaryDeclaration name args members =
110typeClassDictionaryDeclaration name args members =
107− TypeSynonymDeclaration name args (TypeApp tyObject $ rowFromList (map memberToNameAndType members, REmpty))
111+ TypeSynonymDeclaration name args (TypeApp tyObject $ rowFromList (map (first identToProperty . memberToNameAndType) members, REmpty))
109typeClassMemberToDictionaryAccessor :: ModuleName -> ProperName -> [String] -> Declaration -> Declaration
113typeClassMemberToDictionaryAccessor :: ModuleName -> ProperName -> [String] -> Declaration -> Declaration
110typeClassMemberToDictionaryAccessor mn name args (TypeDeclaration ident ty) =
114typeClassMemberToDictionaryAccessor mn name args (TypeDeclaration ident ty) =
111 ExternDeclaration TypeClassAccessorImport ident
115 ExternDeclaration TypeClassAccessorImport ident
112− (Just (JSFunction (Just $ identToJs ident) ["dict"] (JSBlock [JSReturn (JSAccessor (identToJs ident) (JSVar "dict"))])))
116+ (Just (JSFunction (Just $ identToJs ident) ["dict"] (JSBlock [JSReturn (JSIndexer (JSStringLiteral (identToProperty ident)) (JSVar "dict"))])))
113 (quantify (ConstrainedType [(Qualified (Just mn) name, map TypeVar args)] ty))
117 (quantify (ConstrainedType [(Qualified (Just mn) name, map TypeVar args)] ty))
114typeClassMemberToDictionaryAccessor mn name args (PositionedDeclaration pos d) =
118typeClassMemberToDictionaryAccessor mn name args (PositionedDeclaration pos d) =
115 PositionedDeclaration pos $ typeClassMemberToDictionaryAccessor mn name args d
119 PositionedDeclaration pos $ typeClassMemberToDictionaryAccessor mn name args d
@@ -127,13 +131,13 @@ typeInstanceDictionaryDeclaration name mn deps className tys decls = do
127 -- Replace the type arguments with the appropriate types in the member types
131 -- Replace the type arguments with the appropriate types in the member types
128 let memberTypes = map (second (replaceAllTypeVars (zip args tys))) instanceTys
132 let memberTypes = map (second (replaceAllTypeVars (zip args tys))) instanceTys
129 -- Create values for the type instance members
133 -- Create values for the type instance members
130− memberNames <- mapM (memberToNameAndValue memberTypes) decls
134+ memberNames <- map (first identToProperty) <$> mapM (memberToNameAndValue memberTypes) decls
131 -- Create the type of the dictionary
135 -- Create the type of the dictionary
132 -- The type is an object type, but depending on type instance dependencies, may be constrained.
136 -- The type is an object type, but depending on type instance dependencies, may be constrained.
133 -- The dictionary itself is an object literal, but for reasons related to recursion, the dictionary
137 -- The dictionary itself is an object literal, but for reasons related to recursion, the dictionary
134 -- must be guarded by at least one function abstraction. For that reason, if the dictionary has no
138 -- must be guarded by at least one function abstraction. For that reason, if the dictionary has no
135 -- dependencies, we introduce an unnamed function parameter.
139 -- dependencies, we introduce an unnamed function parameter.
136− let dictTy = TypeApp tyObject (rowFromList (memberTypes, REmpty))
140+ let dictTy = TypeApp tyObject (rowFromList (map (first identToProperty) memberTypes, REmpty))
137 constrainedTy = quantify (if null deps then function unit dictTy else ConstrainedType deps dictTy)
141 constrainedTy = quantify (if null deps then function unit dictTy else ConstrainedType deps dictTy)
138 dict = if null deps then Abs (Left (Ident "_")) (ObjectLiteral memberNames) else ObjectLiteral memberNames
142 dict = if null deps then Abs (Left (Ident "_")) (ObjectLiteral memberNames) else ObjectLiteral memberNames
139 return $ ValueDeclaration name TypeInstanceDictionaryValue [] Nothing (TypedValue True dict constrainedTy)
143 return $ ValueDeclaration name TypeInstanceDictionaryValue [] Nothing (TypedValue True dict constrainedTy)
@@ -141,11 +145,11 @@ typeInstanceDictionaryDeclaration name mn deps className tys decls = do
141 unit :: Type
145 unit :: Type
142 unit = TypeApp tyObject REmpty
146 unit = TypeApp tyObject REmpty
144− memberToNameAndValue :: [(String, Type)] -> Declaration -> Desugar (String, Value)
148+ memberToNameAndValue :: [(Ident, Type)] -> Declaration -> Desugar (Ident, Value)
145 memberToNameAndValue tys' d@(ValueDeclaration ident _ _ _ _) = do
149 memberToNameAndValue tys' d@(ValueDeclaration ident _ _ _ _) = do
146− _ <- lift . maybe (Left $ mkErrorStack "Type class member type not found" Nothing) Right $ lookup (identToJs ident) tys'
150+ _ <- lift . maybe (Left $ mkErrorStack "Type class member type not found" Nothing) Right $ lookup ident tys'
147 let memberValue = typeInstanceDictionaryEntryValue d
151 let memberValue = typeInstanceDictionaryEntryValue d
148− return (identToJs ident, memberValue)
152+ return (ident, memberValue)
149 memberToNameAndValue tys' (PositionedDeclaration pos d) = do
153 memberToNameAndValue tys' (PositionedDeclaration pos d) = do
150 (ident, val) <- memberToNameAndValue tys' d
154 (ident, val) <- memberToNameAndValue tys' d
151 return (ident, PositionedValue pos val)
155 return (ident, PositionedValue pos val)