Skip to content

Instantly share code, notes, and snippets.

@palladin
palladin / gist:6270421
Created August 19, 2013 15:28
Effects and Handlers
// http://homepages.inf.ed.ac.uk/slindley/papers/handlers-draft-march2013.pdf
// Quote from the paper
// Effect handler operation clauses generalise exception handler clauses
// by adding a continuation argument, providing support for arbitrary effects. An operation
// clause is an exception clause if it ignores its continuation argument.
type Cont<'T, 'R> = Cont of ((('T -> 'R) * (exn -> 'R)) -> 'R)
type ContBuilder() =
member self.Return x = Cont (fun (k, _) -> k x)
@palladin
palladin / gist:6200023
Created August 10, 2013 11:01
Delimited continuations
type Cont<'T, 'S, 'R> = Cont of (('T -> 'S) -> 'R)
type ContBuilder() =
member self.Return x = Cont (fun k -> k x)
member self.Bind (c : Cont<_, _, _>, f : _ -> Cont<_, _, _>) =
Cont (fun k -> let (Cont contf) = c in contf (fun v -> let (Cont contf') = f v in contf' k))
let delim = new ContBuilder()
let run (c : Cont<_, _, _>) = let (Cont contf) = c in contf id
@palladin
palladin / gist:6157192
Created August 5, 2013 16:15
Session Types in F#
// http://www.eecs.harvard.edu/~tov/pubs/haskell-session-types/session08.pdf
// Basic Operations
[<AbstractClass>]
type Ops = class end
type Eps = class inherit Ops end
type Send<'T, 'Rest when 'Rest :> Ops> = class inherit Ops end
type Recv<'T, 'Rest when 'Rest :> Ops> = class inherit Ops end
type Choose<'Left, 'Right when 'Left :> Ops and 'Right :> Ops> = class inherit Ops end
// http://clojure.com/blog/2012/05/08/reducers-a-library-and-model-for-collection-processing.html
module Reducer =
open System
open System.Text
open System.Collections.Generic
type ReduceFunc<'T,'R> =
abstract Invoke : 'T * 'R -> 'R
@palladin
palladin / gist:5484150
Created April 29, 2013 19:40
Loop Unrolling
type Peano = interface end
and Zero = Zero with
static member inline (|*|) (f, Zero) = f $ Zero
interface Peano
and Succ<'a when 'a :> Peano> = Succ of 'a with
static member inline (|*|) (f, Succ(x)) = f $ Succ(x)
interface Peano
type PeanoToInt = PeanoToInt with
@palladin
palladin / gist:5182248
Created March 17, 2013 16:20
Generic Collections, Type Classes and friends
// http://lampwww.epfl.ch/~odersky/papers/fsttcs2009.pdf
open System
open System.Text
open System.Collections.Generic
// Type classes encoding for iteration and building
type Iterable<'From, 'Elem> = Iterable of (('Elem -> unit) -> unit)
type Iter = Iter with
static member inline ($) (_ : Iter, values : 'Elem []) =
Iterable (fun f ->
@palladin
palladin / gist:5076013
Created March 3, 2013 13:00
GC Friendly Fixpoint
#time
let rec Y f x = f (Y f) x
let Y' f x =
let r = ref Unchecked.defaultof<'a -> 'b>
r := (fun x -> f !r x)
f !r x
let iter f x = if x = 100000000 then x else f (x + 1)
@palladin
palladin / gist:4524544
Created January 13, 2013 15:15
Type Safe Higher-order abstract syntax via GADT encoding
type Expr<'T> =
abstract Eval : unit -> 'T
let eval (x : Expr<'T>) = x.Eval()
let lift (value : 'T) =
{ new Expr<'T> with
member self.Eval () = value
}
let tup (first : Expr<'T>) (second : Expr<'S>) =
@palladin
palladin / gist:4421522
Created December 31, 2012 17:43
Church numerals
type Church =
abstract Apply<'T> : ('T -> 'T) * 'T -> 'T
let zero =
{
new Church with
member self.Apply(f, x) = x
}
let succ (nat : Church) =
@palladin
palladin / gist:3765785
Created September 22, 2012 10:35
raise vs Async.Raise
type Microsoft.FSharp.Control.Async with
static member Raise (e : #exn) = Async.FromContinuations(fun (_,econt,_) -> econt e)
#time
for i in [|1..1000000|] do
try
raise <| new System.Exception()
with ex -> ()
async {