Created
December 26, 2015 19:29
-
-
Save palladin/becda407f30e15ede8b9 to your computer and use it in GitHub Desktop.
Yet another staged fixed-point combinator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
let fix : (Expr<'T -> 'R> -> Expr<'T -> 'R>) -> Expr<'T -> 'R> = fun f -> | |
<@ let r = ref Unchecked.defaultof<_> | |
r := (% lambda (fun r -> f <@ !(%r) @> ) ) r | |
!r @> | |
let power x f = | |
<@ fun n -> | |
match n with | |
| 0 -> 1 | |
| n -> %x * (%f) (n - 1) @> | |
let power2 = fix (power <@ 2 @>) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment