@@ -21,7 +21,6 @@ import qualified Reporting.Region as R
2121import qualified Canonicalize.Environment as Env
2222import qualified Canonicalize.Result as Result
2323import qualified Canonicalize.Type as Canonicalize
24−import qualified Transform.Interface as Interface
2524
2625
2726environment
@@ -55,7 +54,7 @@ importPatches
5554 -> A.Located (Module.Name, Module.ImportMethod)
5655 -> Result.ResultErr [Env.Patch]
5756importPatches allInterfaces (A.A region (importName, method)) =
58− case Interface.filterExports <$> Map.lookup importName allInterfaces of
57+ case restrictToPublicApi <$> Map.lookup importName allInterfaces of
5958 Nothing
6059 | Module.nameIsNative importName ->
6160 Result.ok []
@@ -332,3 +331,59 @@ declToPatches moduleName (A.A region decl) =
332331 ( Nothing
333332 , []
334333 )
334+
335+
336+-- RESTRICT VISIBLE API OF INTERFACES
337+
338+restrictToPublicApi :: Module.Interface -> Module.Interface
339+restrictToPublicApi interface =
340+ interface
341+ { Module.iTypes =
342+ Map.fromList $
343+ Maybe.mapMaybe
344+ (get (Module.iTypes interface))
345+ (Var.getValues exports ++ ctors)
346+
347+ , Module.iAliases =
348+ Map.fromList $
349+ Maybe.mapMaybe
350+ (get (Module.iAliases interface))
351+ (Var.getAliases exports)
352+
353+ , Module.iAdts =
354+ Map.fromList
355+ (Maybe.mapMaybe (trimUnions interface) unions)
356+ }
357+ where
358+ exports :: [Var.Value]
359+ exports =
360+ Module.iExports interface
361+
362+ unions :: [(String, Var.Listing String)]
363+ unions =
364+ Var.getUnions exports
365+
366+ ctors :: [String]
367+ ctors =
368+ concatMap (\(_, Var.Listing ctorList _) -> ctorList) unions
369+
370+
371+get :: Map.Map String a -> String -> Maybe (String, a)
372+get dict key =
373+ case Map.lookup key dict of
374+ Nothing -> Nothing
375+ Just value -> Just (key, value)
376+
377+
378+trimUnions
379+ :: Module.Interface
380+ -> (String, Var.Listing String)
381+ -> Maybe (String, Module.AdtInfo String)
382+trimUnions interface (name, Var.Listing exportedCtors _) =
383+ case Map.lookup name (Module.iAdts interface) of
384+ Nothing -> Nothing
385+ Just (tvars, ctors) ->
386+ let isExported (ctor, _) =
387+ ctor `elem` exportedCtors
388+ in
389+ Just (name, (tvars, filter isExported ctors))