···11+MIT License
22+33+Copyright (c) 2025 Sona Tau Estrada Rivera
44+55+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66+77+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
88+99+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+92
Main.hs
···11+newtype Cont r a = Cont ((a -> r) -> r)
22+33+instance Functor (Cont r) where
44+ fmap :: (a -> b) -> Cont r a -> Cont r b
55+ fmap f (Cont c) = Cont $ \k -> c $ k . f
66+77+instance Applicative (Cont r) where
88+ pure :: a -> Cont r a
99+ pure a = Cont $ \k -> k a
1010+1111+ (<*>) :: Cont r (a -> b) -> Cont r a -> Cont r b
1212+ Cont f <*> Cont v = Cont $ \k -> f (\f' -> v $ k . f')
1313+1414+instance Monad (Cont r) where
1515+ (>>=) :: Cont r a -> (a -> Cont r b) -> Cont r b
1616+ Cont x >>= f = Cont $ \k -> x (\a -> let Cont c = f a in c k)
1717+1818+runCont :: Cont r a -> (a -> r) -> r
1919+runCont (Cont c) = c
2020+2121+evalCont :: Cont a a -> a
2222+evalCont (Cont c) = c id
2323+2424+callCC :: ((a -> Cont r b) -> Cont r a) -> Cont r a
2525+callCC f = Cont $ \k -> runCont (f (\x -> Cont $ \_ -> k x)) k
2626+2727+foldrCPS :: (a -> b -> Cont r b) -> b -> [a] -> Cont r b
2828+foldrCPS f z list = case list of
2929+ [] -> pure z
3030+ (x:xs) -> foldrCPS f z xs >>= f x
3131+3232+addCPS :: Num a => a -> a -> Cont r a
3333+addCPS a b = pure (a + b)
3434+3535+sumCPS :: [Int] -> Int
3636+sumCPS = evalCont . foldrCPS addCPS 0
3737+3838+data Trampoline a = More (() -> Trampoline a) | Done a
3939+4040+runTrampoline :: Trampoline a -> a
4141+runTrampoline t = case t of
4242+ Done a -> a
4343+ More k -> runTrampoline $ k ()
4444+4545+instance Functor Trampoline where
4646+ fmap :: (a -> b) -> Trampoline a -> Trampoline b
4747+ fmap f t = case t of
4848+ Done a -> pure $ f a
4949+ More k -> More . const $ f <$> k ()
5050+5151+instance Applicative Trampoline where
5252+ pure :: a -> Trampoline a
5353+ pure = Done
5454+5555+ (<*>) :: Trampoline (a -> b) -> Trampoline a -> Trampoline b
5656+ l <*> r = case (l, r) of
5757+ (Done f, Done x) -> pure $ f x
5858+ (More k, Done x) -> More . const $ k () <*> pure x
5959+ (Done f, More c) -> More . const $ f <$> c ()
6060+ (More k, More c) -> More . const $ k () <*> c ()
6161+6262+instance Monad Trampoline where
6363+ (>>=) :: Trampoline a -> (a -> Trampoline b) -> Trampoline b
6464+ l >>= f = case l of
6565+ Done a -> f a
6666+ More k -> More . const $ k () >>= f
6767+6868+foldrT :: (a -> b -> Trampoline b) -> b -> [a] -> Trampoline b
6969+foldrT f z list = case list of
7070+ [] -> pure z
7171+ (x:xs) -> More . const $ foldrT f z xs >>= f x
7272+7373+addT :: Int -> Int -> Trampoline Int
7474+addT a b = pure (a + b)
7575+7676+sumT :: [Int] -> Int
7777+sumT = runTrampoline . foldrT addT 0
7878+7979+fibT :: Int -> Trampoline Int
8080+fibT n = case n of
8181+ 0 -> pure 0
8282+ 1 -> pure 1
8383+ n -> More . const $ (+) <$> fibT (n - 2) <*> fibT (n - 1)
8484+8585+fib :: Int -> Int
8686+fib n = case n of
8787+ 0 -> 0
8888+ 1 -> 1
8989+ n -> fib(n - 1) + fib(n - 2)
9090+9191+main = do
9292+ print $ fib 48
+3
README.md
···11+# Haskell CPS & Trampolines
22+33+This is a very simple implementation of trampolines and CPS in Haskell.