[Haskell-beginners] Expose every Function of a wrapped type

Francesco Ariis fa-ml at ariis.it
Fri Nov 15 14:11:12 UTC 2019


Hello Leonardh,
    a couple of ideas:

On Fri, Nov 15, 2019 at 11:36:03AM +0000, Leonhard Applis wrote:
> I currently have a newtype definition of a typed Data.Map. 
> 
> newtype G = G Data.Map String Values 
> 
> [...]
> 
> However, if I have 
> 
> type G2 = Data.Map String Values 

`Map k v` is already an instance of `Monoid` (when `v` is an instance
of `Ord`), are you sure you need to write another one? If the answer
is "yes", you can write a small helper:

    withG :: (M.Map String a -> M.Map String a) -> G a -> G a
    withG mf (G m) = G $ mf m

or use GeneralizedNewtypeDeriving to ease some pain:

    {-# Language GeneralizedNewtypeDeriving #-}

    module Prova where

    import Data.Map as M
    import GHC.Exts

    newtype G a = G { unwrapG :: M.Map String a }
        deriving (Eq, Show, Functor, Foldable)


More information about the Beginners mailing list