Skip to content

Instantly share code, notes, and snippets.

@Icelandjack
Icelandjack / Yoneda_II.markdown
Last active April 8, 2024 11:08
Yoneda Intuition from Humble Beginnings

(previous Yoneda blog) (reddit) (twitter)

Yoneda Intuition from Humble Beginnings

Let's explore the Yoneda lemma. You don't need to be an advanced Haskeller to understand this. In fact I claim you will understand the first section fine if you're comfortable with map/fmap and id.

I am not out to motivate it, but we will explore Yoneda at the level of terms and at the level of types.

@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.

@Icelandjack
Icelandjack / NewtypeDeriving.markdown
Last active April 4, 2023 04:49
Newtype Deriving
@Icelandjack
Icelandjack / Yoneda.markdown
Last active February 10, 2023 00:06
Yoneda
import Data.Functor.Yoneda
import Data.Char
import Data.Kind

infixr 5
  ·

type  List :: (Type -> Type) -> Constraint
class List f where
@Icelandjack
Icelandjack / TF_QC.md
Last active September 23, 2022 16:10
(#14860) QuantifiedConstraints: Can't quantify constraint involving type family

For the Trac ticket #14860.

All these types are valid for h, but its principal type involves -XQuantifiedConstraints

{-# Language FlexibleInstances, MultiParamTypeClasses, GADTs, QuantifiedConstraints #-}

import Data.Kind

type family FB (a::Type) (b::Type) :: Type where
@Icelandjack
Icelandjack / Product_of_Codes.hs
Created July 30, 2022 05:20
Implement product of datatypes in generics-sop: liftA2 (++) of codes
-- This witnesses multiplying two polynomials
--
-- Code (Maybe a) = [[], [a]]
-- Code (Maybe b) = [[], [b]]
--
-- Then the product of (Maybe a, Maybe b) is isomorphic to the
-- multiplication of their codes
--
-- Code (MegaMaybe a b) = [[], [a], [b], [a,b]]
--
@Icelandjack
Icelandjack / Applicative.hs
Created April 9, 2022 13:02
Idiomatically
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
@Icelandjack
Icelandjack / Applicative_Sum.hs
Last active October 16, 2021 14:34
Deriving Applicative for sums of functors
type f ~> g = (forall x. f x -> g x)
infixr 0 ··>
type (··>) :: (Type -> Type) -> (Type -> Type) -> Type
type f ··> g = Proxy '(f, g) -> Type
type Idiom :: f ··> g -> Constraint
class Idiom (hom :: f ··> g) where
idiom :: f ~> g
@Icelandjack
Icelandjack / nary_composition.agda
Last active October 11, 2021 12:23
Dependent polyvariadic mixfix function composition in Agda
infixr 80 _◦_
_◦_ : forall {l1 l2 l3 : Level }
{A : -> Set l1}
{B : A -> Set l2}
{C : (a : A) -> B a -> Set l3}
(g : {a : A} -> (b : B a) -> C a b )
(f : (a : A) -> B a)
-> (a : A) -> C a (f a)
g ◦ f = \a -> g (f a)
@Icelandjack
Icelandjack / Generics_Distr.hs
Created August 13, 2021 21:14
Simplify and Distribute GHC.Generics
norm :: Generic1 f => Summs (Rep1 f) => f ~> Summ (Rep1 f) Zero
norm as = summs (from1 as) (Proxy @Zero)
class Summs rep where
type Summ rep (end :: Type -> Type) :: Type -> Type
summs :: rep a -> Proxy end -> Summ rep end a
skips :: Proxy rep -> end a -> Summ rep end a
instance Summs Zero where