0 people like it.

Stack Calc Monad

 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: 
open System

type Calc = Calc of int list

type CalcBuilder() =
    member this.Bind(x, f) = (fun c0 -> let a, Calc c = x c0 in f a c)
    member this.Delay(f) = (fun c0 -> f c0)
    member this.Return(x) = (fun c0 -> x, c0)
    member this.ReturnFrom(x) = x
    member this.Combine(x, f) = this.Bind(x, f)

let calc = new CalcBuilder()

let popCalc = (fun stack -> (List.head stack, Calc <| List.tail stack))
let pushCalc n = (fun stack -> ((), Calc <| n :: stack))
let addCalc = (fun stack -> match stack with
                            | []          -> ((),Calc [])
                            | [a]         -> ((),Calc [a])
                            | a :: b :: c -> ((),Calc ((a + b) :: c)))

let initialCalc = (), []

let func = calc {
    do! pushCalc 3
    do! pushCalc 4
    do! addCalc
    return! popCalc
}
namespace System
Multiple items
union case Calc.Calc: int list -> Calc

--------------------
type Calc = | Calc of int list

Full name: Script.Calc
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
Multiple items
type CalcBuilder =
  new : unit -> CalcBuilder
  member Bind : x:('i -> 'j * Calc) * f:('j -> int list -> 'k) -> ('i -> 'k)
  member Combine : x:('a -> 'b * Calc) * f:('b -> int list -> 'c) -> ('a -> 'c)
  member Delay : f:('g -> 'h) -> ('g -> 'h)
  member Return : x:'e -> ('f -> 'e * 'f)
  member ReturnFrom : x:'d -> 'd

Full name: Script.CalcBuilder

--------------------
new : unit -> CalcBuilder
val this : CalcBuilder
member CalcBuilder.Bind : x:('i -> 'j * Calc) * f:('j -> int list -> 'k) -> ('i -> 'k)

Full name: Script.CalcBuilder.Bind
val x : ('i -> 'j * Calc)
val f : ('j -> int list -> 'k)
val c0 : 'i
val a : 'j
val c : int list
member CalcBuilder.Delay : f:('g -> 'h) -> ('g -> 'h)

Full name: Script.CalcBuilder.Delay
val f : ('g -> 'h)
val c0 : 'g
member CalcBuilder.Return : x:'e -> ('f -> 'e * 'f)

Full name: Script.CalcBuilder.Return
val x : 'e
val c0 : 'f
member CalcBuilder.ReturnFrom : x:'d -> 'd

Full name: Script.CalcBuilder.ReturnFrom
val x : 'd
member CalcBuilder.Combine : x:('a -> 'b * Calc) * f:('b -> int list -> 'c) -> ('a -> 'c)

Full name: Script.CalcBuilder.Combine
val x : ('a -> 'b * Calc)
val f : ('b -> int list -> 'c)
member CalcBuilder.Bind : x:('i -> 'j * Calc) * f:('j -> int list -> 'k) -> ('i -> 'k)
val calc : CalcBuilder

Full name: Script.calc
val popCalc : stack:int list -> int * Calc

Full name: Script.popCalc
val stack : int list
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val head : list:'T list -> 'T

Full name: Microsoft.FSharp.Collections.List.head
val tail : list:'T list -> 'T list

Full name: Microsoft.FSharp.Collections.List.tail
val pushCalc : n:int -> stack:int list -> unit * Calc

Full name: Script.pushCalc
val n : int
val addCalc : stack:int list -> unit * Calc

Full name: Script.addCalc
val a : int
val b : int
val initialCalc : unit * 'a list

Full name: Script.initialCalc
val func : (obj -> int list -> int * Calc)

Full name: Script.func
Raw view Test code New version

More information

Link:http://fssnip.net/8d
Posted:14 years ago
Author:
Tags: