heterogeneous-list-literals (empty) → 0.1.0.1
raw patch · 6 files changed
+224/−0 lines, 6 filesdep +OneTupledep +basedep +heterogeneous-list-literalssetup-changed
Dependencies added: OneTuple, base, heterogeneous-list-literals
Files
- ChangeLog.md +0/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- heterogeneous-list-literals.cabal +57/−0
- src/Data/HeterogeneousListLiterals.hs +109/−0
- test/Docs.hs +26/−0
+ ChangeLog.md view
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2018, David Millar-Durrant++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 David Millar-Durrant 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ heterogeneous-list-literals.cabal view
@@ -0,0 +1,57 @@+-- Initial indexed-container-literals.cabal generated by cabal init. For +-- further documentation, see http://haskell.org/cabal/users-guide/++name: heterogeneous-list-literals+version: 0.1.0.1+synopsis: Allows the use of tuples as literals for Heterogeneous collections+homepage: https://github.com/davidm-d/heterogeneous-list-literals+license: BSD3+license-file: LICENSE+author: David Millar-Durrant+maintainer: dmillardurrant@gmail.com++-- copyright: +category: Data+build-type: Simple+extra-source-files: ChangeLog.md+cabal-version: >=1.10+description: + This is an incredibly simple library, which makes writing literals for heterogeneous collections easy+ .+ If you write a function with the signature+ .+ > hList :: HLL input output => input -> HList output+ then+ .+ > a :: HList '[]+ > a = hList ()+ > + > b :: HList '[Bool]+ > b = hList (OneTuple True)+ > + > c :: HList '[Bool, Int, Double, String]+ > c = hList (True, 24, 10.5, "Fire")+ .+ The full code is in test\/Docs.hs+ .+ This only supports literals of length up to 20, though that can be easily extended using the code generator in src\/Data\/HeterogeneousListLiterals++library+ exposed-modules: Data.HeterogeneousListLiterals+ -- other-modules: + -- other-extensions: + build-depends: base >=4.9 && <= 4.11+ , OneTuple >= 0.2.1+ hs-source-dirs: src+ default-language: Haskell2010++test-suite HeterogenousListLiterals-test + type: exitcode-stdio-1.0+ main-is: Docs.hs+ build-depends: base, heterogeneous-list-literals+ hs-source-dirs: test+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/davidm-d/heterogeneous-list-literals
+ src/Data/HeterogeneousListLiterals.hs view
@@ -0,0 +1,109 @@+{-# Language+ DataKinds,+ MultiParamTypeClasses,+ TypeInType,+ ConstraintKinds,+ FunctionalDependencies,+ FlexibleInstances+ #-}+module Data.HeterogeneousListLiterals (+ HeterogeneousListLiteral(..)+ , HLL+ , module Data.Tuple.OneTuple+ ) where++import GHC.TypeLits+import Data.Kind+import Data.Tuple.OneTuple+import Control.Monad+import Data.Dynamic+import Data.Tuple++-- | An alias for HeterogeneousListLiteral+type HLL = HeterogeneousListLiteral++-- | A type class which allows you to write tuples which can be transformed into a dynamic list.+-- The type of the input tuple is stored as a type level list in output, check the ReadMe for examples+class HeterogeneousListLiteral (input :: Type) (output :: [Type]) | input -> output, output -> input where+ toDynamicList :: input -> [Dynamic]++instance HeterogeneousListLiteral () '[] where+ toDynamicList () = []++instance Typeable a1 => HeterogeneousListLiteral (OneTuple a1) '[a1] where+ toDynamicList (OneTuple a1) = [toDyn a1]++-- all code generated below comes from this function+-- generate :: Int -- ^ up to N+-- -> String+-- generate n = unlines $ join $ map ("":) $ take n $ dropOneTuple res where+-- values = map ((:) 'a' . show) [1 :: Int ..]+-- dynamicList = map ("toDyn " ++) values+-- constraints = map ("Typeable " ++) values+-- withCommas = scanl1 (\a b -> a++","++b)+-- className = "HeterogeneousListLiteral"+-- template vals dynamicList constraint =+-- ["instance " ++ "(" ++ constraint ++ ") => " ++ className ++ " (" ++ vals ++ ") '[" ++ vals ++ "] where"+-- ," toDynamicList (" ++ vals ++ ") = [" ++ dynamicList ++ "]"]+-- res = zipWith3 template+-- (withCommas values)+-- (withCommas dynamicList)+-- (withCommas constraints)+-- dropOneTuple = tail++instance (Typeable a1,Typeable a2) => HeterogeneousListLiteral (a1,a2) '[a1,a2] where+ toDynamicList (a1,a2) = [toDyn a1,toDyn a2]++instance (Typeable a1,Typeable a2,Typeable a3) => HeterogeneousListLiteral (a1,a2,a3) '[a1,a2,a3] where+ toDynamicList (a1,a2,a3) = [toDyn a1,toDyn a2,toDyn a3]++instance (Typeable a1,Typeable a2,Typeable a3,Typeable a4) => HeterogeneousListLiteral (a1,a2,a3,a4) '[a1,a2,a3,a4] where+ toDynamicList (a1,a2,a3,a4) = [toDyn a1,toDyn a2,toDyn a3,toDyn a4]++instance (Typeable a1,Typeable a2,Typeable a3,Typeable a4,Typeable a5) => HeterogeneousListLiteral (a1,a2,a3,a4,a5) '[a1,a2,a3,a4,a5] where+ toDynamicList (a1,a2,a3,a4,a5) = [toDyn a1,toDyn a2,toDyn a3,toDyn a4,toDyn a5]++instance (Typeable a1,Typeable a2,Typeable a3,Typeable a4,Typeable a5,Typeable a6) => HeterogeneousListLiteral (a1,a2,a3,a4,a5,a6) '[a1,a2,a3,a4,a5,a6] where+ toDynamicList (a1,a2,a3,a4,a5,a6) = [toDyn a1,toDyn a2,toDyn a3,toDyn a4,toDyn a5,toDyn a6]++instance (Typeable a1,Typeable a2,Typeable a3,Typeable a4,Typeable a5,Typeable a6,Typeable a7) => HeterogeneousListLiteral (a1,a2,a3,a4,a5,a6,a7) '[a1,a2,a3,a4,a5,a6,a7] where+ toDynamicList (a1,a2,a3,a4,a5,a6,a7) = [toDyn a1,toDyn a2,toDyn a3,toDyn a4,toDyn a5,toDyn a6,toDyn a7]++instance (Typeable a1,Typeable a2,Typeable a3,Typeable a4,Typeable a5,Typeable a6,Typeable a7,Typeable a8) => HeterogeneousListLiteral (a1,a2,a3,a4,a5,a6,a7,a8) '[a1,a2,a3,a4,a5,a6,a7,a8] where+ toDynamicList (a1,a2,a3,a4,a5,a6,a7,a8) = [toDyn a1,toDyn a2,toDyn a3,toDyn a4,toDyn a5,toDyn a6,toDyn a7,toDyn a8]++instance (Typeable a1,Typeable a2,Typeable a3,Typeable a4,Typeable a5,Typeable a6,Typeable a7,Typeable a8,Typeable a9) => HeterogeneousListLiteral (a1,a2,a3,a4,a5,a6,a7,a8,a9) '[a1,a2,a3,a4,a5,a6,a7,a8,a9] where+ toDynamicList (a1,a2,a3,a4,a5,a6,a7,a8,a9) = [toDyn a1,toDyn a2,toDyn a3,toDyn a4,toDyn a5,toDyn a6,toDyn a7,toDyn a8,toDyn a9]++instance (Typeable a1,Typeable a2,Typeable a3,Typeable a4,Typeable a5,Typeable a6,Typeable a7,Typeable a8,Typeable a9,Typeable a10) => HeterogeneousListLiteral (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) '[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10] where+ toDynamicList (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10) = [toDyn a1,toDyn a2,toDyn a3,toDyn a4,toDyn a5,toDyn a6,toDyn a7,toDyn a8,toDyn a9,toDyn a10]++instance (Typeable a1,Typeable a2,Typeable a3,Typeable a4,Typeable a5,Typeable a6,Typeable a7,Typeable a8,Typeable a9,Typeable a10,Typeable a11) => HeterogeneousListLiteral (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11) '[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11] where+ toDynamicList (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11) = [toDyn a1,toDyn a2,toDyn a3,toDyn a4,toDyn a5,toDyn a6,toDyn a7,toDyn a8,toDyn a9,toDyn a10,toDyn a11]++instance (Typeable a1,Typeable a2,Typeable a3,Typeable a4,Typeable a5,Typeable a6,Typeable a7,Typeable a8,Typeable a9,Typeable a10,Typeable a11,Typeable a12) => HeterogeneousListLiteral (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12) '[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12] where+ toDynamicList (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12) = [toDyn a1,toDyn a2,toDyn a3,toDyn a4,toDyn a5,toDyn a6,toDyn a7,toDyn a8,toDyn a9,toDyn a10,toDyn a11,toDyn a12]++instance (Typeable a1,Typeable a2,Typeable a3,Typeable a4,Typeable a5,Typeable a6,Typeable a7,Typeable a8,Typeable a9,Typeable a10,Typeable a11,Typeable a12,Typeable a13) => HeterogeneousListLiteral (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13) '[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13] where+ toDynamicList (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13) = [toDyn a1,toDyn a2,toDyn a3,toDyn a4,toDyn a5,toDyn a6,toDyn a7,toDyn a8,toDyn a9,toDyn a10,toDyn a11,toDyn a12,toDyn a13]++instance (Typeable a1,Typeable a2,Typeable a3,Typeable a4,Typeable a5,Typeable a6,Typeable a7,Typeable a8,Typeable a9,Typeable a10,Typeable a11,Typeable a12,Typeable a13,Typeable a14) => HeterogeneousListLiteral (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14) '[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14] where+ toDynamicList (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14) = [toDyn a1,toDyn a2,toDyn a3,toDyn a4,toDyn a5,toDyn a6,toDyn a7,toDyn a8,toDyn a9,toDyn a10,toDyn a11,toDyn a12,toDyn a13,toDyn a14]++instance (Typeable a1,Typeable a2,Typeable a3,Typeable a4,Typeable a5,Typeable a6,Typeable a7,Typeable a8,Typeable a9,Typeable a10,Typeable a11,Typeable a12,Typeable a13,Typeable a14,Typeable a15) => HeterogeneousListLiteral (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15) '[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15] where+ toDynamicList (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15) = [toDyn a1,toDyn a2,toDyn a3,toDyn a4,toDyn a5,toDyn a6,toDyn a7,toDyn a8,toDyn a9,toDyn a10,toDyn a11,toDyn a12,toDyn a13,toDyn a14,toDyn a15]++instance (Typeable a1,Typeable a2,Typeable a3,Typeable a4,Typeable a5,Typeable a6,Typeable a7,Typeable a8,Typeable a9,Typeable a10,Typeable a11,Typeable a12,Typeable a13,Typeable a14,Typeable a15,Typeable a16) => HeterogeneousListLiteral (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16) '[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16] where+ toDynamicList (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16) = [toDyn a1,toDyn a2,toDyn a3,toDyn a4,toDyn a5,toDyn a6,toDyn a7,toDyn a8,toDyn a9,toDyn a10,toDyn a11,toDyn a12,toDyn a13,toDyn a14,toDyn a15,toDyn a16]++instance (Typeable a1,Typeable a2,Typeable a3,Typeable a4,Typeable a5,Typeable a6,Typeable a7,Typeable a8,Typeable a9,Typeable a10,Typeable a11,Typeable a12,Typeable a13,Typeable a14,Typeable a15,Typeable a16,Typeable a17) => HeterogeneousListLiteral (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17) '[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17] where+ toDynamicList (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17) = [toDyn a1,toDyn a2,toDyn a3,toDyn a4,toDyn a5,toDyn a6,toDyn a7,toDyn a8,toDyn a9,toDyn a10,toDyn a11,toDyn a12,toDyn a13,toDyn a14,toDyn a15,toDyn a16,toDyn a17]++instance (Typeable a1,Typeable a2,Typeable a3,Typeable a4,Typeable a5,Typeable a6,Typeable a7,Typeable a8,Typeable a9,Typeable a10,Typeable a11,Typeable a12,Typeable a13,Typeable a14,Typeable a15,Typeable a16,Typeable a17,Typeable a18) => HeterogeneousListLiteral (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18) '[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18] where+ toDynamicList (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18) = [toDyn a1,toDyn a2,toDyn a3,toDyn a4,toDyn a5,toDyn a6,toDyn a7,toDyn a8,toDyn a9,toDyn a10,toDyn a11,toDyn a12,toDyn a13,toDyn a14,toDyn a15,toDyn a16,toDyn a17,toDyn a18]++instance (Typeable a1,Typeable a2,Typeable a3,Typeable a4,Typeable a5,Typeable a6,Typeable a7,Typeable a8,Typeable a9,Typeable a10,Typeable a11,Typeable a12,Typeable a13,Typeable a14,Typeable a15,Typeable a16,Typeable a17,Typeable a18,Typeable a19) => HeterogeneousListLiteral (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19) '[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19] where+ toDynamicList (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19) = [toDyn a1,toDyn a2,toDyn a3,toDyn a4,toDyn a5,toDyn a6,toDyn a7,toDyn a8,toDyn a9,toDyn a10,toDyn a11,toDyn a12,toDyn a13,toDyn a14,toDyn a15,toDyn a16,toDyn a17,toDyn a18,toDyn a19]++instance (Typeable a1,Typeable a2,Typeable a3,Typeable a4,Typeable a5,Typeable a6,Typeable a7,Typeable a8,Typeable a9,Typeable a10,Typeable a11,Typeable a12,Typeable a13,Typeable a14,Typeable a15,Typeable a16,Typeable a17,Typeable a18,Typeable a19,Typeable a20) => HeterogeneousListLiteral (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20) '[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20] where+ toDynamicList (a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20) = [toDyn a1,toDyn a2,toDyn a3,toDyn a4,toDyn a5,toDyn a6,toDyn a7,toDyn a8,toDyn a9,toDyn a10,toDyn a11,toDyn a12,toDyn a13,toDyn a14,toDyn a15,toDyn a16,toDyn a17,toDyn a18,toDyn a19,toDyn a20]
+ test/Docs.hs view
@@ -0,0 +1,26 @@+{-# Language TypeApplications, DataKinds, TypeInType, KindSignatures #-}++import Data.HeterogeneousListLiterals+import GHC.TypeLits+import Data.Kind+import Data.Dynamic++data HList (a :: [Type]) = HList [Dynamic]++hList :: HLL input output => input -> HList output+hList = HList . toDynamicList++a :: HList '[]+a = hList ()++b :: HList '[Bool]+b = hList (OneTuple True)++c :: HList '[Bool, Int, Double, String]+c = hList (True, 24, 10.5, "Fire")++-- | just making sure the docs type check+main :: IO ()+main = do+ _ <- return (a,b,c)+ return ()