packages feed

reflection 1.1.2 → 1.1.3

raw patch · 3 files changed

+54/−37 lines, 3 files

Files

fast/Data/Reflection.hs view
@@ -1,4 +1,6 @@-{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, Rank2Types #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE FunctionalDependencies #-}+{-# LANGUAGE Rank2Types #-} ---------------------------------------------------------------------------- -- | -- Module     : Data.Reflection@@ -11,8 +13,9 @@ -- Stability   : experimental -- Portability : non-portable ----- Based on the Functional Pearl: Implicit Configurations paper by--- Oleg Kiselyov and Chung-chieh Shan.+-- Reifies arbitrary terms at the type level. Based on the Functional+-- Pearl: Implicit Configurations paper by Oleg Kiselyov and+-- Chung-chieh Shan. -- -- <http://www.cs.rutgers.edu/~ccshan/prepose/prepose.pdf> --@@ -20,19 +23,18 @@ -- and to cheat by using knowledge of GHC's internal representations -- by Edward Kmett and Elliott Hird. ----- Usage reduces to using two combinators, 'reify' and 'reflect'.+-- Usage comes down to two combinators, 'reify' and 'reflect'. ----- > ghci> reify 6 (\p -> reflect p + reflect p) :: Int--- > 12+-- >>> reify 6 (\p -> reflect p + reflect p)+-- 12 ----- The argument passed along by reify is just a @data Proxy t =+-- The argument passed along by reify is just a @data 'Proxy' t = -- Proxy@, so all of the information needed to reconstruct your value -- has been moved to the type level.  This enables it to be used when -- constructing instances (see @examples/Monoid.hs@). ------------------------------------------------------------------------------- module Data.Reflection     (-    -- * Reifying any term at the type level       Reifies(..)     , reify     ) where@@ -41,10 +43,12 @@ import Unsafe.Coerce  class Reifies s a | s -> a where-  reflect :: p s -> a+  -- | Recover a value inside a 'reify' context, given a proxy for its+  -- reified type.+  reflect :: proxy s -> a -newtype Magic a w = Magic (forall s. Reifies s a => Proxy s -> w)+newtype Magic a r = Magic (forall s. Reifies s a => Proxy s -> r) -reify :: a -> (forall s. Reifies s a => Proxy s -> w) -> w+-- | Reify a value at the type level, to be recovered with 'reflect'.+reify :: a -> (forall s. Reifies s a => Proxy s -> r) -> r reify a k = (unsafeCoerce (Magic k) $! const a) Proxy-
reflection.cabal view
@@ -1,5 +1,5 @@ name:           reflection-version:        1.1.2+version:        1.1.3 license:        BSD3 license-file:   LICENSE author:         Edward A. Kmett, Elliott Hird, Oleg Kiselyov and Chung-chieh Shan@@ -7,7 +7,8 @@ stability:      experimental homepage:       http://github.com/ekmett/reflection category:       Data, Reflection, Dependent Types-synopsis:       Reifies arbitrary terms into types that can be reflected back into terms+synopsis:       Reifies arbitrary terms into types that can be reflected back+                into terms copyright:      2009-2012 Edward A. Kmett,                 2012 Elliott Hird,                 2004 Oleg Kiselyov and Chung-chieh Shan@@ -18,16 +19,19 @@   \"Functional Pearl: Implicit Configurations\" by Oleg Kiselyov and   Chung-chieh Shan. However, the API has been streamlined to improve performance.   .-  The original paper can be obtained from <http://www.cs.rutgers.edu/~ccshan/prepose/prepose.pdf>+  The original paper can be obtained from+  <http://www.cs.rutgers.edu/~ccshan/prepose/prepose.pdf>.   .   /Changes from 0.5 to 1.1/:   .-  * Much faster implementation available that is about 50 /times/ faster than 0.9 and which runs-    purely on black magic. This version is now used by default. To turn it off install with the-    'slow' flag. If you encounter a problem with implementation, please contact the author.+  * Much faster implementation available that is about 50 /times/ faster than+    0.9 and which runs purely on black magic. This version is now used by+    default. To turn it off install with the @slow@ flag. If you encounter a+    problem with the implementation, please contact the author.   .-  * Removed @ReifiedNum@, @reflectNum@, and @reifyIntegral@; @reify@ and @reflect@ are-    about 3 orders of magnitude faster than the special case combinators were.+  * Removed @ReifiedNum@, @reflectNum@, and @reifyIntegral@; @reify@ and+    @reflect@ are about 3 orders of magnitude faster than the special case+    combinators were.   .   /Changes in 0.5/:   .@@ -60,6 +64,9 @@ library   ghc-options: -Wall +  if impl(ghc >= 7.2)+    default-extensions: Trustworthy+   build-depends:     base >= 4 && < 5,     tagged >= 0.2.3 && < 0.3@@ -69,10 +76,9 @@   if impl(ghc) && !flag(slow)     hs-source-dirs: fast   else-    other-extensions: ScopedTypeVariables, FlexibleInstances+    other-extensions: ScopedTypeVariables, FlexibleInstances, CPP     hs-source-dirs: slow    other-extensions: MultiParamTypeClasses, FunctionalDependencies, Rank2Types    exposed-modules: Data.Reflection-
slow/Data/Reflection.hs view
@@ -1,9 +1,13 @@-{-# LANGUAGE FunctionalDependencies #-}+{-# LANGUAGE CPP #-} {-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE Rank2Types #-}-{-# OPTIONS_GHC -fno-cse -fno-full-laziness -fno-float-in -fno-warn-unused-binds #-}+{-# OPTIONS_GHC -fno-cse #-}+{-# OPTIONS_GHC -fno-full-laziness #-}+{-# OPTIONS_GHC -fno-float-in #-}+{-# OPTIONS_GHC -fno-warn-unused-binds #-} ---------------------------------------------------------------------------- -- | -- Module     : Data.Reflection@@ -16,20 +20,21 @@ -- Stability   : experimental -- Portability : non-portable ----- Based on the Functional Pearl: Implicit Configurations paper by--- Oleg Kiselyov and Chung-chieh Shan.+-- Reifies arbitrary terms at the type level. Based on the Functional+-- Pearl: Implicit Configurations paper by Oleg Kiselyov and+-- Chung-chieh Shan. -- -- <http://www.cs.rutgers.edu/~ccshan/prepose/prepose.pdf> ----- The approach from the paper was modified to work with Data.Proxy and streamline--- the API by Edward Kmett and Elliott Hird.+-- The approach from the paper was modified to work with Data.Proxy+-- and streamline the API by Edward Kmett and Elliott Hird. ----- Usage reduces to using two combinators, 'reify' and 'reflect'.+-- Usage comes down to two combinators, 'reify' and 'reflect'. ----- > ghci> reify 6 (\p -> reflect p + reflect p) :: Int--- > 12+-- >>> reify 6 (\p -> reflect p + reflect p)+-- 12 ----- The argument passed along by reify is just a @data Proxy t =+-- The argument passed along by reify is just a @data 'Proxy' t = -- Proxy@, so all of the information needed to reconstruct your value -- has been moved to the type level.  This enables it to be used when -- constructing instances (see @examples/Monoid.hs@).@@ -37,7 +42,6 @@  module Data.Reflection     (-    -- * Reifying any term at the type level       Reifies(..)     , reify     ) where@@ -51,7 +55,7 @@ import Data.Word  class B s where-  reflectByte :: p s -> IntPtr+  reflectByte :: proxy s -> IntPtr  #define CAT(a,b) a/**/b @@ -95,7 +99,7 @@ impossible :: a impossible = error "Data.Reflection.reifyByte: impossible" -reifyByte :: Word8 -> (forall s. B s => Proxy s -> w) -> w+reifyByte :: Word8 -> (forall s. B s => Proxy s -> r) -> r reifyByte w k = case w of { #define GO(n) n -> k (Proxy :: Proxy CAT(T,n)); BYTES(GO)@@ -104,7 +108,9 @@ }  class Reifies s a | s -> a where-  reflect :: p s -> a+  -- | Recover a value inside a 'reify' context, given a proxy for its+  -- reified type.+  reflect :: proxy s -> a  newtype Stable b0 b1 b2 b3 b4 b5 b6 b7 a =   Stable (Stable b0 b1 b2 b3 b4 b5 b6 b7 a)@@ -142,7 +148,8 @@ reflectBefore f = const $! f Proxy {-# NOINLINE reflectBefore #-} -reify :: a -> (forall s. (Reifies s a) => Proxy s -> w) -> w+-- | Reify a value at the type level, to be recovered with 'reflect'.+reify :: a -> (forall s. (Reifies s a) => Proxy s -> r) -> r reify a k = unsafeDupablePerformIO $ do   p <- newStablePtr a   let n = stablePtrToIntPtr p