reflection 2.1.2 → 2.1.3
raw patch · 9 files changed
+350/−51 lines, 9 filesdep +semigroupsdep ~basenew-uploader
Dependencies added: semigroups
Dependency ranges changed: base
Files
- .travis.yml +136/−35
- CHANGELOG.markdown +3/−0
- examples/Constraints.hs +3/−1
- examples/LICENSE +31/−0
- examples/Monoid.hs +10/−3
- examples/ReaderLike.hs +69/−0
- examples/reflection-examples.cabal +70/−0
- fast/Data/Reflection.hs +11/−7
- reflection.cabal +17/−5
.travis.yml view
@@ -1,45 +1,146 @@-env:- - GHCVER=7.0.2 CABALVER=1.24- - GHCVER=7.2.2 CABALVER=1.24- - GHCVER=7.4.2 CABALVER=1.16- - GHCVER=7.6.3 CABALVER=1.16- - GHCVER=7.8.4 CABALVER=1.18- - GHCVER=7.10.1 CABALVER=1.22- - GHCVER=8.0.1 CABALVER=1.24- - GHCVER=head CABALVER=head+# This Travis job script has been generated by a script via+#+# runghc make_travis_yml_2.hs '-o' '.travis.yml' '--irc-channel=irc.freenode.org#haskell-lens' '--no-no-tests-no-bench' '--no-installed' 'cabal.project'+#+# For more information, see https://github.com/hvr/multi-ghc-travis+#+language: c+sudo: false +git:+ submodules: false # whether to recursively clone submodules++notifications:+ irc:+ channels:+ - "irc.freenode.org#haskell-lens"+ skip_join: true+ template:+ - "\x0313reflection\x03/\x0306%{branch}\x03 \x0314%{commit}\x03 %{build_url} %{message}"++cache:+ directories:+ - $HOME/.cabal/packages+ - $HOME/.cabal/store++before_cache:+ - rm -fv $HOME/.cabal/packages/hackage.haskell.org/build-reports.log+ # remove files that are regenerated by 'cabal update'+ - rm -fv $HOME/.cabal/packages/hackage.haskell.org/00-index.*+ - rm -fv $HOME/.cabal/packages/hackage.haskell.org/*.json+ - rm -fv $HOME/.cabal/packages/hackage.haskell.org/01-index.cache+ - rm -fv $HOME/.cabal/packages/hackage.haskell.org/01-index.tar+ - rm -fv $HOME/.cabal/packages/hackage.haskell.org/01-index.tar.idx++ - rm -rfv $HOME/.cabal/packages/head.hackage+ matrix:+ include:+ - compiler: "ghc-7.0.4"+ # env: TEST=--disable-tests BENCH=--disable-benchmarks+ addons: {apt: {packages: [ghc-ppa-tools,cabal-install-2.0,ghc-7.0.4], sources: [hvr-ghc]}}+ - compiler: "ghc-7.2.2"+ # env: TEST=--disable-tests BENCH=--disable-benchmarks+ addons: {apt: {packages: [ghc-ppa-tools,cabal-install-2.0,ghc-7.2.2], sources: [hvr-ghc]}}+ - compiler: "ghc-7.4.2"+ # env: TEST=--disable-tests BENCH=--disable-benchmarks+ addons: {apt: {packages: [ghc-ppa-tools,cabal-install-2.0,ghc-7.4.2], sources: [hvr-ghc]}}+ - compiler: "ghc-7.6.3"+ # env: TEST=--disable-tests BENCH=--disable-benchmarks+ addons: {apt: {packages: [ghc-ppa-tools,cabal-install-2.0,ghc-7.6.3], sources: [hvr-ghc]}}+ - compiler: "ghc-7.8.4"+ # env: TEST=--disable-tests BENCH=--disable-benchmarks+ addons: {apt: {packages: [ghc-ppa-tools,cabal-install-2.0,ghc-7.8.4], sources: [hvr-ghc]}}+ - compiler: "ghc-7.10.3"+ # env: TEST=--disable-tests BENCH=--disable-benchmarks+ addons: {apt: {packages: [ghc-ppa-tools,cabal-install-2.0,ghc-7.10.3], sources: [hvr-ghc]}}+ - compiler: "ghc-8.0.2"+ # env: TEST=--disable-tests BENCH=--disable-benchmarks+ addons: {apt: {packages: [ghc-ppa-tools,cabal-install-2.0,ghc-8.0.2], sources: [hvr-ghc]}}+ - compiler: "ghc-8.2.2"+ # env: TEST=--disable-tests BENCH=--disable-benchmarks+ addons: {apt: {packages: [ghc-ppa-tools,cabal-install-2.0,ghc-8.2.2], sources: [hvr-ghc]}}+ - compiler: "ghc-8.4.1"+ env: GHCHEAD=true+ addons: {apt: {packages: [ghc-ppa-tools,cabal-install-head,ghc-8.4.1], sources: [hvr-ghc]}}+ - compiler: "ghc-head"+ env: GHCHEAD=true+ addons: {apt: {packages: [ghc-ppa-tools,cabal-install-head,ghc-head], sources: [hvr-ghc]}}+ allow_failures:- - env: GHCVER=head CABALVER=head+ - compiler: "ghc-7.0.4"+ - compiler: "ghc-7.2.2"+ - compiler: "ghc-8.4.1"+ - compiler: "ghc-head" before_install:- - travis_retry sudo add-apt-repository -y ppa:hvr/ghc- - travis_retry sudo apt-get update- - travis_retry sudo apt-get install cabal-install-$CABALVER ghc-$GHCVER- - export PATH=/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:$PATH- - cabal --version+ - HC=${CC}+ - HCPKG=${HC/ghc/ghc-pkg}+ - unset CC+ - ROOTDIR=$(pwd)+ - mkdir -p $HOME/.local/bin+ - "PATH=/opt/ghc/bin:/opt/ghc-ppa-tools/bin:$HOME/local/bin:$PATH"+ - HCNUMVER=$(( $(${HC} --numeric-version|sed -E 's/([0-9]+)\.([0-9]+)\.([0-9]+).*/\1 * 10000 + \2 * 100 + \3/') ))+ - echo $HCNUMVER install:- - travis_retry cabal update- - cabal install --enable-tests --only-dependencies+ - cabal --version+ - echo "$(${HC} --version) [$(${HC} --print-project-git-commit-id 2> /dev/null || echo '?')]"+ - BENCH=${BENCH---enable-benchmarks}+ - TEST=${TEST---enable-tests}+ - HADDOCK=${HADDOCK-true}+ - INSTALLED=${INSTALLED-true}+ - GHCHEAD=${GHCHEAD-false}+ - travis_retry cabal update -v+ - "sed -i.bak 's/^jobs:/-- jobs:/' ${HOME}/.cabal/config"+ - rm -fv cabal.project cabal.project.local+ # Overlay Hackage Package Index for GHC HEAD: https://github.com/hvr/head.hackage+ - |+ if $GHCHEAD; then+ sed -i.bak 's/-- allow-newer:.*/allow-newer: *:base, *:template-haskell, *:ghc, *:Cabal/' ${HOME}/.cabal/config + echo 'repository head.hackage' >> ${HOME}/.cabal/config+ echo ' url: http://head.hackage.haskell.org/' >> ${HOME}/.cabal/config+ echo ' secure: True' >> ${HOME}/.cabal/config+ echo ' root-keys: 07c59cb65787dedfaef5bd5f987ceb5f7e5ebf88b904bbd4c5cbdeb2ff71b740' >> ${HOME}/.cabal/config+ echo ' 2e8555dde16ebd8df076f1a8ef13b8f14c66bad8eafefd7d9e37d0ed711821fb' >> ${HOME}/.cabal/config+ echo ' 8f79fd2389ab2967354407ec852cbe73f2e8635793ac446d09461ffb99527f6e' >> ${HOME}/.cabal/config+ echo ' key-threshold: 3' >> ${HOME}/.cabal.config++ cabal new-update head.hackage -v+ fi+ - grep -Ev -- '^\s*--' ${HOME}/.cabal/config | grep -Ev '^\s*$'+ - "printf 'packages: \".\"\\n' > cabal.project"+ - cat cabal.project+ - if [ -f "./configure.ac" ]; then+ (cd "." && autoreconf -i);+ fi+ - rm -f cabal.project.freeze+ - cabal new-build -w ${HC} ${TEST} ${BENCH} --project-file="cabal.project" --dep -j2 all+ - rm -rf "."/.ghc.environment.* "."/dist+ - DISTDIR=$(mktemp -d /tmp/dist-test.XXXX)++# Here starts the actual work to be performed for the package under test;+# any command which exits with a non-zero exit code causes the build to fail. script:- - cabal configure -v2 --enable-tests- - cabal build- - cabal sdist- - export SRC_TGZ=$(cabal info . | awk '{print $2 ".tar.gz";exit}') ;- cd dist/;- if [ -f "$SRC_TGZ" ]; then- cabal install "$SRC_TGZ";- else- echo "expected '$SRC_TGZ' not found";- exit 1;- fi+ # test that source-distributions can be generated+ - (cd "." && cabal sdist)+ - mv "."/dist/reflection-*.tar.gz ${DISTDIR}/+ - cd ${DISTDIR} || false+ - find . -maxdepth 1 -name '*.tar.gz' -exec tar -xvf '{}' \;+ - "printf 'packages: reflection-*/*.cabal\\n' > cabal.project"+ - cat cabal.project -notifications:- irc:- channels:- - "irc.freenode.org#haskell-lens"- skip_join: true- template:- - "\x0313reflection\x0f/\x0306%{branch}\x0f \x0314%{commit}\x0f %{message} \x0302\x1f%{build_url}\x0f"++ # build & run tests, build benchmarks+ - cabal new-build -w ${HC} ${TEST} ${BENCH} all++ # cabal check+ - (cd reflection-* && cabal check)++ # haddock+ - rm -rf ./dist-newstyle+ - if $HADDOCK; then cabal new-haddock -w ${HC} ${TEST} ${BENCH} all; else echo "Skipping haddock generation";fi++# REGENDATA ["-o",".travis.yml","--irc-channel=irc.freenode.org#haskell-lens","--no-no-tests-no-bench","--no-installed","cabal.project"]+# EOF
CHANGELOG.markdown view
@@ -1,3 +1,6 @@+# 2.1.3 [2018.01.18]+* Add `Semigroup` instance for `ReflectedMonoid`.+ # 2.1.2 * Support cross-compilation and unregistered GHC builds.
examples/Constraints.hs view
@@ -1,4 +1,6 @@ {-# LANGUAGE Rank2Types, TypeFamilies, TypeOperators, ConstraintKinds, PolyKinds, FlexibleInstances, MultiParamTypeClasses, ScopedTypeVariables, FlexibleContexts, UndecidableInstances #-}+module Constraints where+ import Control.Newtype -- from newtype import Data.Constraint -- from constraints import Data.Constraint.Unsafe -- from constraints@@ -22,7 +24,7 @@ with :: Def p a -> (forall s. Reifies s (Def p a) => Lift p a s) -> a with d v = reify d $ lower . asProxyOf v -reifyInstance :: Def p a -> (forall s. Reifies s (Def p a) => Proxy s -> r) -> r+reifyInstance :: Def p a -> (forall (s :: *). Reifies s (Def p a) => Proxy s -> r) -> r reifyInstance = reify asProxyOf :: f s -> Proxy s -> f s
+ examples/LICENSE view
@@ -0,0 +1,31 @@+Copyright (c) 2009-2013 Edward Kmett+Copyright (c) 2004 Oleg Kiselyov and Chung-chieh Shan+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are+met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Edward Kmett nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
examples/Monoid.hs view
@@ -1,6 +1,8 @@-{-# LANGUAGE Rank2Types, FlexibleContexts, UndecidableInstances #-}+{-# LANGUAGE CPP, Rank2Types, FlexibleContexts, UndecidableInstances #-}+module Monoid where+ import Data.Reflection -- from reflection-import Data.Monoid -- from base+import Data.Semigroup -- from base import Data.Proxy -- from tagged -- | Values in our dynamically-constructed 'Monoid' over 'a'@@ -9,8 +11,13 @@ -- | A dictionary describing a 'Monoid' data Monoid_ a = Monoid_ { mappend_ :: a -> a -> a, mempty_ :: a } +instance Reifies s (Monoid_ a) => Semigroup (M a s) where+ a <> b = M $ mappend_ (reflect a) (runM a) (runM b)+ instance Reifies s (Monoid_ a) => Monoid (M a s) where- mappend a b = M $ mappend_ (reflect a) (runM a) (runM b)+#if !(MIN_VERSION_base(4,11,0))+ mappend = (<>)+#endif mempty = a where a = M $ mempty_ (reflect a) -- Construct a 'Monoid' instance out of a binary operation and unit that you have in scope!
+ examples/ReaderLike.hs view
@@ -0,0 +1,69 @@+-- The UndecidableInstances here is benign, just for the "advanced"+-- example at the end.+{-# LANGUAGE FlexibleContexts, ScopedTypeVariables, UndecidableInstances #-}++-- I don't demonstrate the advantages over implicit parameters here,+-- like multiple reifications of the same thing coexisting in+-- different, statically-checked contexts, etc.; this is intended as a+-- comparison to Reader(T).++import Data.Proxy+import Data.Reflection+import Control.Applicative+import System.IO++data MyConfig = MyConfig+ { magic :: Bool+ , volume :: Integer+ }++data Report p = Report+ { magicality :: String+ , loud :: Bool+ } deriving (Show)++-- some arbitrary thing we do calculations with+newtype Datum p = Datum Integer deriving (Read, Show)++report :: forall p. (Reifies p MyConfig) => Report p+report = Report+ { magicality = if magic conf then "Magical." else "Not so magical..."+ , loud = volume conf >= 11+ } where+ conf = reflect (Proxy :: Proxy p)++calculate :: forall p. (Reifies p MyConfig) => Datum p -> Datum p -> Datum p+calculate (Datum m) (Datum n) = Datum ((m+n) * volume conf) where+ conf = reflect (Proxy :: Proxy p)++run :: forall p. (Reifies p MyConfig) => Proxy p -> IO ()+run p = do+ d1 <- ask "Datum 1:" :: IO (Datum p)+ d2 <- ask "Datum 2:" :: IO (Datum p)+ -- look ma, no plumbing+ print $ calculate d1 (calculate d2 d1)+ print (report :: Report p)++ask :: Read s => String -> IO s+ask prompt = do+ putStr prompt+ putChar ' '+ hFlush stdout+ readLn++main :: IO ()+main = do+ conf <- MyConfig <$> ask "Magic?" <*> ask "Volume:"+ reify conf run++-- If you're feeling adventurous, here is something we could not do+-- with ReaderT:+instance Reifies p MyConfig => Num (Datum p) where+ (+) = calculate+ m * n+ | magic conf = m + n+ | otherwise = Datum 0 -- sorry, no magic for you.+ where conf = reflect (Proxy :: Proxy p)+ abs = undefined+ signum = undefined+ fromInteger = Datum
+ examples/reflection-examples.cabal view
@@ -0,0 +1,70 @@+name: reflection-examples+version: 0.1+license: BSD3+license-file: LICENSE+author: Edward A. Kmett, Elliott Hird, Oleg Kiselyov and Chung-chieh Shan+maintainer: Edward A. Kmett <ekmett@gmail.com>+stability: experimental+homepage: http://github.com/ekmett/reflection+bug-reports: http://github.com/ekmett/reflection/issues+category: Data, Reflection, Dependent Types+synopsis: Examples of reflection+description: Examples of reflection.+copyright: 2009-2013 Edward A. Kmett,+ 2012 Elliott Hird,+ 2004 Oleg Kiselyov and Chung-chieh Shan+build-type: Simple+cabal-version: >= 1.10+tested-with: GHC == 7.0.4+ , GHC == 7.2.2+ , GHC == 7.4.2+ , GHC == 7.6.3+ , GHC == 7.8.4+ , GHC == 7.10.3+ , GHC == 8.0.2+ , GHC == 8.2.2+ , GHC == 8.4.1++flag examples+ default: True++library+ if !flag(examples)+ buildable: False+ exposed-modules: Constraints+ Monoid+ build-depends: base >= 4.8 && < 5,+ constraints,+ newtype,+ reflection,+ tagged+ hs-source-dirs: .+ ghc-options: -Wall+ default-language: Haskell2010++executable reflection-from-json+ if !flag(examples)+ buildable: False+ main-is: FromJSON.hs+ build-depends: aeson,+ base >= 4.8 && < 5,+ lens,+ lens-aeson,+ reflection,+ tagged,+ text+ hs-source-dirs: .+ ghc-options: -Wall -threaded -rtsopts+ default-language: Haskell2010++executable reflection-reader-like+ if !flag(examples)+ buildable: False+ main-is: ReaderLike.hs+ build-depends: base >= 4.8 && < 5,+ reflection,+ tagged+ hs-source-dirs: .+ ghc-options: -Wall -threaded -rtsopts+ default-language: Haskell2010+
fast/Data/Reflection.hs view
@@ -8,7 +8,7 @@ {-# LANGUAGE PatternGuards #-} {-# LANGUAGE Rank2Types #-} {-# LANGUAGE ScopedTypeVariables #-}-#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 706+#if __GLASGOW_HASKELL__ >= 706 {-# LANGUAGE DataKinds #-} {-# LANGUAGE PolyKinds #-} {-# LANGUAGE TypeOperators #-}@@ -23,8 +23,6 @@ # endif #endif -{-# OPTIONS_GHC -fno-warn-orphans #-}- {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE UndecidableInstances #-}@@ -32,6 +30,7 @@ {-# OPTIONS_GHC -fno-cse #-} {-# OPTIONS_GHC -fno-full-laziness #-} {-# OPTIONS_GHC -fno-float-in #-}+{-# OPTIONS_GHC -fno-warn-orphans #-} {-# OPTIONS_GHC -fno-warn-unused-binds #-} #ifndef MIN_VERSION_base@@ -118,9 +117,9 @@ #if __GLASGOW_HASKELL__ < 710 import Data.Foldable-import Data.Monoid #endif +import Data.Semigroup as Sem import Data.Proxy #if __GLASGOW_HASKELL__ < 710@@ -132,7 +131,7 @@ import Foreign.Ptr import Foreign.StablePtr -#if (defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 707) || (defined(MIN_VERSION_template_haskell) && USE_TYPE_LITS)+#if (__GLASGOW_HASKELL__ >= 707) || (defined(MIN_VERSION_template_haskell) && USE_TYPE_LITS) import GHC.TypeLits #endif @@ -358,7 +357,7 @@ | proxyName == ''Proxy = ConE 'Proxy `SigE` (ConT ''Proxy `AppT` f ta) onProxyType1 f a = LamE [SigP WildP na] body `AppE` a- where + where body = ConE 'Proxy `SigE` (ConT ''Proxy `AppT` f na) na = VarT (mkName "na") @@ -553,8 +552,13 @@ data ReifiedMonoid a = ReifiedMonoid { reifiedMappend :: a -> a -> a, reifiedMempty :: a } +instance Reifies s (ReifiedMonoid a) => Sem.Semigroup (ReflectedMonoid a s) where+ ReflectedMonoid x <> ReflectedMonoid y = reflectResult (\m -> ReflectedMonoid (reifiedMappend m x y))+ instance Reifies s (ReifiedMonoid a) => Monoid (ReflectedMonoid a s) where- mappend (ReflectedMonoid x) (ReflectedMonoid y) = reflectResult (\m -> ReflectedMonoid (reifiedMappend m x y))+#if !(MIN_VERSION_base(4,11,0))+ mappend = (<>)+#endif mempty = reflectResult (\m -> ReflectedMonoid (reifiedMempty m )) reflectResult :: forall f s a. Reifies s a => (a -> f s) -> f s
reflection.cabal view
@@ -1,5 +1,5 @@ name: reflection-version: 2.1.2+version: 2.1.3 license: BSD3 license-file: LICENSE author: Edward A. Kmett, Elliott Hird, Oleg Kiselyov and Chung-chieh Shan@@ -27,12 +27,20 @@ . Austin Seipp's tutorial <https://www.fpcomplete.com/user/thoughtpolice/using-reflection Reflecting values to types and back> provides a summary of the approach taken by this library, along with more motivating examples.+tested-with: GHC == 7.0.4+ , GHC == 7.2.2+ , GHC == 7.4.2+ , GHC == 7.6.3+ , GHC == 7.8.4+ , GHC == 7.10.3+ , GHC == 8.0.2+ , GHC == 8.2.2+ , GHC == 8.4.1 extra-source-files:- examples/Monoid.hs- examples/FromJSON.hs- examples/Constraints.hs- examples/Benchmark.hs+ examples/reflection-examples.cabal+ examples/LICENSE+ examples/*.hs CHANGELOG.markdown README.markdown slow/Data/Reflection.hs@@ -69,6 +77,10 @@ if impl(ghc < 7.8) build-depends: tagged >= 0.4.4 && < 1++ if !impl(ghc >= 8.0)+ build-depends:+ semigroups >= 0.11 && < 0.19 default-language: Haskell98