Skip to content

Instantly share code, notes, and snippets.

View raichoo's full-sized avatar

raichoo raichoo

View GitHub Profile
@raichoo
raichoo / Codensity.agda
Created September 11, 2015 18:44
Codensity Monad in Agda
module Codensity where
open import Level
open import Function
data Codensity {l} (M : Set l → Set l) (A : Set l) : Set (suc l) where
codensity : ({B : Set l} → (A → M B) → M B) → Codensity M A
runCodensity : ∀ {l} {A : Set l} {M : Set l → Set l}
→ Codensity M A
@raichoo
raichoo / Church.hs
Created August 22, 2015 23:05
Playing with Church Encoding
{-# LANGUAGE RankNTypes #-}
module Church where
import Prelude hiding (and, fst, snd, or, either, succ, toInteger)
type Prod a b = forall c. (a -> b -> c) -> c
and :: a -> b -> Prod a b
and x y p = p x y
@raichoo
raichoo / coyo.idr
Last active October 26, 2017 22:27
Using Coyoneda to improve performance in Idris
module Main
{-
Strictness always bothers me a little, since it forces you to do
things like fusion manually. This prohibits code reuse. I won't
elaborate on this too much since there is already a great blog
post about this:
http://augustss.blogspot.co.uk/2011/05/more-points-for-lazy-evaluation-in.html
@raichoo
raichoo / gist:f1ecaff3a465bbbd797b
Created July 16, 2015 21:04
Comonad: Dual of a Monad
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RebindableSyntax #-}
module Comonad where
import Prelude (Functor(..))
class Functor m => Monad m where
return :: a -> m a
join :: m (m a) -> m a
@raichoo
raichoo / gist:4d88028301b452818267
Last active August 29, 2015 14:22
Polynomial functors, initial algebras and catamorphisms
{-# LANGUAGE PatternSynonyms #-}
module Polynomial where
import Prelude hiding (Maybe(..))
class Bifunctor f where
bimap :: (a -> b) -> (c -> d) -> f a c -> f b d
instance Bifunctor (,) where
bimap f g (x, y) = (f x, g y)
@raichoo
raichoo / gist:c6a907c7fb62a87f5265
Created June 5, 2015 23:30
Playing with polynomial functors
module Polynomial where
import Prelude hiding (Maybe(..))
class Bifunctor f where
bimap :: (a -> b) -> (c -> d) -> f a c -> f b d
instance Bifunctor (,) where
bimap f g (x, y) = (f x, g y)
@raichoo
raichoo / gist:8a94fbc706501a4e7864
Last active August 29, 2015 14:22
Preventing GND from doing nasty stuff
-- This MinList example is taken from the Safe Haskell paper
{-# LANGUAGE Safe #-}
{-# LANGUAGE RoleAnnotations #-}
module MinList (MinList, newMinList, insertMin) where
data MinList a = MinList a [a]
deriving Show
type role MinList nominal
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
module Main where
import Control.Monad.Free
import Control.Monad.State hiding (withState)
import Data.Functor.Coyoneda
import Data.Char (toUpper)
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FunctionalDependencies #-}
module MonadBase where
import Language.Haskell.TH
class (Monad m, Monad t) => MonadBase m t | t -> m where
@raichoo
raichoo / gist:01969563d1a534cf4f26
Last active June 18, 2025 08:44
Typed Interpreter in Haskell
{-# OPTIONS_GHC -Wall -fno-warn-incomplete-patterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE NPlusKPatterns #-}
module Main where