{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# HLINT ignore "Use first" #-}
{-# HLINT ignore "Use second" #-}
module Main (main) where
import Control.Monad (when)
import Data.Bifunctor (bimap)
import GHC.Generics (Generic1 (..), Rep1)
import Gongju.Dissect
import System.Exit (exitFailure)
-- 1. Custom expression functor
data ExprF a
= Lit !Int
| Add a a
| Mul a a
| Neg a
deriving (Eq, Show, Functor, Generic1)
instance Dissect ExprF where
type D ExprF = D (Rep1 ExprF)
right = genericRight
-- Naive recursive evaluation of ExprF (Fix ExprF)
evalNaive :: Fix ExprF -> Int
evalNaive (Fix (Lit n)) = n
evalNaive (Fix (Add a b)) = evalNaive a + evalNaive b
evalNaive (Fix (Mul a b)) = evalNaive a * evalNaive b
evalNaive (Fix (Neg a)) = negate (evalNaive a)
-- Algebra for cataTail
evalAlg :: ExprF Int -> Int
evalAlg (Lit n) = n
evalAlg (Add a b) = a + b
evalAlg (Mul a b) = a * b
evalAlg (Neg a) = negate a
-- Build a skewed tree of depth n: Neg (Neg (Neg ... (Lit 42)))
mkSkewedTree :: Int -> Fix ExprF
mkSkewedTree 0 = Fix (Lit 42)
mkSkewedTree n = Fix (Neg (mkSkewedTree (n - 1)))
-- Build a balanced tree of depth n
mkBalancedTree :: Int -> Fix ExprF
mkBalancedTree 0 = Fix (Lit 1)
mkBalancedTree n =
let sub = mkBalancedTree (n - 1)
in Fix (Add sub sub)
main :: IO ()
main = do
putStrLn "=== Running gongju-dissect test suite ==="
-- 1. Test tmap == fmap
let expr1 = Add (10 :: Int) (20 :: Int)
tmapped = tmap (+1) expr1
fmapped = fmap (+1) expr1
when (tmapped /= fmapped) $ do
putStrLn "FAIL: tmap /= fmap on Add"
exitFailure
putStrLn "PASS: tmap == fmap on expression functor"
-- 2. Test cataTail == evalNaive on balanced tree
let balanced = mkBalancedTree 12
resNaive = evalNaive balanced
resCata = cataTail evalAlg balanced
when (resNaive /= resCata || resCata /= 4096) $ do
putStrLn "FAIL: cataTail /= evalNaive on balanced tree"
exitFailure
putStrLn "PASS: cataTail == evalNaive on balanced tree"
-- 3. Test cataTail on depth 25,000 skewed tree (O(1) host stack!)
let depth = 25000
skewed = mkSkewedTree depth
resSkewed = cataTail evalAlg skewed
expectedSkewed = if even depth then 42 else -42
when (resSkewed /= expectedSkewed) $ do
putStrLn "FAIL: cataTail on depth 25,000 skewed tree"
exitFailure
putStrLn "PASS: cataTail evaluated depth 25,000 skewed tree in O(1) host stack"
-- 4. Test cataTailWith using explicit DissectDict
let customDict = dictFromDissect
resDict = cataTailWith customDict evalAlg skewed
when (resDict /= expectedSkewed) $ do
putStrLn "FAIL: cataTailWith with explicit DissectDict"
exitFailure
putStrLn "PASS: cataTailWith with explicit DissectDict"
-- 5. Test Bifunctor laws on building blocks
let c = Clown [1, 2, 3] :: Clown [] Int String
j = Joker ["hello", "world"] :: Joker [] Int String
cMapped = bimap (+1) id c
jMapped = bimap id reverse j
when (runClown cMapped /= [2, 3, 4]) $ do
putStrLn "FAIL: Clown bimap"
exitFailure
when (runJoker jMapped /= ["olleh", "dlrow"]) $ do
putStrLn "FAIL: Joker bimap"
exitFailure
putStrLn "PASS: Bifunctor instances on Clown/Joker"
putStrLn "=== All gongju-dissect tests passed successfully ==="