@@ -50,6 +50,44 @@ desugarTypeClasses = flip evalStateT M.empty . mapM desugarModule
50desugarModule :: Module -> Desugar Module
50desugarModule :: Module -> Desugar Module
51desugarModule (Module name decls) = Module name <$> concat <$> mapM (desugarDecl (ModuleName name)) decls
51desugarModule (Module name decls) = Module name <$> concat <$> mapM (desugarDecl (ModuleName name)) decls
54+-- Desugar type class and type class instance declarations
56+-- Type classes become type synonyms for their dictionaries, and type instances become dictionary declarations.
57+-- Additional values are generated to access individual members of a dictionary, with the appropriate type.
66+-- instance Foo String where
69+-- instance (Foo a) => Foo [a] where
74+-- type Foo a = { foo :: a -> a }
76+-- foreign import foo "function foo(dict) {\
77+-- \ return dict.foo;\
78+-- \}" :: forall a. (Foo a) => a -> a
80+-- __Test_Foo_string_foo = (\s -> s ++ s) :: String -> String
82+-- __Test_Foo_string :: Foo String
83+-- __Test_Foo_string = { foo: __Test_Foo_string_foo :: String -> String (unchecked) }
85+-- __Test_Foo_array_foo :: forall a. (Foo a) => [a] -> [a]
86+-- __Test_Foo_array_foo _1 = map (foo _1)
88+-- __Test_Foo_array :: forall a. Foo a -> Foo [a]
89+-- __Test_Foo_array _1 = { foo: __Test_Foo_array_foo _1 :: [a] -> [a] (unchecked) }
53desugarDecl :: ModuleName -> Declaration -> Desugar [Declaration]
91desugarDecl :: ModuleName -> Declaration -> Desugar [Declaration]
54desugarDecl mn d@(TypeClassDeclaration name arg members) = do
92desugarDecl mn d@(TypeClassDeclaration name arg members) = do
55 let tys = map memberToNameAndType members
93 let tys = map memberToNameAndType members
@@ -119,13 +157,6 @@ qualifiedToString :: ModuleName -> Qualified ProperName -> String
119qualifiedToString mn (Qualified Nothing pn) = qualifiedToString mn (Qualified (Just mn) pn)
157qualifiedToString mn (Qualified Nothing pn) = qualifiedToString mn (Qualified (Just mn) pn)
120qualifiedToString _ (Qualified (Just (ModuleName mn)) pn) = runProperName mn ++ "_" ++ runProperName pn
158qualifiedToString _ (Qualified (Just (ModuleName mn)) pn) = runProperName mn ++ "_" ++ runProperName pn
122−quantify :: Type -> Type
123−quantify ty' = foldr (\arg t -> ForAll arg t Nothing) ty' tyVars
125− tyVars = nub $ everything (++) (mkQ [] collect) ty'
126− collect (TypeVar v) = [v]
130-- Generate a name for a type class dictionary, based on the module name, class name and type name
161-- Generate a name for a type class dictionary, based on the module name, class name and type name