validated-literals (empty) → 0.1.0
raw patch · 7 files changed
+159/−0 lines, 7 filesdep +basedep +bytestringdep +template-haskellsetup-changed
Dependencies added: base, bytestring, template-haskell
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- ValidLiterals.hs +36/−0
- examples/ByteString.hs +15/−0
- examples/Even.hs +15/−0
- examples/Examples.hs +12/−0
- validated-literals.cabal +49/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Merijn Verstraaten++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 Merijn Verstraaten 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
+ ValidLiterals.hs view
@@ -0,0 +1,36 @@+{-# LANGUAGE DefaultSignatures #-}+{-# LANGUAGE FlexibleContexts #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE TemplateHaskell #-}+module ValidLiterals where++import Data.Maybe+import Language.Haskell.TH+import Language.Haskell.TH.Syntax++class Validate a b where+ fromLiteral :: a -> Maybe b++ spliceValid :: a -> b -> Q (TExp b)+ default spliceValid :: Lift b => a -> b -> Q (TExp b)+ spliceValid _ v = [|| v ||]++valid :: Validate a b => a -> Q (TExp b)+valid input = case fromLiteral input of+ Nothing -> fail "Invalid input used for type-safe validated literal!"+ Just result -> spliceValid input result++validInteger :: Validate Integer b => Integer -> Q (TExp b)+validInteger = valid++validRational :: Validate Rational b => Rational -> Q (TExp b)+validRational = valid++validString :: Validate String b => String -> Q (TExp b)+validString = valid++validList :: Validate [a] b => [a] -> Q (TExp b)+validList = valid++hackySpliceValid :: (Lift a, Validate a b) => a -> b -> Q (TExp b)+hackySpliceValid v _ = [|| fromJust (fromLiteral v) ||]
+ examples/ByteString.hs view
@@ -0,0 +1,15 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}+module ByteString (ByteString) where++import Data.ByteString.Char8 (ByteString, pack)+import Data.Char++import ValidLiterals++instance Validate String ByteString where+ fromLiteral s+ | all ((<=255) . ord) s = Just $ pack s+ | otherwise = Nothing++ spliceValid = hackySpliceValid
+ examples/Even.hs view
@@ -0,0 +1,15 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE TemplateHaskell #-}+module Even where++import ValidLiterals++newtype Even = Even Integer++instance Integral a => Validate a Even where+ fromLiteral i+ | even i = Just . Even $ fromIntegral i+ | otherwise = Nothing++ spliceValid _ (Even i) = [|| Even i ||]
+ examples/Examples.hs view
@@ -0,0 +1,12 @@+{-# LANGUAGE TemplateHaskell #-}+module Examples where++import ValidLiterals+import Even+import ByteString++x :: Even+x = $$(validInteger 38)++b :: ByteString+b = $$(valid "HTTP/1.1 GET")
+ validated-literals.cabal view
@@ -0,0 +1,49 @@+Name: validated-literals+Version: 0.1.0++Homepage: https://github.com/merijn/validated-literals+Bug-Reports: https://github.com/merijn/validated-literals/issues++Author: Merijn Verstraaten+Maintainer: Merijn Verstraaten <merijn@inconsistent.nl>+Copyright: Copyright © 2015 Merijn Verstraaten++License: BSD3+License-File: LICENSE++Category: Data+Cabal-Version: >= 1.10+Build-Type: Simple+Tested-With: GHC == 7.8.3++Synopsis: Compile-time checking for partial smart-constructors++Description:++Extra-Source-Files:+ examples/ByteString.hs+ examples/Even.hs+ examples/Examples.hs++Library+ Default-Language: Haskell2010+ GHC-Options: -Wall++ Default-Extensions: + Other-Extensions: DefaultSignatures, FlexibleContexts,+ MultiParamTypeClasses, TemplateHaskell++ Exposed-Modules: ValidLiterals+ Other-Modules: ++ Build-Depends: base >=4.8 && <4.9+ , template-haskell+ , bytestring >=0.10 && <0.11++Source-Repository head+ Type: mercurial+ Location: https://bitbucket.org/merijnv/validated-literals++Source-Repository head+ Type: git+ Location: git+ssh://github.com:merijn/validated-literals