Skip to content

Instantly share code, notes, and snippets.

@sjoerdvisscher
sjoerdvisscher / Univ.hs
Last active January 14, 2025 14:27
Universal properties with plain Control.Category
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE StandaloneKindSignatures #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE RankNTypes #-}
import Prelude hiding (id, (.))
import Control.Category
import Data.Functor.Coyoneda
import Data.Bifunctor (first)
newtype Object f g = Object { runObject :: forall a. f a -> g (Object f g, a) }
newtype Obj f g = Obj { runObj :: forall a. f a -> Coyoneda g (Obj f g, a) }
to :: Object f g -> Obj f g
@sjoerdvisscher
sjoerdvisscher / FixSquares.hs
Last active August 9, 2023 15:39
Folds and unfolds using squares
-- Using https://hackage.haskell.org/package/squares
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
@sjoerdvisscher
sjoerdvisscher / ProAlg.hs
Last active July 30, 2023 10:28
Algebra for a profunctor
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
import Data.Profunctor
import Data.Profunctor.Composition
import Data.Functor.Const
@sjoerdvisscher
sjoerdvisscher / Rift.hs
Last active October 23, 2023 20:10
All types from the kan-extensions package are special cases of Procompose, Rift and Ran from the profunctors package.
{-# LANGUAGE StandaloneKindSignatures, GADTs, DataKinds, PolyKinds, RankNTypes, TypeOperators #-}
module Rift where
import Data.Bifunctor.Clown
import Data.Bifunctor.Joker
import Data.Profunctor
import Data.Profunctor.Cayley
import Data.Profunctor.Composition
import Data.Profunctor.Ran
import Data.Kind (Type)
@sjoerdvisscher
sjoerdvisscher / Confluence.hs
Created July 14, 2023 16:58
Confluence? Oh, you mean a distributive law between Promonads! https://elk.zone/types.pl/@totbwf/110712999795376253
{-# LANGUAGE RankNTypes, GADTs, TypeOperators #-}
import Data.Profunctor
data Exists2 f g where
Exists2 :: f x -> g x -> Exists2 f g
data Confluence p = Confluence (forall a b c. (p a b, p a c) -> Exists2 (p b) (p c))
newtype Op p a b = Op { runOp :: p b a }
@sjoerdvisscher
sjoerdvisscher / Promonoidal.hs
Created July 10, 2023 09:22
(Strong) Promonoidal functors
{-# LANGUAGE TypeOperators, RankNTypes, TupleSections, GADTs, DeriveFunctor, FlexibleInstances, FlexibleContexts, MultiParamTypeClasses, TypeFamilies, AllowAmbiguousTypes, TypeApplications, ScopedTypeVariables, UndecidableInstances #-}
import Data.Bifunctor
import Data.Functor.Kan.Lan
import Data.Kind (Type)
import Data.Void (Void, absurd)
type f ~> g = forall a. f a -> g a
class Bifunctor m => Tensor m where
@sjoerdvisscher
sjoerdvisscher / Endofunctors.hs
Last active August 11, 2025 12:27
Another go at implementing polynomial functors a la David Spivak
{-# LANGUAGE GHC2021, GADTs, DataKinds #-}
{-# LANGUAGE TypeData #-}
module Endofunctors where
import Control.Comonad (Comonad(..))
import Control.Comonad.Cofree (Cofree(..))
import Control.Monad (join, ap, void)
import Control.Monad.Free (Free(..))
import Data.Bifunctor (first, second, bimap)
import Data.Functor.Day
{-# LANGUAGE ConstraintKinds, TypeApplications, ScopedTypeVariables, FlexibleInstances, RankNTypes, DeriveFunctor #-}
import Data.Functor.Compose
import Data.Functor.Identity
class Cofunctor f where
comap :: (f a -> f b) -> (a -> b)
instance Cofunctor Identity where
comap f = runIdentity . f . Identity
@sjoerdvisscher
sjoerdvisscher / StateMachine.hs
Last active March 2, 2023 17:08
State machines as natural transformations between (polynomial) functors, a la David Spivak
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE TypeOperators #-}
module StateMachine where
import Control.Comonad (extract)
import Control.Comonad.Cofree
import Data.Bifunctor (first, second)
import Data.Functor.Day