computation-expression

StackOverflow in continuation monad

筅森魡賤 提交于 2020-01-10 10:42:10
问题 Using the following continuation monad: type ContinuationMonad() = member this.Bind (m, f) = fun c -> m (fun a -> f a c) member this.Return x = fun k -> k x let cont = ContinuationMonad() I fail to see why the following gives me a stack overflow: let map f xs = let rec map xs = cont { match xs with | [] -> return [] | x :: xs -> let! xs = map xs return f x :: xs } map xs id;; let q = [1..100000] |> map ((+) 1) While the following doesn't: let map f xs = let rec map xs = cont { match xs with |

StackOverflow in continuation monad

最后都变了- 提交于 2020-01-10 10:41:27
问题 Using the following continuation monad: type ContinuationMonad() = member this.Bind (m, f) = fun c -> m (fun a -> f a c) member this.Return x = fun k -> k x let cont = ContinuationMonad() I fail to see why the following gives me a stack overflow: let map f xs = let rec map xs = cont { match xs with | [] -> return [] | x :: xs -> let! xs = map xs return f x :: xs } map xs id;; let q = [1..100000] |> map ((+) 1) While the following doesn't: let map f xs = let rec map xs = cont { match xs with |

StackOverflow in continuation monad

荒凉一梦 提交于 2020-01-10 10:41:23
问题 Using the following continuation monad: type ContinuationMonad() = member this.Bind (m, f) = fun c -> m (fun a -> f a c) member this.Return x = fun k -> k x let cont = ContinuationMonad() I fail to see why the following gives me a stack overflow: let map f xs = let rec map xs = cont { match xs with | [] -> return [] | x :: xs -> let! xs = map xs return f x :: xs } map xs id;; let q = [1..100000] |> map ((+) 1) While the following doesn't: let map f xs = let rec map xs = cont { match xs with |

F#: Is there a way to extend the monad keyword list?

北城余情 提交于 2019-12-31 08:11:40
问题 Inside an F# monad, if you say let! , the compiler translates that to a Bind member that you've defined on the monad builder. Now I see there are Query monads, as shown here on MSDN, where you can say: query { for student in db.Student do select student count } and the select and count , for example, will be translated to the QueryBuilder members Linq.QueryBuilder.Select and Linq.QueryBuilder.Count. My question is, is this mapping of keywords to members hardwired into the F# compiler, or is

F# computation expression for nested Boolean tests?

为君一笑 提交于 2019-12-23 19:12:59
问题 I think I've got enough understanding of F# monads (workflows) that I see a few places in my code where implementing them makes sense. For example, I've got a function with multiple nested if/thens, i.e. the function should continue only so long as the data pass certain "tests" along the way. I'm familiar with the "maybe" monad, but in all the examples that I've seen, it's coded to operate on let! bindings, which I'm not doing. I'm hoping that someone can provide me with an example of the

Wrangling TryWith in Computation expressions

别说谁变了你拦得住时间么 提交于 2019-12-21 05:38:12
问题 (Having failed to 'grok' FParsec, I followed the advice I read somewhere and started trying to write a little parser myself. Somehow I spotted what looked like a chance to try and monadify it, and now I have N problems...) This is my 'Result' type (simplified) type Result<'a> = | Success of 'a | Failure of string Here's the computation expression builder type ResultBuilder() = member m.Return a = Success(a) member m.Bind(r,fn) = match r with | Success(a) -> fn a | Failure(m) -> Failure(m) In

Why does this computation expression builder expect “unit” in my for loop?

佐手、 提交于 2019-12-21 05:19:04
问题 This is a follow-up question to this question. I'm trying to create a computation expression builder that accumulates a value through custom operations, and also supports standard F# language constructs at the same time. For the purposes of having a simple example to talk about, I'm using a computation expression that builds F# lists. Thanks to suggestions from kvb and Daniel I'm further along, but still having trouble with for loops. The builder: type Items<'a> = Items of 'a list type

How do I translate a `where T : U` generic type parameter constraint from C# to F#?

陌路散爱 提交于 2019-12-18 03:09:16
问题 F# is giving me some trouble with its type inference rules. I'm writing a simple computation builder but can't get my generic type variable constraints right. The code that I would want looks as follows in C# : class FinallyBuilder<TZ> { readonly Action<TZ> finallyAction; public FinallyBuilder(Action<TZ> finallyAction) { this.finallyAction = finallyAction; } public TB Bind<TA, TB>(TA x, Func<TA, TB> cont) where TA : TZ { // ^^^^^^^^^^^^^ try // this is what gives me a headache { // in the F#

Retry Computation expression or other construct in F#

会有一股神秘感。 提交于 2019-12-10 19:36:06
问题 I want to be able to write a computation expression in F# that will be able to retry an operation if it throws an exception. Right now my code looks like: let x = retry (fun() -> GetResourceX()) let y = retry (fun() -> GetResourceY()) let z = retry (fun() -> DoThis(x, y)) etc. (this is obviously an astract representation of the actual code) I need to be able to retry each of the functions a set number of times, which I have defined elswhere. I was thinking a computation expression could help

(How) can I make this monadic bind tail-recursive?

会有一股神秘感。 提交于 2019-12-09 05:54:50
问题 I have this monad called Desync - [<AutoOpen>] module DesyncModule = /// The Desync monad. Allows the user to define in a sequential style an operation that spans /// across a bounded number of events. Span is bounded because I've yet to figure out how to /// make Desync implementation tail-recursive (see note about unbounded recursion in bind). And /// frankly, I'm not sure if there is a tail-recursive implementation of it... type [<NoComparison; NoEquality>] Desync<'e, 's, 'a> = Desync of (