Code Snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
| #nowarn "40"
open System
/// Types (alias types)
type Agent<'a> = MailboxProcessor<'a>
/// Domain agents
let print fn =
Agent.Start(fun inbox ->
let rec loop = async {
let! msg = inbox.Receive()
fn msg
return! loop }
loop)
let duplicate a1 a2 fn =
Agent.Start(fun inbox ->
let rec loop = async {
let! msg = inbox.Receive()
fn a1 a2 msg
return! loop }
loop)
let delay a fn =
Agent.Start(fun inbox ->
let rec loop = async {
let! msg = inbox.Receive()
fn a msg
return! loop }
loop)
let add a fn =
Agent.Start(fun inbox ->
let rec loop state = async {
let! msg = inbox.Receive()
state |> function | None -> () | Some v -> fn a (0I+v+msg);
return! loop (Some (msg))}
loop (None))
/// Domain functions
let out = lazy print (fun msg -> printfn "%A" msg)
let rec delta2int =
lazy
duplicate out (pairsInt:Lazy<Agent<bigint>>)
(fun a1 a2 msg -> a1.Value.Post msg; a2.Value.Post msg)
and prefixInt0 = lazy delay delta2int (fun a msg -> a.Value.Post msg)
and prefixInt1 = lazy delay prefixInt0 (fun a msg -> a.Value.Post msg)
and pairsInt = lazy add prefixInt1 (fun a msg -> a.Value.Post msg)
prefixInt0.Value.Post 0I
prefixInt1.Value.Post 1I
// Main (recursive loop)
let rec main () : unit = main ()
main ()
|
Code output:
Error output (only on Mono):
References: