computation-expression

Extended computation expressions without for..in..do

人走茶凉 提交于 2019-12-02 16:41:48
What I mean by extended computation expressions is computation expressions with custom keywords defined via CustomOperation attribute. When reading about extended computation expressions , I come across very cool IL DSL by @kvb: let il = ILBuilder() // will return 42 when called // val fortyTwoFn : (unit -> int) let fortyTwoFn = il { ldc_i4 6 ldc_i4_0 ldc_i4 7 add mul ret } I wonder how the operations compose without using for..in..do construct. My gut feeling is that it starts with x.Zero member, but I haven't found any reference to verify that. If the example above is too technical, here is

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

三世轮回 提交于 2019-12-02 15:35:30
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 it extensible? For example, can I say something like: FooMonadBuilder() { bar } and somehow tell the F

Recursive functions in computation expressions

回眸只為那壹抹淺笑 提交于 2019-12-01 06:02:00
Some background first. I am currently learning some stuff about monadic parser combinators. While I tried to transfer the 'chainl1' function from this paper (p. 16-17), I came up with this solution: let chainl1 p op = parser { let! x = p let rec chainl1' (acc : 'a) : Parser<'a> = let p' = parser { let! f = op let! y = p return! chainl1' (f acc y) } p' <|> succeed acc return! chainl1' x } I tested the function with some large input and got a StackOverflowException. Now I am wondering, is it posible to rewrite a recursive function, that uses some computation expression, in a way so it is using

StackOverflow in continuation monad

独自空忆成欢 提交于 2019-11-30 07:07:20
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 | [] -> return [] | x :: xs -> let! v = fun g -> g(f x) let! xs = map xs return v :: xs } map xs id;;

Why do F# computation expressions require a builder object (rather than a class)?

耗尽温柔 提交于 2019-11-30 04:51:28
F# computation expressions have the syntax: ident { cexpr } Where ident is the builder object (this syntax is taken from Don Syme's 2007 blog entry ). In all the examples I've seen, builder objects are singleton instances, and stateless to boot. Don gives the example of defining a builder object called attempt : let attempt = new AttemptBuilder() My question: Why doesn't F# just use the AttemptBuilder class directly in computation expressions? Surely the notation could be de-sugared to static method calls just as easily as instance method calls. Using an instance value means that one could in

How do you compose query expressions in F#?

二次信任 提交于 2019-11-29 19:59:55
I've been looking at query expressions here http://msdn.microsoft.com/en-us/library/vstudio/hh225374.aspx And I've been wondering why the following is legitimate let testQuery = query { for number in netflix.Titles do where (number.Name.Contains("Test")) } But you can't really do something like this let christmasPredicate = fun (x:Catalog.ServiceTypes.Title) -> x.Name.Contains("Christmas") let testQuery = query { for number in netflix.Titles do where christmasPredicate } Surely F# allows composability like this so you can reuse a predicate?? What if I wanted Christmas titles combined with

What is the role of `while`-loops in computation expressions in F#?

半城伤御伤魂 提交于 2019-11-29 19:55:01
问题 If you define a While method of the builder-object, you can use while -loops in your computation expressions. The signature of the While method is: member b.While (predicate:unit->bool, body:M<'a>) : M<'a> For comparison, the signature of the For method is: member b.For (items:seq<'a>, body:unit->M<'a>) : M<'a> You should notice that, in the While -method, the body is a simple type, and not a function as in the For method. You can embed some other statements, like let and function-calls

Why do F# computation expressions require a builder object (rather than a class)?

▼魔方 西西 提交于 2019-11-29 02:23:40
问题 F# computation expressions have the syntax: ident { cexpr } Where ident is the builder object (this syntax is taken from Don Syme's 2007 blog entry). In all the examples I've seen, builder objects are singleton instances, and stateless to boot. Don gives the example of defining a builder object called attempt : let attempt = new AttemptBuilder() My question: Why doesn't F# just use the AttemptBuilder class directly in computation expressions? Surely the notation could be de-sugared to static

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

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 01:24:59
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# version return cont(x); } finally { finallyAction(x); } } } The best (but non-compiling code) I've come

How do you compose query expressions in F#?

纵饮孤独 提交于 2019-11-27 09:37:43
问题 I've been looking at query expressions here http://msdn.microsoft.com/en-us/library/vstudio/hh225374.aspx And I've been wondering why the following is legitimate let testQuery = query { for number in netflix.Titles do where (number.Name.Contains("Test")) } But you can't really do something like this let christmasPredicate = fun (x:Catalog.ServiceTypes.Title) -> x.Name.Contains("Christmas") let testQuery = query { for number in netflix.Titles do where christmasPredicate } Surely F# allows