Created
September 24, 2015 15:22
-
-
Save palladin/1fd6d7669e5e6c8b5964 to your computer and use it in GitHub Desktop.
MonadFix in F#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let force (value : Lazy<_>) = value.Force() | |
let fix : (Lazy<'T> -> 'T) -> Lazy<'T> = fun f -> | |
let rec x = lazy (f x) in x | |
// Maybe MonadFix | |
let mfix : (Lazy<'T> -> option<Lazy<'T>>) -> Lazy<option<Lazy<'T>>> = fun f -> | |
let rec x = lazy ( match force x with | Some v -> f v | None -> None ) in x |
Hmmm maybe you are correct...
I was trying to mimic the Haskell definition
mfix :: (α → Maybe α) → Maybe α
mfix f = let x = (f · unJust) x in x
where unJust (Just x ) = x
According to an old snippet of mine
http://www.fssnip.net/36
the trick is to push force inside the f
Maybe something like this ... ???
mfix :: (α → Maybe α) → Maybe α
mfix f = let x = (f · unJust) x in x
where unJust (Just x ) = x
let mfix : (Lazy<'T> -> option<'T>) -> Lazy<option<'T>> = fun f ->
let rec x = lazy ( f (lazy (Option.get (force x) )) ) in x
I think maybe option<'T> is not a good example for mfix and something more lazy like
let mfix : (Lazy<'T> -> seq<'T>) -> Lazy<seq<'T>>
is more easily demonstrable
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hmm... Looking at this more closely, isn't the body of
mfix
above equivalent toand undefined?