Skip to content

Instantly share code, notes, and snippets.

@joncol
Last active December 29, 2021 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joncol/50461ec618fe1fb7b7245c5d7869e3ed to your computer and use it in GitHub Desktop.
Save joncol/50461ec618fe1fb7b7245c5d7869e3ed to your computer and use it in GitHub Desktop.
GADTs example
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
module GADTs where
data Subject = Math | English | French
deriving (Eq, Show)
class MathOrEnglish (a :: Subject)
instance MathOrEnglish 'Math
instance MathOrEnglish 'English
data User a where
Teacher :: Subject -> User a
-- Uncommenting line 25 below gives the error:
-- • Expected a type, but ‘a’ has kind ‘Subject’
-- • In the type ‘a’
-- In the definition of data constructor ‘Student’
-- In the data declaration for ‘User’ (lsp)
-- Student :: (MathOrEnglish a) => a -> User a -- doesn't work
Student :: (Eq a) => a -> User a -- works
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment