How to export classes?

I am creating a Common.hs file that exports the functions that I usually use. It also replaces some of the Prelude functions with versions that I prefer:

 -- Common.hs module Common import qualified Prelude as Prelude import qualified Data.Foldable as Foldable sum = Foldable.sum -- I want to replace Prelude sum print = Prelude.print -- I want to export Prelude print type Int = Prelude.Int -- I want to export Prelude Int ... etc (huge list) ... 

That way, whenever I create a new Haskell file, I just need to import Common and compile it with -XNoImplicitPrelude :

 -- Test.hs import Common main = print (sum [1,2,3]) -- will be the `sum` version I want 

This approach seems to work, but I could not figure out how to export the classes:

 -- Test.hs import Common import Prelude (Show) data Foo = Foo Int Int deriving Show main = print (Foo 1 2) 

Please note that I had to manually import the Prelude Show. Can I export it to Common.hs ?

+5
source share
1 answer

You must explicitly re-export type classes (this means that you also have to explicitly export everything else), i.e.

Your foreplay:

 module CustomPrelude ( sum, product, Prelude.IO, Prelude.String, Prelude.print, Prelude.Show, Prelude.show, (Prelude.$), Prelude.Int, (++) ) where import qualified Prelude import Data.Foldable import Data.Monoid (++) :: Monoid m => m -> m -> m (++) = mappend 

Using:

 {-# LANGUAGE NoImplicitPrelude #-} module PreludeTest where import CustomPrelude data A x = A xx instance Show x => Show (A x) where show (A xy) = "A " ++ show x ++ " " ++ show y main :: IO () main = print $ A (1::Int) 2 

To save yourself typing (and to preserve sanity while editing), you probably want to do: A) Divide your new foreplay into several specialized files B) Export the module so that you do not have to explicitly repeat everything

eg:.

 module CustomPrelude.Classes (Show,show,Functor,fmap, Monoid,mempty,mappend, Monad,return,(>>=)) where import Data.Monoid 

 module CustomPrelude.Functions ( module ReExport, Prelude.print, Prelude.IO, -- ok these technically aren't functions, but I didn't want to make yet another module just for demonstration purposes Prelude.String, Prelude.Int, (Prelude.$), (++) ) where import qualified Prelude import Data.Foldable as ReExport import Data.Traversable as ReExport import Data.Monoid as ReExport (++) :: Monoid m => m -> m -> m (++) = mappend 

 module CustomPrelude ( module P ) where import CustomPrelude.Classes as P import CustomPrelude.Functions as P 

Please note that you can save a little on work by renaming your re-exported modules under the same name when importing.

+9
source

All Articles