{-# LANGUAGE DataKinds #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE PolyKinds #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} {-# LANGUAGE UndecidableInstances #-} import Data.List (intercalate, minimumBy) import Data.List.NonEmpty (NonEmpty (..)) import qualified Data.List.NonEmpty as NonEmpty import Data.Ord (comparing) data Nat = Zero | Succ Nat deriving (Show) data Vec (n :: Nat) a where VNull :: Vec 'Zero a VCons :: a -> Vec n a -> Vec ('Succ n) a type family NAdd (x :: Nat) (y :: Nat) :: Nat where NAdd ('Succ x) y = 'Succ (NAdd x y) NAdd 'Zero y = y data Proxy a = Proxy cast01 :: Proxy (NAdd 'Zero x) -> Proxy x cast01 = id data EqualityProof (a :: k) (b :: k) where QED :: EqualityProof a a type a :~: b = EqualityProof a b data SNat (n :: Nat) where SZero :: SNat 'Zero SSucc :: SNat n -> SNat ('Succ n) lemma1 :: SNat n -> NAdd n 'Zero :~: n lemma1 SZero = QED lemma1 (SSucc n) = case lemma1 n of QED -> QED cast02 :: SNat x -> Proxy (NAdd x 'Zero) -> Proxy x cast02 snat = case lemma1 snat of QED -> id data Tree (k :: Nat) a where Tree :: a -> Children k a -> Tree k a data Children (k :: Nat) a where CZero :: Children 'Zero a CCons :: Tree k a -> Children k a -> Children ('Succ k) a singletonTree :: a -> Tree 'Zero a singletonTree x = Tree x CZero mergeTree :: Ord a => Tree k a -> Tree k a -> Tree ('Succ k) a mergeTree l@(Tree lroot lchildren) r@(Tree rroot rchildren) | lroot <= rroot = Tree lroot (CCons r lchildren) | otherwise = Tree rroot (CCons l rchildren) data Binary = B0 Binary | B1 Binary | BEnd deriving (Show) type family BInc (binary :: Binary) :: Binary where BInc 'BEnd = 'B1 'BEnd BInc ('B0 binary) = 'B1 binary BInc ('B1 binary) = 'B0 (BInc binary) type family BAdd (x :: Binary) (y :: Binary) :: Binary where BAdd 'BEnd y = y BAdd x 'BEnd = x BAdd ('B0 x) ('B0 y) = 'B0 (BAdd x y) BAdd ('B1 x) ('B0 y) = 'B1 (BAdd x y) BAdd ('B0 x) ('B1 y) = 'B1 (BAdd x y) BAdd ('B1 x) ('B1 y) = 'B0 (BInc (BAdd x y)) type BZero = 'B0 'BEnd type BOne = BInc BZero type BTwo = BInc BOne type BThree = BInc BTwo type BFour = BInc BThree type BFive = BInc BFour data SBin (b :: Binary) where SBEnd :: SBin 'BEnd SB0 :: SBin b -> SBin ('B0 b) SB1 :: SBin b -> SBin ('B1 b) data Forest (k :: Nat) (b :: Binary) a where FEnd :: Forest k 'BEnd a F0 :: Forest ('Succ k) b a -> Forest k ('B0 b) a F1 :: Tree k a -> Forest ('Succ k) b a -> Forest k ('B1 b) a emptyForest :: Forest k 'BEnd a emptyForest = FEnd insertTree :: Ord a => Tree k a -> Forest k b a -> Forest k (BInc b) a insertTree s FEnd = F1 s FEnd insertTree s (F0 f) = F1 s f insertTree s (F1 t f) = F0 (insertTree (mergeTree s t) f) mergeForests :: Ord a => Forest k lb a -> Forest k rb a -> Forest k (BAdd lb rb) a mergeForests FEnd rf = rf mergeForests lf FEnd = lf mergeForests (F0 lf) (F0 rf) = F0 (mergeForests lf rf) mergeForests (F1 l lf) (F0 rf) = F1 l (mergeForests lf rf) mergeForests (F0 lf) (F1 r rf) = F1 r (mergeForests lf rf) mergeForests (F1 l lf) (F1 r rf) = F0 (insertTree (mergeTree l r) (mergeForests lf rf)) newtype Heap (b :: Binary) a = Heap {unHeap :: Forest 'Zero b a} emptyHeap :: Heap 'BEnd a emptyHeap = Heap emptyForest pushHeap :: Ord a => a -> Heap b a -> Heap (BInc b) a pushHeap x (Heap forest) = Heap (insertTree (singletonTree x) forest) mergeHeap :: Ord a => Heap lb a -> Heap rb a -> Heap (BAdd lb rb) a mergeHeap (Heap lf) (Heap rf) = Heap (mergeForests lf rf) type family Ones (n :: Nat) :: Binary where Ones 'Zero = 'BEnd Ones ('Succ n) = 'B1 (Ones n) childrenToForest :: Children n a -> Forest 'Zero (Ones n) a childrenToForest children = childrenToForest_go SZero (childrenSingleton children) FEnd children childrenSingleton :: Children n a -> SNat n childrenSingleton CZero = SZero childrenSingleton (CCons _ c) = SSucc (childrenSingleton c) childrenToForest_go :: m ~ NAdd x n => SNat x -> SNat n -> Forest n (Ones x) a -> Children n a -> Forest 'Zero (Ones m) a childrenToForest_go xnat _snat@SZero acc CZero = case lemma1 xnat of QED -> acc childrenToForest_go xnat (SSucc nnat) acc (CCons tree children) = case lemma2 xnat nnat of QED -> childrenToForest_go (SSucc xnat) nnat (F1 tree acc) children lemma2 :: SNat n -> SNat m -> NAdd n ('Succ m) :~: 'Succ (NAdd n m) lemma2 SZero _ = QED lemma2 (SSucc n) m = case lemma2 n m of QED -> QED instance Functor (Vec n) where fmap _ VNull = VNull fmap f (VCons x v) = VCons (f x) (fmap f v) vecToList :: Vec n a -> [a] vecToList VNull = [] vecToList (VCons x v) = x : vecToList v vecToNonEmpty :: NNonZero n ~ 'True => Vec n a -> NonEmpty a vecToNonEmpty (VCons x v) = x :| vecToList v type family NNonZero (n :: Nat) :: Bool where NNonZero 'Zero = 'False NNonZero ('Succ _) = 'True type family BNonZero (b :: Binary) :: Bool where BNonZero 'BEnd = 'False BNonZero ('B1 b) = 'True BNonZero ('B0 b) = BNonZero b type family Popcount (b :: Binary) :: Nat where Popcount 'BEnd = 'Zero Popcount ('B1 b) = 'Succ (Popcount b) Popcount ('B0 b) = Popcount b lemma3 :: BNonZero b ~ 'True => SBin b -> NNonZero (Popcount b) :~: 'True lemma3 (SB1 _) = QED lemma3 (SB0 b) = case lemma3 b of QED -> QED type family Width (binary :: Binary) :: Nat where Width 'BEnd = 'Zero Width ('B0 binary) = 'Succ (Width binary) Width ('B1 binary) = 'Succ (Width binary) data CutTree (k :: Nat) (b :: Binary) a where CutTree :: Width (BAdd b (Ones x)) ~ Width (BInc (BAdd b (Ones x))) => SNat x -> SNat k -> Tree (NAdd k x) a -> Forest k b a -> CutTree k (BInc (BAdd b (Ones x))) a lumberjack_go :: forall k b a. SNat k -> Forest k b a -> Vec (Popcount b) (CutTree k b a) lumberjack_go _ FEnd = VNull lumberjack_go nnat0 (F0 forest0) = fmap (\cutTree -> case cutTree of CutTree xnat (SSucc nnat) t1 forest1 -> CutTree (SSucc xnat) nnat (case lemma2 nnat xnat of QED -> t1) (F0 forest1)) (lumberjack_go (SSucc nnat0) forest0) lumberjack_go nnat0 (F1 tree0 forest0) = VCons (CutTree SZero nnat0 (case lemma1 nnat0 of QED -> tree0) (F0 forest0)) (fmap (\cutTree -> case cutTree of CutTree xnat (SSucc nnat) t1 forest1 -> CutTree (SSucc xnat) nnat (case lemma2 nnat xnat of QED -> t1) (F1 tree0 forest1)) (lumberjack_go (SSucc nnat0) forest0)) lumberjack :: forall b a. BNonZero b ~ 'True => Forest 'Zero b a -> NonEmpty.NonEmpty (CutTree 'Zero b a) lumberjack trees = let cutTrees :: Vec (Popcount b) (CutTree 'Zero b a) cutTrees = lumberjack_go SZero trees in case lemma3 (forestSingleton trees :: SBin b) of QED -> vecToNonEmpty cutTrees forestSingleton :: Forest k b a -> SBin b forestSingleton FEnd = SBEnd forestSingleton (F0 t) = SB0 (forestSingleton t) forestSingleton (F1 _ t) = SB1 (forestSingleton t) popForest :: forall a b. Ord a => CutTree 'Zero b a -> (a, Forest 'Zero (BDec b) a) popForest (CutTree _xnat _nnat (Tree x (children :: Children r a)) (forest :: Forest 'Zero l a)) = let cforest = childrenToForest children merged :: Forest 'Zero (BAdd l (Ones r)) a merged = mergeForests forest cforest evidence :: SBin (BAdd l (Ones r)) evidence = forestSingleton merged in (x, case lemma4 evidence of QED -> merged) type family BDec (binary :: Binary) :: Binary where BDec ('B1 b) = 'B0 b BDec ('B0 b) = 'B1 (BDec b) lemma4 :: (Width x ~ Width (BInc x)) => SBin x -> BDec (BInc x) :~: x lemma4 (SB0 _) = QED lemma4 (SB1 b) = case lemma4 b of QED -> QED popHeap :: (BNonZero b ~ 'True, Ord a) => Heap b a -> (a, Heap (BDec b) a) popHeap (Heap forest0) = let cutTrees = lumberjack forest0 selected = minimumBy (comparing cutTreeRoot) cutTrees in case popForest selected of (x, forest1) -> (x, Heap forest1) where cutTreeRoot :: CutTree k b a -> a cutTreeRoot (CutTree _ _ (Tree x _) _) = x instance forall a b. Show a => Show (Heap b a) where show = intercalate "\n" . goTrees 0 . unHeap where goTrees :: forall m c. Show a => Int -> Forest m c a -> [String] goTrees _ FEnd = [] goTrees order (F0 trees) = ("(no tree of order " ++ show order ++ ")") : goTrees (order + 1) trees goTrees order (F1 tree trees) = ("(tree of order " ++ show order ++ ")") : goTree " " tree ++ goTrees (order + 1) trees goTree :: forall m. String -> Tree m a -> [String] goTree indentation (Tree x children) = (indentation ++ show x) : goChildren (' ' : indentation) children goChildren :: forall m. String -> Children m a -> [String] goChildren _ CZero = [] goChildren indentation (CCons x xs) = goTree indentation x ++ goChildren indentation xs type family BIncLTR (b :: Binary) :: Binary where BIncLTR b = FromRight 'B1 (Carry b) type family Carry (b :: Binary) :: Either Binary Binary where Carry ('B1 'BEnd) = 'Left ('B0 'BEnd) Carry ('B0 'BEnd) = 'Right ('B1 'BEnd) Carry ('B0 b) = 'Right (UnEither 'B1 'B0 (Carry b)) Carry ('B1 b) = MapEither 'B0 'B1 (Carry b) type family MapEither (f :: a -> c) (g :: b -> d) (e :: Either a b) :: Either c d where MapEither f _ ('Left x) = 'Left (f x) MapEither _ g ('Right y) = 'Right (g y) type family UnEither (f :: a -> c) (g :: b -> c) (e :: Either a b) :: c where UnEither f _ ('Left x) = f x UnEither _ g ('Right y) = g y type family FromRight (f :: a -> b) (e :: Either a b) :: b where FromRight f ('Left x) = f x FromRight _ ('Right y) = y