Skip to content

Instantly share code, notes, and snippets.

@sjoerdvisscher
sjoerdvisscher / Cofreer.hs
Last active May 3, 2024 10:49
Cofree j-relative comonad
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
module Cofreer where
import Data.Kind (Type)
@sjoerdvisscher
sjoerdvisscher / skewbintree.hs
Last active March 18, 2024 17:13
Skew binary tree with the invariants enforced by the type system, using a nested data type.
{-# LANGUAGE GADTs #-}
type Id = Int
data Path a where
P :: P a -> Path a
Two :: b -> b -> P (Br b) -> (P (Br b) -> P a) -> Path a
data Br a = Br a Id a
data P a = Nil | Zero (P (Br a)) | One a (P (Br a))
@sjoerdvisscher
sjoerdvisscher / OpenStarSemiring.lhs
Created June 14, 2020 13:31
Playing with 'A Very General Method of Computing Shortest Paths'
This is an extension of "A Very General Method of Computing Shortest Paths" to use "open matrices".
This is from a paper "The Open Algebraic Path Problem" by Jade Master https://arxiv.org/abs/2005.06682
> {-# LANGUAGE TypeFamilies #-}
> {-# LANGUAGE TypeApplications #-}
> {-# LANGUAGE FlexibleContexts #-}
> {-# LANGUAGE StandaloneDeriving #-}
> {-# LANGUAGE AllowAmbiguousTypes #-}
> {-# LANGUAGE ScopedTypeVariables #-}
> module OpenStarSemiring where
@sjoerdvisscher
sjoerdvisscher / FeedbackMonad.hs
Last active February 22, 2024 13:36
From Lenses to Composable Continuations, and what lies between (Bob Atkey)
-- https://www.youtube.com/watch?v=YpklMn5yNA0
{-# LANGUAGE RankNTypes, QuantifiedConstraints #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE GADTs #-}
import Prelude (id, (.), ($), Functor(..), (<$>), fst)
import Data.Functor.Identity (Identity(..))
import Data.Void
import Data.Bifunctor (second)
class Feedback m where
@sjoerdvisscher
sjoerdvisscher / Endofunctors.hs
Last active December 28, 2023 01:54
Another go at implementing polynomial functors a la David Spivak
{-# LANGUAGE GHC2021, GADTs, DataKinds #-}
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
import GHC.Generics
@sjoerdvisscher
sjoerdvisscher / diffLinTypes.hs
Last active November 11, 2023 22:45
Deriving differentiation with linear generics
-- https://twitter.com/paf31/status/1362207106703630338
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE LinearTypes #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
@sjoerdvisscher
sjoerdvisscher / KindCat.hs
Last active October 23, 2023 20:11
Profunctor-based category theory
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE StandaloneKindSignatures #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeApplications #-}
@sjoerdvisscher
sjoerdvisscher / Univ.hs
Last active October 23, 2023 20:10
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 #-}
@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 / Comonoid.hs
Last active October 15, 2023 09:50
A dual of Applicative
-- https://github.com/viercc/functor-monad/tree/main/day-comonoid
{-# LANGUAGE GHC2021 #-}
import Data.Functor.Day
import Control.Comonad
data Multi f a where
MZ :: a -> Multi f a
MS :: Multi f (b -> a) -> f b -> Multi f a
fromMulti :: Applicative f => Multi f a -> f a