Skip to content

Instantly share code, notes, and snippets.

Avatar

bleis-tift bleis-tift

View GitHub Profile
View infinite-loop
[|(λv.λv2.v2) ((λv.v v) (λv.v v))|]
1. app2 -> λk1.[|λv.λv2.v2|] (λf1.[|(λv.v v) (λv.v v)|] (λa1.f1 a1 k1))
2. lam -> λk1.(λk2.k2 (λv.[|λv2.v2|])) (λf1.[|(λv.v v) (λv.v v)|] (λa1.f1 a1 k1))
3. lam -> λk1.(λk2.k2 (λv.(λk3.k3 (λv2.[|v2|])))) (λf1.[|(λv.v v) (λv.v v)|] (λa1.f1 a1 k1))
4. val -> λk1.(λk2.k2 (λv.(λk3.k3 (λv2.(λk4.k4 v2))))) (λf1.[|(λv.v v) (λv.v v)|] (λa1.f1 a1 k1))
5. app2 -> λk1.(λk2.k2 (λv.(λk3.k3 (λv2.(λk4.k4 v2))))) (λf1.(λk5.[|λv.v v|] (λf5.[|λv.v v|] (λa5.f5 a5 k5))) (λa1.f1 a1 k1))
6. lam -> λk1.(λk2.k2 (λv.(λk3.k3 (λv2.(λk4.k4 v2))))) (λf1.(λk5.(λk6.k6 (λv.[|v v|])) (λf5.[|λv.v v|] (λa5.f5 a5 k5))) (λa1.f1 a1 k1))
7. app2 -> λk1.(λk2.k2 (λv.(λk3.k3 (λv2.(λk4.k4 v2))))) (λf1.(λk5.(λk6.k6 (λv.(λk7.[|v|] (λf7.[|v|] (λa7.f7 a7 k7))))) (λf5.[|λv.v v|] (λa5.f5 a5 k5))) (λa1.f1 a1 k1))
8. val -> λk1.(λk2.k2 (λv.(λk3.k3 (λv2.(λk4.k4 v2))))) (λf1.(λk5.(λk6.k6 (λv.(λk7.(λk8.k8 v) (λf7.[|v|] (λa7.f7 a7 k7))))) (λf5.[|λv.v v|] (λa5.f5 a5 k5))) (λa1.f1 a1 k1))
9. val -> λk1.(λk2.k2 (λv.(λk3.k3 (
View type-safe-printf.md
[| x       |] = λk.k x                              ・・・val
[| λx.t    |] = λk.k (λx.[|t|])                     ・・・lam
[| π t     |] = λk.[|t|] (λa.k (π a))               ・・・app1
[| t1 t2   |] = λk.[|t1|] (λf.[|t2|] (λa.f a k))    ・・・app2
[| π t1 t2 |] = λk.[|t1|] (λa.[|t2|] (λb.k (π a b)))・・・app3
[| Sx.t    |] = λk.[x↦λa.λk'.k' (k a)]([|t|] (λv.v))・・・shift
[| <t>     |] = λk.k ([|t|] (λv.v))                 ・・・reset

            [|<concat (Sk.λx.k x) (Sk.λx.k (i2s x))> "a" 10|]
View gist:99875ff6fbc2c3861b8907bf7b2336f7
[| x     |] = λk.k x                              ・・・val
[| λx.t  |] = λk.k (λx.[|t|])                     ・・・lam
[| π t   |] = λk.[|t|] (λa.k (π a))               ・・・app1
[| t1 t2 |] = λk.[|t1|] (λf.[|t2|] (λa.f a k))    ・・・app2
[| Sx.t  |] = λk.[x↦λa.λk'.k' (k a)]([|t|] (λv.v))・・・shift
[| <t>   |] = λk.k ([|t|] (λv.v))                 ・・・reset

変換対象: <add10 (Sx.x (x 100))>
View gist:57da660849bca1cbc342ef91a6c55281

CPS

CPS, CPS変換とは

以下のような普通の関数を考える。

let add x y = x + y;;
let mul x y = x * y;;

let f () =
  let a = add 1 2 in
View sample.fs
type CommandInfo =
{ Names: string[]
Summary: string
Syntax: string
Help: string option }
type CommandWrapper(info: CommandInfo, proc: string -> unit) =
inherit Command()
override __.Names = info.Names
View shadowing.fs
//// シャドーイングの例
// これは無限ループ
let x = 0
while x < 10 do // ここでのxは常に最初のx
let x = x + 1 // 新しいxのスコープはwhileの本体のみで有効
printfn "%d" x
//// 再代入の例
@bleis-tift
bleis-tift / Program.fs
Created Apr 11, 2017
コレクション初期化子もどき
View Program.fs
open System.Collections.Generic
type CollectionBuilder< 'TCollection, 'TElem, 'TRet when 'TCollection : (member Add : 'TElem -> 'TRet)> () =
[<DefaultValue>] val mutable xs : 'TCollection
member inline this.Yield(x: 'TElem) =
(^TCollection : (member Add : 'TElem -> 'TRet) (this.xs, x)) |> ignore
member inline this.Delay(f) = f
member inline this.Combine(_, rest) = rest ()
member inline this.Run(f) = f (); this.xs
member inline this.Zero() = ()
View 57.fs
//================================================================================
// 共通
//================================================================================
type EmailAddress =
{ EAValue: string }
type SurfaceAddress =
{ SAValue: string }
type Customer =
View gist:376b8a772adbf03d85bb6c77d03ad99a

Computation Expression Problems

1. The translation rule of do! e;

In the language specification, the translation rule of do! e; is defined as follows:

T(do! e;, V, C, q) = T(let! () = src(e) in b.Return(), V, C, q)

And the signature of Return is usually 'a -> M<'a>, so the type of do! e; results M<unit>.

View gist:3c0c520b95c3a062e71972f5355db542
AsyncTaskMethodBuilderのStartが呼び出される
stateMachine(生成されるクラス)のMoveNextが呼び出される
非同期メソッドの呼び出し結果に対してGetAwaiterが呼び出される
AsyncTaskMethodBuilderのAwaitUnsafeOnCompletedが呼び出される
AwaitUnsafeOnCompletedが呼び出される
GetCompletionActionが呼び出される
AsyncMethodBuilder.MoveNextRunnerが生成される(ここにstateMachineのインスタンスが渡される)
AsyncMethodBuilder.MoveNextRunnerのRunが返される(中でstateMachineのMoveNextを呼び出している)
awaiterのUnsafeOnCompletedが返されたactionを渡して呼び出される
ここからはAwaiter次第だが、TaskAwaiterを例にする