Skip to content

Instantly share code, notes, and snippets.

@habib-sadullaev
Last active September 10, 2023 09:18
Show Gist options
  • Save habib-sadullaev/32baa4beaed1c03936408e8fd23c6960 to your computer and use it in GitHub Desktop.
Save habib-sadullaev/32baa4beaed1c03936408e8fd23c6960 to your computer and use it in GitHub Desktop.
//fsharplint:disable
type M<'a> = 'a option
let inline unit (x: 'a) : M<'a> = Some x
let inline (>>=) (x: M<'a>) (f: 'a -> M<'b>) : M<'b> =
x |> Option.bind f
type MonadBuilder() =
member _.Bind(mx: M<'a>, f: 'a -> M<'b>) = mx >>= f
member _.Return(x: 'a) = unit x
member _.ReturnFrom(mx : M<'a>) = mx
let monad = MonadBuilder()
let leftIdentity1 a f : M<_> =
unit a >>= (fun x -> f x)
let leftIdentity2 a f : M<_> =
f a
let leftIdentity3 a f : M<_> =
monad {
let! a = unit a
return! f a }
let leftIdentity4 a f : M<_> =
monad { return! f a }
let rightIdentity1 ma : M<_> =
ma >>= (fun x -> unit x)
let rightIdentity2 ma : M<_> =
ma
let rightIdentity3 ma : M<_> =
monad { let! a = ma
return a }
let rightIdentity4 ma : M<_> =
monad { return! ma }
let association1 ma f g =
ma >>= (fun x -> f x >>= (fun y -> g y))
let association2 ma f g =
ma >>= (fun x -> f x) >>= (fun y -> g y)
let assosiation3 ma f g =
monad { let! a = ma
let! res =
a |> fun x ->
monad { let! y = f x
return! g y }
return res }
let assosiation4 ma f g =
monad { let! x = ma
let! y = f x
let! res = g y
return res }
@habib-sadullaev
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment