Skip to content

Instantly share code, notes, and snippets.

@igrep
Created November 16, 2022 09:33
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 igrep/72908d8be0ad5cfe1fd62ef4838b3955 to your computer and use it in GitHub Desktop.
Save igrep/72908d8be0ad5cfe1fd62ef4838b3955 to your computer and use it in GitHub Desktop.
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