packages feed

MicroHs-0.17.4.0: tests/Roles.hs

{-# LANGUAGE RoleAnnotations #-}
module Roles(main) where
import Data.Coerce
import Data.Kind (Type)
import RolesDefs

-- Issue #442
type role Foo phantom
data Foo a = Foo deriving Show

foo :: forall (a :: Type) (b :: Type) . Foo a -> Foo b
foo = coerce

-- Roles from another module
p1 :: P Bool Age -> P Char Int                  -- phantom and representational
p1 = coerce

p2 :: Maybe (P Bool Age) -> Maybe (P () Int)    -- nested
p2 = coerce

p3 :: [Age] -> [Int]                            -- no role annotation
p3 = coerce

n1 :: N Int -> N Int                            -- nominal, same type
n1 = coerce

type S x = N x
newtype W = W (N Age) deriving Show

s1 :: S Age -> N Age                            -- synonym, same nominal argument
s1 = coerce

w1 :: W -> N Age                                -- newtype unwrapping to a nominal application
w1 = coerce

g :: Coercible a b => P x a -> P y b            -- given constraint used by the instance context
g = coerce

main :: IO ()
main = do
  print (foo (Foo :: Foo Int) :: Foo Bool)
  print (p1 (P (Age 1)))
  print (p2 (Just (P (Age 2))))
  print (p3 [Age 3])
  print (n1 (N 4))
  print (s1 (N (Age 5)))
  print (w1 (W (N (Age 6))))
  print (g (P (Age 7)) :: P () Int)