Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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
@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 #-}
{-# 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 }