Skip to content

Instantly share code, notes, and snippets.

@palladin
palladin / Streamsig.fsx
Last active September 25, 2021 17:52
Stream sig
type Stream<'T> // abstract type
module Stream =
// general combinators
val map : (Expr<'A> -> Expr<'B>) -> Stream<'A> -> Stream<'B>
val collect : (Expr<'A> -> Stream<'B>) -> Stream<'A> -> Stream<'B>
val filter : (Expr<'A> -> Expr<bool>) -> Stream<'A> -> Stream<'A>
val take : Expr<int> -> Stream<'A> -> Stream<'A>
val zipWith : (Expr<'A> -> Expr<'B> -> Expr<'C>) -> Stream<'A> -> Stream<'B> -> Stream<'C>
// ...
@palladin
palladin / regex.fsx
Created December 31, 2015 16:20
A Staged Regular Expression Matcher
// http://www.cs.princeton.edu/courses/archive/spr09/cos333/beautiful.html
// http://scala-lms.github.io/tutorials/regex.html
#r "../packages/FSharp.Compiler.Service.1.3.1.0/lib/net45/FSharp.Compiler.Service.dll"
#r "../packages/QuotationCompiler.0.0.7-alpha/lib/net45/QuotationCompiler.dll"
open QuotationCompiler
@palladin
palladin / fix.fsx
Created December 26, 2015 19:29
Yet another staged fixed-point combinator
open Microsoft.FSharp.Quotations
// <@ fun x -> (% <@ x @> ) @> ~ lambda (fun x -> x)
let lambda (f : Expr<'T> -> Expr<'R>) : Expr<'T -> 'R> =
let var = new Var("__temp__", typeof<'T>)
Expr.Cast<_>(Expr.Lambda(var, f (Expr.Cast<_>(Expr.Var var))))
// Staged fixed-point combinator
@palladin
palladin / ack.fsx
Created December 26, 2015 19:27
Staged Ackermann
// http://lambda-the-ultimate.org/node/4039#comment-61431
open Microsoft.FSharp.Quotations
// <@ fun x -> (% <@ x @> ) @> ~ lambda (fun x -> x)
let lambda (f : Expr<'T> -> Expr<'R>) : Expr<'T -> 'R> =
let var = new Var("__temp__", typeof<'T>)
Expr.Cast<_>(Expr.Lambda(var, f (Expr.Cast<_>(Expr.Var var))))
// Staged fixed-point combinator
@palladin
palladin / fold.fsx
Created December 26, 2015 19:26
Type-level Fold
type Bool = interface end
and True = True with
static member inline (|*|) (f, True) = (BoolFold ? (f) <- True)
interface Bool
and False = False with
static member inline (|*|) (f, False) = (BoolFold ? (f) <- False)
interface Bool
and And<'a, 'b when 'a :> Bool and 'b :> Bool> = And of 'a * 'b with
static member inline (|*|) (f, x) = (BoolFold ? (f) <- x)
interface Bool
@palladin
palladin / partition.fsx
Created October 20, 2015 10:00
CloudFlow.partition
let partition : CloudFlow<'T> -> ('T -> bool) -> (CloudFlow<'T> * CloudFlow<'T>) = fun flow f ->
(flow |> CloudFlow.filter f), (flow |> CloudFlow.filter (fun x -> not (f x)))
@palladin
palladin / stage-fix.fsx
Created October 10, 2015 22:00
Staged Fixed-point combinator
open Microsoft.FSharp.Quotations
// <@ fun x -> (% <@ x @> ) @> ~ lambda (fun x -> x)
let lambda (f : Expr<'T> -> Expr<'R>) : Expr<'T -> 'R> =
let var = new Var("__temp__", typeof<'T>)
Expr.Cast<_>(Expr.Lambda(var, f (Expr.Cast<_>(Expr.Var var))))
// fixed-point combinator
let rec fix : (('Τ -> 'R) -> ('Τ -> 'R)) -> 'Τ -> 'R = fun f x ->
@palladin
palladin / stage-syb.fsx
Created October 10, 2015 21:59
Stage your boilerplate
open Microsoft.FSharp.Quotations
// <@ fun x -> (% <@ x @> ) @> ~ lambda (fun x -> x)
let lambda (f : Expr<'T> -> Expr<'R>) : Expr<'T -> 'R> =
let var = new Var("__temp__", typeof<'T>)
Expr.Cast<_>(Expr.Lambda(var, f (Expr.Cast<_>(Expr.Var var))))
// encoding of rank-2 polymorphism
@palladin
palladin / mfix.fsx
Created September 24, 2015 15:22
MonadFix in F#
let force (value : Lazy<_>) = value.Force()
let fix : (Lazy<'T> -> 'T) -> Lazy<'T> = fun f ->
let rec x = lazy (f x) in x
// Maybe MonadFix
let mfix : (Lazy<'T> -> option<Lazy<'T>>) -> Lazy<option<Lazy<'T>>> = fun f ->
let rec x = lazy ( match force x with | Some v -> f v | None -> None ) in x
@palladin
palladin / functionalFutures.fsx
Last active September 25, 2021 17:52
Programming with distributed functional futures
open MBrace.Core
f : Cloud<Int>
g,h : int -> Cloud<Int>
j,k : int -> int -> Cloud<int>
let spawn c = Cloud.StartAsTask c
let get t = Cloud.AwaitTask t
let (>>=) c f = cloud.Bind(c, f)