Skip to content

Instantly share code, notes, and snippets.

@Icelandjack
Icelandjack / Constraints.org
Last active April 2, 2024 20:22
Type Classes and Constraints

Reddit discussion.

Disclaimer 1: Type classes are great but they are not the right tool for every job. Enjoy some balance and balance to your balance.

Disclaimer 2: I should tidy this up but probably won’t.

Disclaimer 3: Yeah called it, better to be realistic.

Type classes are a language of their own, this is an attempt to document features and give a name to them.

Total Type Families

A total type family feels quite comfortable. By total here, I mean a type family whose equations cover all the possible cases and which is guaranteed to terminate. That is, a total type family is properly a function on types. Those other dependently typed languages have functions on types, and they seem to work nicely. I am completely unbothered by total type families.

What are type families?

Non-covering Type Families (are strange)

A non-covering type family is a type family whose patterns do not cover the whole space.

@Icelandjack
Icelandjack / DerivingClasses.md
Last active January 30, 2017 14:47
Deriving for Classes

Just like you can derive instances for data types (example is from homoiconic)

data Foo = F Int Bool 
 deriving Show

maybe it makes sense to do it for certain type classes.

If you define

@Icelandjack
Icelandjack / ShootingStar.md
Last active August 25, 2017 14:42
Playing around with GHC Trac ticket #12369

Are these examples of

Application

data App0 :: Type -> Type where
  App0 :: a -> App0
@Icelandjack
Icelandjack / TypeApp.markdown
Last active September 6, 2018 13:01
Examples of Type Application for Infix Operators

Nested

Showing where types change:

infixr 5 :::
data Nest a = N | a ::: Nest (a, a)

In

@Icelandjack
Icelandjack / OneZero.markdown
Created March 9, 2017 01:53
Musings on One / Zero

Representable

data One a = One
  deriving Functor

instance Distributive One where
  distribute :: Functor f => f (One a) -> One (f a)
  distribute _ = One
@Icelandjack
Icelandjack / or-patterns_as_expressions.markdown
Last active March 19, 2017 12:54
Or-Patterns as Expressions

HList : Sublist -> Hask

type Cat k = k -> k -> Type

data Sublist :: Cat [a] where
  Stop :: Sublist '[] '[]
  Drop :: Sublist xs ys -> Sublist (x:xs) ys
  Keep :: Sublist xs ys -> Sublist (x:xs) (x:ys)

One of the best parts of Haskell is getting functionality for free

newtype T a = T_ (Compose [] Maybe a) 
  deriving (Functor, Foldable, Traversable, Applicative, Alternative)

{-# Complete T #-}
pattern T :: [Maybe a] -> T a
pattern T a = T_ (Compose a)
@Icelandjack
Icelandjack / NewtypeDeriving.markdown
Last active April 4, 2023 04:49
Newtype Deriving