Skip to content

Instantly share code, notes, and snippets.

@igrep
Created November 16, 2022 09:33
Embed
What would you like to do?
Example type class for a type-level list with no instance for an empty list.
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
import Data.Kind (Type)
data ExampleData (as :: [Type]) = ExampleData
class ExampleClass (as :: [Type]) where
example :: ExampleData as -> String
instance {-# OVERLAPPING #-} ExampleClass '[a] where
example _ = "one"
instance ExampleClass as => ExampleClass (a ': as) where
example _ = "two or more, " ++ example (ExampleData @as)
main = putStrLn $ example (ExampleData @'[Char, Int, Bool])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment