Fork me on GitHub

All

Code Snippet:

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

Code output:

type Agent<'a> = MailboxProcessor<'a>
val print : fn:('a -> unit) -> MailboxProcessor<'a>
val duplicate :
  a1:'a -> a2:'b -> fn:('a -> 'b -> 'c -> unit) -> MailboxProcessor<'c>
val delay : a:'a -> fn:('a -> 'b -> unit) -> MailboxProcessor<'b>
val add :
  a:'a ->
    fn:('a -> Numerics.BigInteger -> unit) ->
      MailboxProcessor<Numerics.BigInteger>
val out : Lazy<MailboxProcessor<bigint>> = Value is not created.
val delta2int : Lazy<MailboxProcessor<bigint>> = Value is not created.
val prefixInt0 : Lazy<MailboxProcessor<bigint>> = Value is not created.
val prefixInt1 : Lazy<MailboxProcessor<bigint>> = Value is not created.
val pairsInt : Lazy<Agent<bigint>> = Value is not created.
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
2178309
3524578
5702887
9227465
14930352
24157817
39088169
63245986
102334155
165580141
267914296
433494437
701408733
1134903170
1836311903
2971215073
4807526976
7778742049
12586269025
20365011074
32951280099
53316291173
86267571272
139583862445
225851433717
365435296162
591286729879
956722026041
1548008755920
2504730781961
4052739537881
6557470319842
10610209857723
17167680177565
27777890035288
44945570212853
72723460248141
117669030460994
190392490709135
308061521170129
498454011879264
806515533049393
1304969544928657
...

Error output (only on Mono):

Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
  at Microsoft.FSharp.Control.AsyncBuilderImpl+bindA@783[Microsoft.FSharp.Core.Unit,Microsoft.FSharp.Core.Unit].Invoke (Microsoft.FSharp.Control.AsyncParams`1 args) [0x00000] in <filename unknown>:0
  at Microsoft.FSharp.Control.AsyncBuilderImpl+callA@805[Microsoft.FSharp.Core.Unit,Microsoft.FSharp.Core.Unit].Invoke (Microsoft.FSharp.Control.AsyncParams`1 args) [0x00000] in <filename unknown>:0
  at Microsoft.FSharp.Control.AsyncBuilderImpl+tryWithA@839[Microsoft.FSharp.Core.Unit].Invoke (Microsoft.FSharp.Control.AsyncParams`1 args) [0x00000] in <filename unknown>:0
  at Microsoft.FSharp.Control.AsyncBuilderImpl+callA@805[Microsoft.FSharp.Core.Unit,Microsoft.FSharp.Core.Unit].Invoke (Microsoft.FSharp.Control.AsyncParams`1 args) [0x00000] in <filename unknown>:0
  at Microsoft.FSharp.Control.AsyncBuilderImpl+queueAsync@727[Microsoft.FSharp.Core.Unit].Invoke (Microsoft.FSharp.Core.Unit unitVar0) [0x00000] in <filename unknown>:0
  at <StartupCode$FSharp-Core>.$Control.loop@426-40 (Microsoft.FSharp.Control.Trampoline this, Microsoft.FSharp.Core.FSharpFunc`2 action) [0x00000] in <filename unknown>:0
  at Microsoft.FSharp.Control.Trampoline.ExecuteAction (Microsoft.FSharp.Core.FSharpFunc`2 firstAction) [0x00000] in <filename unknown>:0
  at Microsoft.FSharp.Control.TrampolineHolder.Protect (Microsoft.FSharp.Core.FSharpFunc`2 firstAction) [0x00000] in <filename unknown>:0
  at <StartupCode$FSharp-Core>.$Control+-ctor@512-1.Invoke (System.Object state) [0x00000] in <filename unknown>:0
Mono JIT compiler version 4.0.0 ((detached/d136b79 Mon Apr 13 14:40:59 EDT 2015)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           normal
        SIGSEGV:       altstack
        Notification:  kqueue
        Architecture:  x86
        Disabled:      none
        Misc:          softdebug
        LLVM:          yes(3.6.0svn-mono-(detached/a173357)
        GC:            sgen

References:

Code Snippet:

 1 #load @"Utils/ListLazy.fs"
 2 
 3 open Stermon.Research.Utils
 4 
 5 let corruptStartPoint l =
 6   let rec loopStart = function
 7     | Cons(h1,t1),Cons(h2,t2) -> (h1 = h2) |> function
 8       | true -> Some h1
 9       | false ->
10         loopStart (t1.Force(),t2.Force())
11     | _,_ -> None
12   let rec meetPoint = function
13     | Cons(h1,t1),Cons(h2,t2) -> (h1 = h2) |> function
14       | true -> loopStart (l,t2.Force())
15       | false ->
16         meetPoint (t1.Force(),(t2.Force() |> List.Lazy.skip 1I))
17     | _,_ -> None
18   meetPoint (l,(l |> List.Lazy.skip 1I))

Code output:

[Loading Utils/ListLazy.fs]

namespace FSI_0002.Stermon.Research.Utils
  type 'a ListLazy =
    | Cons of 'a * Lazy<'a ListLazy>
    | Nil
  module Lazy = begin
    val single : h:'a -> 'a ListLazy
    val cons : h:'a -> l:'a ListLazy -> 'a ListLazy
    val head : _arg1:'a ListLazy -> 'a
    val tail : _arg1:'a ListLazy -> 'a ListLazy
    val iter : f:('a -> unit) -> _arg1:'a ListLazy -> unit
    val map : f:('a -> 'b) -> _arg1:'a ListLazy -> 'b ListLazy
    val fold : f:('a -> 'b -> 'a) -> init:'a -> _arg1:'b ListLazy -> 'a
    val foldBack :
      f:('a -> Lazy<'b> -> 'b) -> init:'b -> _arg1:'a ListLazy -> 'b
    val unfold : f:('a -> ('b * 'a) option) -> init:'a -> 'b ListLazy
    val reduce : f:('a -> 'a -> 'a) -> _arg1:'a ListLazy -> 'a
    val reduceBack : f:('a -> Lazy<'a> -> 'a) -> _arg1:'a ListLazy -> 'a
    val skip :
      n:System.Numerics.BigInteger -> _arg1:'a ListLazy -> 'a ListLazy
    val take :
      n:System.Numerics.BigInteger -> _arg1:'a ListLazy -> 'a ListLazy
    val append : l1:'a ListLazy -> l2:'a ListLazy -> 'a ListLazy
    val concat : _arg1:'a ListLazy ListLazy -> 'a ListLazy
    val ofList : _arg1:'a list -> 'a ListLazy
    val toList : l:'a ListLazy -> 'a list
  end

val corruptStartPoint :
  l:'a Stermon.Research.Utils.ListLazy -> 'a option when 'a : equality

Non corrupt linked list

All

1 let example1 = List.Lazy.unfold(fun s -> Some(s,s+1)) 0 |> List.Lazy.take 26I
2 
3 example1
4 |> List.Lazy.iter(printf "%i ")
5 
6 corruptStartPoint example1
> val example1 : int ListLazy = Cons (0,Value is not created.)
> 0 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 val it : unit = ()
> val it : int option = None

Corrupt linked list

All

 1 let corruptGenerator n =
 2   List.Lazy.unfold(
 3     fun s -> (s < n) |> function
 4     | false -> Some((s % n) + n,s+1)
 5     | true  -> Some(s,s+1)) 0
 6 
 7 let example2 = corruptGenerator 7
 8 
 9 example2
10 |> List.Lazy.take 28I
11 |> List.Lazy.iter(printf "%i ")
12 
13 corruptStartPoint example2
> val corruptGenerator : n:int -> int ListLazy
> val example2 : int ListLazy = Cons (0,Value is not created.)
> 0 1 2 3 4 5 6 7 8 9 10 11 12 13 7 8 9 10 11 12 13 7 8 9 10 11 12 13 val it : unit = ()
> val it : int option = Some 7

References:

All

Code Snippet:

 1 type 'a ListLazy = Cons of 'a * 'a ListLazy Lazy | Nil
 2 
 3 module List =
 4   module Lazy =
 5     let single h = Cons(h, lazy (Nil))
 6     let cons h l = Cons(h, lazy (l))
 7     let head = function | Nil -> failwith "empty list" | Cons(h,_) -> h
 8     let tail = function | Nil -> failwith "empty list" | Cons(_,t) -> t.Force()
 9     let rec iter f = function
10       | Nil -> () | Cons(h,t) -> f h; iter f (t.Force())
11     let rec map f = function
12       | Nil -> Nil | Cons(h,t) -> Cons(f h, lazy (map f (t.Force())))
13     let rec fold f init = function
14       | Nil -> init | Cons(h,t) -> fold f (f init h) (t.Force())
15     let rec foldBack f init = function
16       | Nil -> init | Cons(h,t) -> f h (lazy (foldBack f init (t.Force())))
17     let rec unfold f init = f init |> function
18       | None -> Nil | Some(a,s) -> Cons (a, lazy (unfold f s))
19     let rec reduce f = function
20       | Nil -> failwith "empty list" | Cons(h,t) -> fold f h (t.Force())
21     let rec reduceBack f = function
22       | Nil -> failwith "empty list" | Cons(h,t) -> foldBack f h (t.Force())
23     let rec skip n = function
24       | Nil -> Nil | Cons(h,t) -> (n = 0I) |> function
25         | true  -> cons h (t.Force())
26         | false -> skip (n-1I) (t.Force())
27     let rec take n = function
28       | Nil -> Nil | Cons(h,t) -> (n = 0I) |> function
29         | true  -> Nil
30         | false -> Cons(h, lazy (take (n-1I) (t.Force())))
31     let rec append l1 l2 = l1 |> function
32       | Nil -> l2 | Cons(h,t) -> Cons(h, lazy (append (t.Force()) l2))
33     let rec concat = function
34       | Nil -> Nil | Cons(h,t) -> append h (concat (t.Force()))
35     let rec ofList = function
36       | [] -> Nil | h :: t -> cons h (ofList t)
37     let toList l =
38       let rec toList' acc = function
39         | Nil -> List.rev acc
40         | Cons(h,t) -> toList' (h::acc) (t.Force())
41       toList' [] l

Code output:

> 
type 'a ListLazy =
  | Cons of 'a * Lazy<'a ListLazy>
  | Nil
module List = begin
  module Lazy = begin
    val single : h:'a -> 'a ListLazy
    val cons : h:'a -> l:'a ListLazy -> 'a ListLazy
    val head : _arg1:'a ListLazy -> 'a
    val tail : _arg1:'a ListLazy -> 'a ListLazy
    val iter : f:('a -> unit) -> _arg1:'a ListLazy -> unit
    val map : f:('a -> 'b) -> _arg1:'a ListLazy -> 'b ListLazy
    val fold : f:('a -> 'b -> 'a) -> init:'a -> _arg1:'b ListLazy -> 'a
    val foldBack :
      f:('a -> Lazy<'b> -> 'b) -> init:'b -> _arg1:'a ListLazy -> 'b
    val unfold : f:('a -> ('b * 'a) option) -> init:'a -> 'b ListLazy
    val reduce : f:('a -> 'a -> 'a) -> _arg1:'a ListLazy -> 'a
    val reduceBack : f:('a -> Lazy<'a> -> 'a) -> _arg1:'a ListLazy -> 'a
    val skip :
      n:System.Numerics.BigInteger -> _arg1:'a ListLazy -> 'a ListLazy
    val take :
      n:System.Numerics.BigInteger -> _arg1:'a ListLazy -> 'a ListLazy
    val append : l1:'a ListLazy -> l2:'a ListLazy -> 'a ListLazy
    val concat : _arg1:'a ListLazy ListLazy -> 'a ListLazy
    val ofList : _arg1:'a list -> 'a ListLazy
    val toList : l:'a ListLazy -> 'a list
  end
end

Infinite Fibonacci (and squared) sequence

 1 let fib =
 2   (List.Lazy.single 0I,
 3    List.Lazy.unfold(fun (a1,a2) -> Some(a1+a2,(a2,a1+a2))) (1I,0I))
 4   ||> List.Lazy.append
 5 
 6 let fibSquared =
 7   fib |> List.Lazy.foldBack(fun x l -> Cons(x*x,l)) Nil
 8 
 9 fib
10 |> List.Lazy.take 10I
11 |> List.Lazy.iter(printf "%A ")
12 
13 fibSquared
14 |> List.Lazy.take 10I
15 |> List.Lazy.iter(printf "%A ")
> 
val fib : System.Numerics.BigInteger ListLazy = Cons (0,Value is not created.)
val fibSquared : System.Numerics.BigInteger ListLazy =
  Cons (0,Value is not created.)

> 0 1 1 2 3 5 8 13 21 34 val it : unit = ()
> 0 1 1 4 9 25 64 169 441 1156 val it : unit = ()

Runtime errors when skipping/taking on empty sequences (F# Seq)

1 fib
2 |> List.Lazy.take 10I
3 |> List.Lazy.toList
4 |> List.toSeq
5 |> Seq.skip 20 
6 |> Seq.iter(printf "%A ")
> val it : seq<System.Numerics.BigInteger> =
  Error: The input sequence has an insufficient number of elements.
1 fib
2 |> List.Lazy.take 10I
3 |> List.Lazy.toList
4 |> List.toSeq
5 |> Seq.take 20
6 |> Seq.iter(printf "%A ")
> 0 1 1 2 3 5 8 13 21 34 System.InvalidOperationException: The input sequence has an insufficient number of elements.
  at Microsoft.FSharp.Collections.SeqModule+Take@999[System.Numerics.BigInteger].GenerateNext (IEnumerable`1& next) [0x00000] in <filename unknown>:0 
  at Microsoft.FSharp.Core.CompilerServices.GeneratedSequenceBase`1[System.Numerics.BigInteger].MoveNextImpl () [0x00000] in <filename unknown>:0 
  at Microsoft.FSharp.Core.CompilerServices.GeneratedSequenceBase`1[System.Numerics.BigInteger].System-Collections-IEnumerator-MoveNext () [0x00000] in <filename unknown>:0 
  at Microsoft.FSharp.Collections.SeqModule.Iterate[BigInteger] (Microsoft.FSharp.Core.FSharpFunc`2 action, IEnumerable`1 source) [0x00000] in <filename unknown>:0 
  at <StartupCode$FSI_0047>.$FSI_0047.main@ () [0x00000] in <filename unknown>:0 
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
Stopped due to error

No runtime errors when skipping/taking on empty lists

1 fib
2 |> List.Lazy.take 10I
3 |> List.Lazy.skip 20I // Behave as C# Linq.Enumerable.Skip
4 |> List.Lazy.iter(printf "%A ")
5 
6 fib
7 |> List.Lazy.take 10I
8 |> List.Lazy.take 20I // Behave as C# Linq.Enumerable.Take (or F# Seq.truncate)
9 |> List.Lazy.iter(printf "%A ")
> val it : unit = ()
> 0 1 1 2 3 5 8 13 21 34 val it : unit = ()

References: