Home In the following computation expression, for/while do not loop
Reply: 0

In the following computation expression, for/while do not loop

user2517
1#
user2517 Published in April 26, 2018, 5:22 pm

I have the following computation expression. The problem is that For/While do not loop.

type Internal<'T> = Internal of option<'T>
type Maybe<'T>    = Maybe    of Lazy< Internal<'T> >
type Delayed<'T>  = Delayed  of (unit -> Internal<'T>)

let run (Maybe f) = f.Force()

type MaybeBuilder() = 
    member this.Bind(m, f) = m |> bind f
    member this.Return(x) = Internal (Some x)
    member this.ReturnFrom(Maybe v) = v.Force()
    member this.Zero() = Internal None
    member this.Combine(a, g) = a |> combine g
    member this.Delay(f) = f |> delay 
    member this.Run(f) = f |> run 
    member this.While(guard, Delayed body) =
        if not ( guard() ) then this.Zero()
        else ( body() ) |> bind ( fun () -> this.While(guard, Delayed body) ) 
    member this.TryWith(Delayed body, handler) =
        try this.ReturnFrom( Maybe ( lazy body() ) )
        with e -> e |> handler 
    member this.TryFinally(Delayed body, compensation) = 
        try this.ReturnFrom( Maybe ( lazy body() ) )
        finally compensation()
    member this.Using(disposable : #System.IDisposable, body) =
        let body' = fun () -> disposable |> body 
        this.TryFinally( Delayed body', fun () ->
            match disposable with 
            | null -> ()
            | disp -> disp.Dispose() )
    member this.For(sequence : #seq<_>, body) =
        let sequence' = sequence |> seq 
        this.Using( sequence'.GetEnumerator(), fun enum -> 
            this.While( enum.MoveNext, this.Delay( fun () -> enum.Current |> body ) ) )

I am not certain whether this is a "Combine" issue or what and I do appreciate the help. The Helper functions are defined inside the MaybeBuilder type:

let bind (f : 'T -> Internal<'U>) (Internal m) =
    match m with
    | None -> Internal None
    | Some x -> x |> f
let combine (Delayed g) (Internal m) =
    match m with 
    | Some _ -> Internal m
    | None   -> g()
let delay f = Delayed f    
let run (Delayed f) =
    let (Internal runResult) = f()
    Maybe (lazy Internal runResult)

As an example, the following code produces "j is 1" and that is it.

let maybe = new MaybeBuilder()    

let m7 : Maybe<int> =
    maybe {
        let sequence = seq [ 1 .. 13 ]
        for j in sequence do 
            printfn "j is %i" j  
    }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.307919 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO