Skip to content

Instantly share code, notes, and snippets.

@palladin
Created September 24, 2015 15:22
Show Gist options
  • Save palladin/1fd6d7669e5e6c8b5964 to your computer and use it in GitHub Desktop.
Save palladin/1fd6d7669e5e6c8b5964 to your computer and use it in GitHub Desktop.
MonadFix in F#
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
@palladin
Copy link
Author

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

@palladin
Copy link
Author

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