Skip to content

Instantly share code, notes, and snippets.

@sjoerdvisscher
sjoerdvisscher / FreeWeb.hs
Created December 19, 2009 16:35
Free Monad Web example
import Control.Monad.Free -- from category-extras
data WebF r = Display String r | Form String (String -> r)
instance Functor WebF where
fmap f (Display m r) = Display m (f r)
fmap f (Form m g) = Form m (f . g)
type Web = Free WebF
@sjoerdvisscher
sjoerdvisscher / HFunctorCombinators.hs
Created December 21, 2009 23:43
HFunctor combinators
{-# LANGUAGE RankNTypes, TypeOperators, KindSignatures, ScopedTypeVariables #-}
import Control.Functor.HigherOrder
import Control.Functor.Extras
newtype K k (r :: * -> *) a = K k
newtype I f (r :: * -> *) a = I (r (f a))
newtype E (r :: * -> *) a = E a
data (f :*: g) (r :: * -> *) a = f r a :*: g r a
data (f :+: g) (r :: * -> *) a = L (f r a) | R (g r a)
@sjoerdvisscher
sjoerdvisscher / SLC.hs
Created December 29, 2009 10:28
Symmetric Lambda Calculus
{-# LANGUAGE GADTs, KindSignatures #-}
data Val = B Int | Unit | Pair Val Val | In1 Val | In2 Val | Closr (Val -> Cnt -> Ans) | Contx Val Cnt
type Cnt = Val -> Ans
type Env = Ide -> Either Val Cnt
type Ans = IO ()
type Ide = String
data E :: * -> * where
@sjoerdvisscher
sjoerdvisscher / CBV.hs
Created January 18, 2010 14:05
CBV denotational semantics for categorical terms (from Declarative Continuations and Categorical Duality by Filinski)
{-# LANGUAGE MultiParamTypeClasses, KindSignatures #-}
module CBV where
import Control.Functor
import Control.Category
import Control.Category.Object
import Control.Category.Associative
import Control.Category.Braided
import Control.Category.Cartesian
import Control.Category.Cartesian.Closed
@sjoerdvisscher
sjoerdvisscher / category.hs
Created January 24, 2010 13:39
Categorical functors with composition and identity
{-# LANGUAGE TypeOperators, TypeFamilies, EmptyDataDecls, UndecidableInstances, RankNTypes, FlexibleContexts #-}
import Prelude hiding ((.), id)
import Control.Category
import Control.Arrow (Kleisli(..))
type family Obj (f :: * -> *) a :: *
class CFunctor f where
type Dom f :: * -> * -> *
type Cod f :: * -> * -> *
@sjoerdvisscher
sjoerdvisscher / browserdifferences.html
Created February 23, 2010 16:10
Browsers completely disagree what \s should match
<script>
var r = /\s/, a = [];
for (var i = 0; i < 0x10000; i++)
if (r.test(String.fromCharCode(i)))
a.push(i.toString(16));
document.write(a);
</script>
<p>IE: 9,a,b,c,d,20</p>
<p>FF: 9,a,b,c,d,20,a0,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,200a,200b,2028,2029,3000</p>
<p>Chrome/Safari/Opera:
@sjoerdvisscher
sjoerdvisscher / gist:338607
Created March 20, 2010 10:43
Type level Fin
data Z = Z
newtype S n = S n
data family Fin n a b :: *
data instance Fin (S n) n Z = FZ
newtype instance Fin (S n) a (S b) = FS (Fin (S n) (S a) b)
{-# LANGUAGE NoMonomorphismRestriction, TypeSynonymInstances, FlexibleInstances, MultiParamTypeClasses #-}
class ExpSYM repr where
lit :: Int -> repr
neg :: repr -> repr
add :: repr -> repr -> repr
class Ann repr e where
a :: e -> repr -> repr
@sjoerdvisscher
sjoerdvisscher / Repeated dissection
Created August 8, 2010 20:02
Exactly like clowns and jokers but with the same input and output types so dissection can be repeated.
{-# LANGUAGE TypeFamilies, EmptyDataDecls, TypeOperators, FlexibleInstances, ScopedTypeVariables #-}
import Data.Maybe (catMaybes)
import Data.List (intercalate)
import Control.Applicative (liftA2)
-- Void datatypes as labels, which are easier to work with
data K a
data X
data Rec
@sjoerdvisscher
sjoerdvisscher / ghcloop.hs
Created October 14, 2010 11:29
How to make ghc loop.
{-# LANGUAGE EmptyDataDecls #-}
data Empty
data Loop = CLoop (Loop -> Empty)
loop_step :: Loop -> Empty
loop_step b@(CLoop f) = f b
loop :: Empty