git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix #594
commit1baf09e6021b60b89e67e76f6af0c693f4464ba3 authorPhil Freeman <paf31@cantab.net> dateSat 20 Sep 2014, 19:09 (12 years ago) parent708edba
4 files changed+46−16
src/Language/PureScript/Sugar/TypeClasses.hs+26−14 src/Language/PureScript/TypeChecker.hs+14−1 src/Language/PureScript/TypeChecker/Types.hs+2−1 src/Language/PureScript/TypeClassDictionaries.hs+4
src/Language/PureScript/Sugar/TypeClasses.hs+26−14
@@ -151,36 +151,46 @@ desugarModule _ = error "Exports should have been elaborated in name desugaring"
151151-- };
152152-}
153153desugarDecl :: ModuleName -> [DeclarationRef] -> Declaration -> Desugar (Maybe DeclarationRef, [Declaration])
154−desugarDecl mn _ d@(TypeClassDeclaration name args implies members) = do
155− modify (M.insert (mn, name) d)
156− return $ (Nothing, d : typeClassDictionaryDeclaration name args implies members : map (typeClassMemberToDictionaryAccessor mn name args) members)
157−desugarDecl mn exps d@(TypeInstanceDeclaration name deps className tys members) = do
158− desugared <- lift $ desugarCases members
159− dictDecl <- typeInstanceDictionaryDeclaration name mn deps className tys desugared
160− let expRef = if isExportedClass className && all isExportedType (getConstructors `concatMap` tys)
161− then Just $ TypeInstanceRef name
162− else Nothing
163− return $ (expRef, [d, dictDecl])
154+desugarDecl mn exps = go
164155 where
156+ go d@(TypeClassDeclaration name args implies members) = do
157+ modify (M.insert (mn, name) d)
158+ return $ (Nothing, d : typeClassDictionaryDeclaration name args implies members : map (typeClassMemberToDictionaryAccessor mn name args) members)
159+ go d@(ExternInstanceDeclaration name _ className tys) = return (expRef name className tys, [d])
160+ go d@(TypeInstanceDeclaration name deps className tys members) = do
161+ desugared <- lift $ desugarCases members
162+ dictDecl <- typeInstanceDictionaryDeclaration name mn deps className tys desugared
163+ return $ (expRef name className tys, [d, dictDecl])
164+ go (PositionedDeclaration pos d) = do
165+ (dr, ds) <- rethrowWithPosition pos $ desugarDecl mn exps d
166+ return (dr, map (PositionedDeclaration pos) ds)
167+ go other = return (Nothing, [other])
168+
169+ expRef :: Ident -> Qualified ProperName -> [Type] -> Maybe DeclarationRef
170+ expRef name className tys
171+ | isExportedClass className && all isExportedType (getConstructors `concatMap` tys) = Just $ TypeInstanceRef name
172+ | otherwise = Nothing
173+
165174 isExportedClass :: Qualified ProperName -> Bool
166175 isExportedClass = isExported (elem . TypeClassRef)
176+
167177 isExportedType :: Qualified ProperName -> Bool
168178 isExportedType = isExported $ \pn -> isJust . find (matchesTypeRef pn)
179+
169180 isExported :: (ProperName -> [DeclarationRef] -> Bool) -> Qualified ProperName -> Bool
170181 isExported test (Qualified (Just mn') pn) = mn /= mn' || test pn exps
171182 isExported _ _ = error "Names should have been qualified in name desugaring"
183+
172184 matchesTypeRef :: ProperName -> DeclarationRef -> Bool
173185 matchesTypeRef pn (TypeRef pn' _) = pn == pn'
174186 matchesTypeRef _ _ = False
187+
175188 getConstructors :: Type -> [Qualified ProperName]
176189 getConstructors = everythingOnTypes (++) getConstructor
190+
177191 getConstructor :: Type -> [Qualified ProperName]
178192 getConstructor (TypeConstructor tcname) = [tcname]
179193 getConstructor _ = []
180−desugarDecl mn exps (PositionedDeclaration pos d) = do
181− (dr, ds) <- rethrowWithPosition pos $ desugarDecl mn exps d
182− return (dr, map (PositionedDeclaration pos) ds)
183−desugarDecl _ _ other = return (Nothing, [other])
184194
185195memberToNameAndType :: Declaration -> (Ident, Type)
186196memberToNameAndType (TypeDeclaration ident ty) = (ident, ty)
@@ -275,3 +285,5 @@ typeInstanceDictionaryDeclaration name mn deps className tys decls =
275285 typeInstanceDictionaryEntryValue (ValueDeclaration _ _ [] _ val) = val
276286 typeInstanceDictionaryEntryValue (PositionedDeclaration pos d) = PositionedValue pos (typeInstanceDictionaryEntryValue d)
277287 typeInstanceDictionaryEntryValue _ = error "Invalid declaration in type instance definition"
288+
289+
src/Language/PureScript/TypeChecker.hs+14−1
@@ -210,6 +210,7 @@ typeCheckAll mainModuleName moduleName exps = go
210210 let instances = filter (\tcd -> let Qualified (Just mn) _ = tcdName tcd in importedModule == mn) tcds
211211 addTypeClassDictionaries [ tcd { tcdName = Qualified (Just moduleName) ident, tcdType = TCDAlias (canonicalizeDictionary tcd) }
212212 | tcd <- instances
213+ , tcdExported tcd
213214 , let (Qualified _ ident) = tcdName tcd
214215 ]
215216 ds <- go rest
@@ -223,11 +224,23 @@ typeCheckAll mainModuleName moduleName exps = go
223224 go (d@(ExternInstanceDeclaration dictName deps className tys) : rest) = do
224225 mapM_ (checkTypeClassInstance moduleName) tys
225226 forM_ deps $ mapM_ (checkTypeClassInstance moduleName) . snd
226− addTypeClassDictionaries [TypeClassDictionaryInScope (Qualified (Just moduleName) dictName) className tys (Just deps) TCDRegular]
227+ addTypeClassDictionaries [TypeClassDictionaryInScope (Qualified (Just moduleName) dictName) className tys (Just deps) TCDRegular isInstanceExported]
227228 ds <- go rest
228229 return $ d : ds
230+ where
231+ isInstanceExported :: Bool
232+ isInstanceExported = maybe True (any exportsInstance) exps
233+
234+ exportsInstance :: DeclarationRef -> Bool
235+ exportsInstance (TypeInstanceRef name) | name == dictName = True
236+ exportsInstance (PositionedDeclarationRef _ r) = exportsInstance r
237+ exportsInstance _ = False
238+
229239 go (PositionedDeclaration pos d : rest) =
230240 rethrowWithPosition pos $ do
231241 (d' : rest') <- go (d : rest)
232242 return (PositionedDeclaration pos d' : rest')
233243
244+
245+
246+
src/Language/PureScript/TypeChecker/Types.hs+2−1
@@ -863,7 +863,7 @@ check' val t@(ConstrainedType constraints ty) = do
863863 n <- liftCheck freshDictionaryName
864864 return $ Ident $ "__dict_" ++ className ++ "_" ++ show n
865865 val' <- makeBindingGroupVisible $ withTypeClassDictionaries (zipWith (\name (className, instanceTy) ->
866− TypeClassDictionaryInScope name className instanceTy Nothing TCDRegular) (map (Qualified Nothing) dictNames)
866+ TypeClassDictionaryInScope name className instanceTy Nothing TCDRegular False) (map (Qualified Nothing) dictNames)
867867 constraints) $ check val ty
868868 return $ TypedValue True (foldr (Abs . Left) val' dictNames) t
869869check' val (SaturatedTypeSynonym name args) = do
@@ -1146,3 +1146,4 @@ meet e1 e2 t1 t2 = do
11461146
11471147
11481148
1149+
src/Language/PureScript/TypeClassDictionaries.hs+4
@@ -46,6 +46,10 @@ data TypeClassDictionaryInScope
4646 -- The type of this dictionary
4747 --
4848 , tcdType :: TypeClassDictionaryType
49+ -- |
50+ -- Is this instance exported by its module?
51+ --
52+ , tcdExported :: Bool
4953 } deriving (Show, Data, Typeable)
5054
5155-- |