This is an attempt to write a State Monad Transformer.
I haven't been able to make it generic and according to
Brian McNamara "this will never be possible in any .NET language"
http://cs.hubfs.net/forums/thread/13974.aspx
type StateT<'a, 'b, 'state> = StateT of ('state -> Parser<'a * 'state, 'b>)
type StateTBuilder (p:ParserMonad) =
// a -> m a
member this.Return a = StateT (fun s -> p.Return(a, s))
// m a -> (a -> m b) -> m b
member this.Bind (m, f) = StateT (fun s -> p { let! (v, s') = let (StateT f) = m in f s
let (StateT f') = f v
return! f' s' })
let statefulParser = StateTBuilder(parser)
let getState = StateT (fun s -> parser.Return(s, s))
let setState s = StateT (fun _ -> parser.Return((), s))
let lift c = StateT (fun s -> parser.Bind (c, (fun x -> parser.Return (x, s))))
let Execute m s = let (StateT f) = m in
parser { let! (x,_) = f s
return x }
No comments:
Post a Comment