Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest examples demonstrating the need for nominal type role in Haskell

Tags:

haskell

I'm trying to understand what determines whether a type parameter must be nominal.

While GADTs and type familes seem different in the sense they are not "simple containers" as their instance definitions can "look at" their parameters, can simple types have obvious need for nominal parameters, like Set?

like image 781
sevo Avatar asked Mar 10 '18 13:03

sevo


Video Answer


1 Answers

Should you be able to coerce?

 coerce :: Coercible a b => F a -> F b

If not, then the argument of F should be nominal.

In the case of Set its first role is nominal

type role Set nominal

because we don't want

coerce :: Set Int -> Set ReversedInt

because newtype ReversedInt = RevInt Int deriving (Eq, Ord) via Data.Ord.Down Int has a completely different Ord than Int and the internal structure of Set depends on the ordering of its elements. Same for Map

type role Map nominal representational

For a GADT like

data F :: Type -> Type where
  FInt :: F Int

we have a single inhabitant, so we don't want to allow

coerce :: F Int -> F ReversedInt

So, GHC infers type role F nominal.

like image 197
Iceland_jack Avatar answered Nov 16 '22 01:11

Iceland_jack