Skip to content

Instantly share code, notes, and snippets.

@yzyzsun
Last active November 8, 2021 06:27
Show Gist options
  • Save yzyzsun/3d1fe11aedfdcc2aea4b5ddfbecc94c9 to your computer and use it in GitHub Desktop.
Save yzyzsun/3d1fe11aedfdcc2aea4b5ddfbecc94c9 to your computer and use it in GitHub Desktop.
Fixed-point combinators using iso-recursive types
newtype Rec a = Fold { unfold :: Rec a -> a }
fix :: (a -> a) -> a
fix f = (\x -> f (unfold x x)) (Fold (\x -> f (unfold x x)))
newtype Rec a = Fold (Rec a -> a)
unfold :: forall a. Rec a -> Rec a -> a
unfold (Fold x) = x
fix :: forall a b. ((a -> b) -> a -> b) -> a -> b
fix f = (\x -> f (\v -> unfold x x v)) (Fold (\x -> f (\v -> unfold x x v)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment