@@ -28,7 +28,7 @@ import Control.Applicative
2828import Control.Monad.State
2929import Data.Maybe (fromMaybe)
3030
31−type MemberMap = M.Map (ModuleName, ProperName) [(String, Type)]
31+type MemberMap = M.Map (ModuleName, ProperName) (String, [(String, Type)])
3232
3333desugarTypeClasses :: [Module] -> [Module]
3434desugarTypeClasses = flip evalState M.empty . mapM desugarModule
@@ -39,7 +39,7 @@ desugarModule (Module name decls) = Module name <$> concat <$> mapM (desugarDecl
3939desugarDecl :: ModuleName -> Declaration -> State MemberMap [Declaration]
4040desugarDecl mn (TypeClassDeclaration name arg members) = do
4141 let tys = map memberToNameAndType members
42− modify (M.insert (mn, name) tys)
42+ modify (M.insert (mn, name) (arg, tys))
4343 return $ typeClassDictionaryDeclaration name arg members : map (typeClassMemberToDictionaryAccessor name arg) members
4444desugarDecl mn (TypeInstanceDeclaration deps name ty members) = do
4545 entries <- mapM (typeInstanceDictionaryEntryDeclaration mn deps name ty) members
@@ -57,29 +57,34 @@ typeClassDictionaryDeclaration name arg members =
5757typeClassMemberToDictionaryAccessor :: ProperName -> String -> Declaration -> Declaration
5858typeClassMemberToDictionaryAccessor name arg (TypeDeclaration ident ty) =
5959 ExternDeclaration ident
60− (Just (JSFunction (Just (Ident arg)) [Ident "dict"] (JSReturn (JSAccessor arg (JSVar (Ident "dict"))))))
60+ (Just (JSFunction (Just ident) [Ident "dict"] (JSBlock [JSReturn (JSAccessor arg (JSVar (Ident "dict")))])))
6161 (ForAll arg (ConstrainedType [(Qualified Nothing name, TypeVar arg)] ty))
6262typeClassMemberToDictionaryAccessor _ _ _ = error "Invalid declaration in type class definition"
6363
6464typeInstanceDictionaryDeclaration :: [(Qualified ProperName, Type)] -> Qualified ProperName -> Type -> [Declaration] -> Declaration
6565typeInstanceDictionaryDeclaration deps name ty decls =
6666 ExternDeclaration (mkDictionaryValueName name ty)
67− (Just (JSObjectLiteral $ map memberToNameAndValue decls))
67+ (Just (JSFunction (Just (mkDictionaryValueName name ty))
68+ (map (\n -> Ident ('_' : show n)) [1..length deps])
69+ (JSBlock [JSReturn (JSObjectLiteral $ map memberToNameAndValue decls)])))
6870 (Function (map (\(pn, ty') -> TypeApp (TypeConstructor pn) ty') deps) (TypeApp (TypeConstructor name) ty))
6971 where
7072 memberToNameAndValue :: Declaration -> (String, JS)
7173 memberToNameAndValue (ValueDeclaration ident _ _ _) =
72− (identToString ident, JSVar $ mkDictionaryEntryName name ty ident)
74+ (identToString ident, let dict = JSVar $ mkDictionaryEntryName name ty ident
75+ in if null deps then dict
76+ else JSApp dict (map (\n -> JSVar (Ident ('_' : show n))) [1..length deps]))
7377 memberToNameAndValue _ = error "Invalid declaration in type instance definition"
7478
7579typeInstanceDictionaryEntryDeclaration :: ModuleName -> [(Qualified ProperName, Type)] -> Qualified ProperName -> Type -> Declaration -> State MemberMap Declaration
7680typeInstanceDictionaryEntryDeclaration mn deps name ty (ValueDeclaration ident binders grd val) = do
7781 m <- get
78− let valTy = fromMaybe (error "Type class instance dictionary member is missing in typeInstanceDictionaryEntryDeclaration") $ do
79− members <- M.lookup (qualify mn name) m
80− lookup (identToString ident) members
82+ let valTy = fromMaybe (error "Type class instance dictionary member is missing in typeInstanceDictionaryEntryDeclaration") $
83+ do (arg, members) <- M.lookup (qualify mn name) m
84+ ty' <- lookup (identToString ident) members
85+ return $ replaceTypeVars arg ty ty'
8186 return $ ValueDeclaration (mkDictionaryEntryName name ty ident) binders grd
82− (TypedValue val (ConstrainedType deps valTy))
87+ (TypedValue val (if null deps then valTy else ConstrainedType deps valTy))
8388typeInstanceDictionaryEntryDeclaration _ _ _ _ _ = error "Invalid declaration in type instance definition"
8489
8590identToString :: Ident -> String