Created
June 13, 2018 07:05
-
-
Save LeifW/f6c2e6da3647ad43c151cc9e27e46ac6 to your computer and use it in GitHub Desktop.
Depends on data-has
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE FlexibleContexts, FlexibleInstances, UndecidableInstances #-} | |
import Data.Has | |
data Foo = Foo deriving Show | |
data Bar = Bar deriving Show | |
class HasFoo r where | |
foo :: r -> Foo | |
instance HasFoo Foo where | |
foo = id | |
instance Has Foo r => HasFoo r where | |
foo = getter | |
useFoo :: HasFoo r => r -> Foo | |
useFoo = foo | |
{- | |
I want to call this with `useFooAndBar (Foo, Bar)`, but there's no HasFoo instance for that, hence the `instance Has Foo r => HasFoo r`, but that gives: | |
Foo.hs:12:10: error: | |
• The constraint ‘Has Foo r’ is no smaller than the instance head | |
(Use UndecidableInstances to permit this) | |
• In the instance declaration for ‘HasFoo r’ | |
| | |
12 | instance Has Foo r => HasFoo r where | |
| ^^^^^^^^ | |
-} | |
useFooAndBar :: (HasFoo r, Has Bar r) => r -> (Foo, Bar) | |
useFooAndBar x = (foo x, getter x) | |
--class HasBar r where | |
-- bar :: r -> Bar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment