Created
April 17, 2017 20:30
-
-
Save wangbj/5939aa7a30c3d756d98f5b5775e162a6 to your computer and use it in GitHub Desktop.
type level function is quite slow
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 MultiParamTypeClasses #-} | |
{-# LANGUAGE FunctionalDependencies #-} | |
{-# LANGUAGE TypeFamilyDependencies #-} | |
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE PolyKinds #-} | |
{-# LANGUAGE TypeOperators #-} | |
{-# LANGUAGE GADTs #-} | |
{-# LANGUAGE UndecidableInstances #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
import qualified GHC.TypeLits as TypeLits | |
data Z | |
data S n | |
class KnownNat n where | |
natSing :: n -> Integer | |
instance KnownNat Z where | |
natSing _ = 0 | |
instance KnownNat n => KnownNat (S n) where | |
natSing _ = 1 + natSing (undefined :: n) | |
natVal :: KnownNat n => n -> Integer | |
natVal = natSing | |
type family FromInteger a where | |
FromInteger 0 = Z | |
FromInteger n = S (FromInteger (n TypeLits.- 1)) | |
type T10k = FromInteger 10000 | |
-- natVal (undefined :: T10k) => reduction stack overflow | |
main = natVal (undefined :: T10k) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment