1 {-# OPTIONS_GHC -O -ddump-rules #-}
3 -- Trac #2486
4 --
5 -- The thing to look for here is that specialisations for fib and tak
6 -- at both Int and Double are indeed generated; hence -ddump-rules
8 module Main where
10 import System.Environment
11 import Numeric
13 main = do
15 let m = n-1
16 a = 27 + fromIntegral n
17 putStr \$
18 line "Ack" [3,n] (ack 3 n) show ++
19 line "Fib" [a] (fib a :: Double) (\n -> showFFloat (Just 1) n []) ++
20 line "Tak" [3*m,2*m,m] (tak (3*m) (2*m) m :: Int) show ++
21 line "Fib" [3] (fib 3 :: Int) show ++
22 line "Tak" [3,2,1] (tak 3 2 1 :: Double) show
23 where
24 line pre a r f = pre ++ "(" ++ csv f a "" ++ "): " ++ f r ++ "\n"
25 csv f [a] s = s ++ f a
26 csv f (a:b) s = s ++ f a ++ "," ++ csv f b s
28 ack :: Int -> Int -> Int
29 ack 0 n = n+1
30 ack m 0 = ack (m-1) 1
31 ack m n = ack (m-1) (ack m (n-1))
33 fib :: (Num a, Ord a) => a -> a
34 fib n = if n >= 2 then fib (n-1) + fib (n-2) else 1
36 tak :: (Num a, Ord a) => a -> a -> a -> a
37 tak x y z = if y < x then tak (tak (x-1) y z) (tak (y-1) z x) (tak (z-1) x y) else z