Advertisement
Guest User

Overlapping instances

a guest
Apr 28th, 2021
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE ScopedTypeVariables #-}
  2. {-# LANGUAGE KindSignatures #-}
  3. {-# LANGUAGE FlexibleInstances #-}
  4. {-# LANGUAGE GADTs #-}
  5.  
  6. module Main where
  7.  
  8. main :: IO ()
  9. main = pure ()
  10.  
  11. data Key a where
  12.   KeyInt :: Key Int
  13.  
  14. instance Show (Key a) where
  15.   show KeyInt = "KeyInt"
  16.  
  17. class KnownKey a where
  18.   knownKey :: Maybe (Key a)
  19.   knownKey = Nothing
  20.  
  21. instance {-# INCOHERENT #-} KnownKey Int where
  22.   knownKey = Just KeyInt
  23.  
  24. instance {-# OVERLAPPING #-} KnownKey a where
  25.   knownKey = Nothing
  26.  
  27. myFunc :: forall a. a -> Maybe (Key a)
  28. myFunc val = (knownKey :: Maybe (Key a))
  29. -- hopefully
  30. -- >>> myFunc (3 :: Int) = Just KeyInt
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement