Skip to content

Instantly share code, notes, and snippets.

@pete-murphy
Last active November 12, 2021 04:57
Show Gist options
  • Save pete-murphy/9c3a5ff96a277f0c6423fcab23b121e3 to your computer and use it in GitHub Desktop.
Save pete-murphy/9c3a5ff96a277f0c6423fcab23b121e3 to your computer and use it in GitHub Desktop.
Pairing fundeps
module Main where
import Prelude
import Control.Comonad (class Comonad)
import Control.Comonad as Comonad
import Control.Extend (class Extend)
import Control.Extend as Extend
import Data.Array.NonEmpty (NonEmptyArray)
import Data.Array.NonEmpty as NE
import Data.Lazy (Lazy)
import Data.Lazy as Lazy
class Pairing f g | f -> g, g -> f where
pair :: forall a b c. (a -> b -> c) -> f a -> g b -> c
move
:: forall w m a b
. Comonad w
=> Pairing m w
=> w a
-> m b
-> w a
move space movement = pair (\_ newSpace -> newSpace) movement (Comonad.duplicate space)
------------
data Sequence a = Next (Sequence a) | End a
derive instance Functor Sequence
instance Apply Sequence where
apply (End f) = map f
apply (Next next) = apply next
instance Bind Sequence where
bind (End a) f = f a
bind (Next next) f = Next (bind next f)
instance Applicative Sequence where
pure = End
instance Monad Sequence
------------
newtype Stream a = Stream
{ car :: a
, cdr :: Lazy (Stream a)
}
derive instance Functor Stream
instance Comonad Stream where
extract (Stream { car }) = car
instance Extend Stream where
extend f stream@(Stream { cdr }) =
Stream { car: f stream, cdr: map (Extend.extend f) cdr }
------------
instance Pairing Sequence Stream where
pair f (End a) (Stream { car }) = f a car
pair f (Next next) (Stream { cdr }) = pair f next (Lazy.force cdr)
------------
instance Pairing Sequence NonEmptyArray where
pair f (End a) xs = f a (NE.head xs)
pair f (Next next) xs = pair f next xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment