Last active
June 26, 2019 18:05
-
-
Save DKurilo/0a5024aafae32a5ff8e5b3384d0a615c to your computer and use it in GitHub Desktop.
Fibonacci in compile time
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
{-# LANGUAGE FlexibleInstances, FunctionalDependencies, UndecidableInstances #-} | |
-- Esoteric programming with Haskell: | |
-- http://www.cse.chalmers.se/~hallgren/Papers/hallgren.pdf | |
-- https://repl.it/repls/AquaGainsboroSquares | |
module Main where | |
import System.IO | |
u = undefined | |
data Zero | |
data Succ n | |
class Eval n where eval :: n -> Int | |
instance Eval Zero where eval _ = 0 | |
instance Eval n => Eval (Succ n) where eval n = 1 + eval (predcs n) | |
class Add a b c | a b -> c where add :: a -> b -> c | |
instance Add Zero b b | |
instance Add a b c => Add (Succ a) b (Succ c) | |
class Fib a b | a -> b where | |
fib :: a -> b | |
instance Fib Zero Zero | |
instance Fib One One | |
instance (Fib a b, Fib (Succ a) c, Add b c d) => Fib (Succ (Succ a)) d | |
class Pred a b | a -> b where predcs :: a -> b | |
instance Pred (Succ n) n | |
type One = Succ Zero | |
type Three = Succ (Succ (Succ Zero)) | |
type Six = Succ (Succ (Succ (Succ (Succ (Succ Zero))))) | |
type Twelve = Succ (Succ (Succ (Succ (Succ (Succ Six))))) | |
type Thirteen = Succ Twelve | |
main :: IO () | |
main = print . eval . fib $ (u::Twelve) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment