Created
August 21, 2019 16:21
-
-
Save JakobBruenker/50c48c8bed2e151921a11f693686428a to your computer and use it in GitHub Desktop.
This file contains 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 TypeInType #-} | |
{-# LANGUAGE TypeOperators #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE UndecidableInstances #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE ConstraintKinds #-} | |
{-# LANGUAGE ConstraintKinds #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
module CompileTime where | |
import GHC.TypeLits | |
import Data.Proxy | |
type family IsLastZero (xs :: [Nat]) :: Bool where | |
IsLastZero '[] = False | |
IsLastZero '[0] = True | |
IsLastZero (_:xs) = IsLastZero xs | |
type family FromNatDown (n :: Nat) :: [Nat] where | |
FromNatDown 0 = '[0] | |
FromNatDown n = n : FromNatDown (n - 1) | |
class LastZero xs | |
instance IsLastZero xs ~ True => LastZero xs | |
type LastZero' xs = IsLastZero xs ~ True | |
{- | |
GHCi: | |
*CompileTime> import Data.Proxy | |
*CompileTime Data.Proxy> | |
*CompileTime Data.Proxy> :set -XDataKinds -XGADTs -XFlexibleContexts | |
*CompileTime Data.Proxy> :set -freduction-depth=0 | |
*CompileTime Data.Proxy> :set +s | |
*CompileTime Data.Proxy> Proxy :: LastZero (FromNatDown 1000) => Proxy () | |
Proxy | |
(20.17 secs, 86,688 bytes) | |
*CompileTime Data.Proxy> Proxy :: LastZero' (FromNatDown 1000) => Proxy () | |
Proxy | |
(1.99 secs, 86,688 bytes) | |
=> type is roughly 10x faster for list size 1000 | |
-} |
Author
JakobBruenker
commented
Aug 21, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment