on main help sign in
Use type-level Nat
commit5de021d6db0671e263ed6cbe0d84aba527927d49 authorVladislav Zavialov <vlad.z.4096@gmail.com> dateSun 17 Dec 2017, 17:41 (9 years ago) parentbac9009
2 files changed+16−17
benchmark/Main.hs+15−17 typerep-map.cabal+1
benchmark/Main.hs+15−17
@@ -1,4 +1,5 @@
11{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE PolyKinds #-}
23{-# LANGUAGE ExplicitForAll #-}
34{-# LANGUAGE ExplicitNamespaces #-}
45{-# LANGUAGE KindSignatures #-}
@@ -7,6 +8,8 @@
78{-# LANGUAGE TypeOperators #-}
89{-# LANGUAGE UndecidableInstances #-}
910
11+{-# OPTIONS_GHC -fplugin GHC.TypeLits.KnownNat.Solver #-}
12+
1013import Criterion.Main (bench, defaultMain, nf, whnf)
1114
1215import Prelude hiding (lookup)
@@ -16,7 +19,7 @@ import Control.Exception.Base (evaluate)
1619import Data.Maybe (fromJust)
1720import Data.Proxy (Proxy (..))
1821import Data.Typeable (Typeable)
19−import GHC.TypeLits (type (-), Nat)
22+import GHC.TypeLits
2023
2124import Data.TypeRep.Map (TypeRepMap (..), empty, insert, keys, lookup, size)
2225
@@ -26,31 +29,29 @@ main = do
2629 evaluate $ rknf bigMap
2730 defaultMain
2831 [ bench "lookup" $ nf tenLookups bigMap
29− , bench "insert new" $ whnf (\x -> rknf $ insert x bigMap) (proxy (111 :: Int))
30− , bench "update old" $ whnf (\x -> rknf $ insert x bigMap) (proxy 'b')
32+ , bench "insert new" $ whnf (\x -> rknf $ insert x bigMap) (Proxy :: Proxy 9999999999)
33+ , bench "update old" $ whnf (\x -> rknf $ insert x bigMap) (Proxy :: Proxy 1)
3134 ]
3235
33−tenLookups :: TypeRepMap Proxy
34− -> ( Proxy (BigProxy 10), Proxy (BigProxy 20)
35− , Proxy (BigProxy 30), Proxy (BigProxy 40)
36− , Proxy (BigProxy 50), Proxy (BigProxy 60)
37− , Proxy (BigProxy 70), Proxy (BigProxy 80)
36+tenLookups :: TypeRepMap (Proxy :: Nat -> *)
37+ -> ( Proxy 10, Proxy 20, Proxy 30, Proxy 40
38+ , Proxy 50, Proxy 60, Proxy 70, Proxy 80
3839 )
3940tenLookups tmap = (lp, lp, lp, lp, lp, lp, lp, lp)
4041 where
41− lp :: Typeable a => Proxy a
42+ lp :: forall (a::Nat). Typeable a => Proxy a
4243 lp = fromJust $ lookup tmap
4344
4445-- TypeRepMap of 10000 elements
45−bigMap :: TypeRepMap Proxy
46−bigMap = buildBigMap 10000 (Proxy :: Proxy Z) empty
46+bigMap :: TypeRepMap (Proxy :: Nat -> *)
47+bigMap = buildBigMap 10000 (Proxy :: Proxy 0) empty
4748
4849data Z
4950data S a
5051
51−buildBigMap :: forall a . Typeable a => Int -> Proxy a -> TypeRepMap Proxy -> TypeRepMap Proxy
52−buildBigMap 1 x = insert (proxy 'a') . insert x
53−buildBigMap n x = insert x . buildBigMap (n - 1) (Proxy :: Proxy (S a))
52+buildBigMap :: forall a . (Typeable a, KnownNat a) => Int -> Proxy (a :: Nat) -> TypeRepMap (Proxy :: Nat -> *) -> TypeRepMap (Proxy :: Nat -> *)
53+buildBigMap 1 x = insert x
54+buildBigMap n x = insert x . buildBigMap (n - 1) (Proxy :: Proxy (a + 1))
5455
5556rknf :: TypeRepMap f -> ()
5657rknf = rnf . keys
@@ -58,6 +59,3 @@ rknf = rnf . keys
5859type family BigProxy (n :: Nat) :: * where
5960 BigProxy 0 = Z
6061 BigProxy n = S (BigProxy (n - 1))
61−
62−proxy :: a -> Proxy a
63−proxy _ = Proxy
typerep-map.cabal+1
@@ -45,6 +45,7 @@ benchmark typerep-map-benchmark
4545 , criterion
4646 , typerep-map
4747 , deepseq
48+ , ghc-typelits-knownnat
4849
4950source-repository head
5051 type: git