Skip to content

Instantly share code, notes, and snippets.

@sjoerdvisscher
sjoerdvisscher / FixSquares.hs
Last active April 28, 2026 10:33
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 / LinearStateMonad.hs
Last active March 18, 2026 14:54
Alternative ways to write the linear quicksort from https://www.tweag.io/blog/2021-02-10-linear-base/
{-# LANGUAGE LinearTypes #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE QualifiedDo #-}
import Prelude.Linear hiding (partition)
import qualified Control.Functor.Linear as Linear
import Control.Functor.Linear (State, state, execState, pure)
import qualified Data.Array.Mutable.Linear as Array
@sjoerdvisscher
sjoerdvisscher / Endofunctors.hs
Last active March 16, 2026 23:51
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
@sjoerdvisscher
sjoerdvisscher / LinearClearBits.hs
Created December 25, 2025 15:45
A purely linear clear bits
{-# LANGUAGE GHC2024, LinearTypes #-}
module LinearClearBits where
data With3 a b c where
With3 :: x %1 -> (x %1 -> a) -> (x %1 -> b) -> (x %1 -> c) -> With3 a b c
w1 :: With3 a b c %1 -> a
w1 (With3 x f _ _) = f x
w2 :: With3 a b c %1 -> b
@sjoerdvisscher
sjoerdvisscher / GenericArrow.hs
Created August 13, 2025 14:08
Generic1 ((->) a) instance
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE RankNTypes #-}
module GenericArrow where
@sjoerdvisscher
sjoerdvisscher / Phases.hs
Created July 30, 2025 18:14
Phases with any Ord key type
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DeriveFunctor #-}
module Phases where
import Control.Applicative ((<**>))
import Data.Foldable (sequenceA_)
@sjoerdvisscher
sjoerdvisscher / laws.hs
Last active July 14, 2025 18:14
First class checkable laws using the free-functors package
{-# LANGUAGE
TypeFamilies
, KindSignatures
, ScopedTypeVariables
, ConstraintKinds
, FlexibleInstances
, FlexibleContexts
, DeriveGeneric
, DeriveAnyClass
, TypeApplications
@sjoerdvisscher
sjoerdvisscher / Lib.hs
Last active July 3, 2025 08:30
Broad search for any `Foldable`
-- Inspired by the level-monad package by Sebastian Fischer
module Lib
( search
) where
import Data.Foldable (fold)
import Data.Monoid (First(..))
newtype Levels m = Levels { levels :: [m] }
@sjoerdvisscher
sjoerdvisscher / Parallel.hs
Last active February 19, 2025 16:54
Par Means Parallel: Multiplicative Linear Logic Proofs as Concurrent Functional Programs
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LinearTypes #-}
{-# LANGUAGE QualifiedDo #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeOperators #-}
module Parallel where
import Control.Concurrent (forkIO)
@sjoerdvisscher
sjoerdvisscher / IndexedOptic.hs
Last active February 4, 2025 15:32
Indexed Optics as in section 2 of https://arxiv.org/pdf/2112.11145
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE GADTs #-}
module IndexedOptic where
import Data.Kind (Type)
type f ~> g = forall i. f i -> g i