Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explicitly import instances

How do I explicitly import typeclass instances? Also, how do I do this with a qualified import?

Currently, I'm doing

import Control.Monad.Error () 

to import the monad instance that I can use for (Either String). Previously, I used

import Control.Monad.Error 

I'm not satisfied with either one, because the Monad instance is implicitly imported.

like image 244
Matt Fenwick Avatar asked Jan 04 '12 14:01

Matt Fenwick


2 Answers

The inability to control imports of instances is one of the trade-offs the Haskell typeclass system makes. Here's an example in a hypothetical Haskell dialect where you can:

Foo.hs:

module Foo where  data Foo = FooA | FooB deriving (Eq, Ord) 

Bar.hs:

module Bar (myMap) where  import Data.Map (Map) import qualified Data.Map as Map  import Foo  myMap :: Map Foo Int myMap = Map.singleton FooA 42 

Baz.hs:

module Baz where  import Data.Map (Map) import qualified Data.Map as Map  import Foo hiding (instance Ord Foo) import Bar (myMap)  instance Ord Foo where   FooA > FooB = True   FooB > FooA = False  ouch :: Map Foo Int ouch = Map.insert FooB 42 myMap 

Yikes! The set myMap was created with the proper instance Ord Foo, but it's being combined with a map created with a different, contradictory instance.

Being able to do this would violate Haskell's open world assumption. Unfortunately, I don't know of a good, centralised resource for learning about it. This section of RWH might be helpful (I searched for "haskell open world assumption").

like image 191
ehird Avatar answered Sep 20 '22 06:09

ehird


You can't. Instances are always implicitly exported and hence you can't explicitly import them. By the way, Either e's Monad instance is nowadays in Control.Monad.Instances.

like image 31
Daniel Fischer Avatar answered Sep 23 '22 06:09

Daniel Fischer