validity (empty) → 0.1.0.0
raw patch · 4 files changed
+99/−0 lines, 4 filesdep +basesetup-changed
Dependencies added: base
Files
- LICENSE +21/−0
- Setup.hs +2/−0
- src/Data/Validity.hs +52/−0
- validity.cabal +24/−0
+ LICENSE view
@@ -0,0 +1,21 @@+The MIT License (MIT)++Copyright (c) 2016 Tom Sydney Kerckhove++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Data/Validity.hs view
@@ -0,0 +1,52 @@+{-# LANGUAGE FlexibleInstances #-}++{-|++ @Validity@ is used to specify additional invariants upon values that are not+ enforced by the type system.++ Let's take an example.+ Suppose we were to implement a type @Prime@ that represents prime integers.++ If you were to completely enforce the invariant that the represented number is+ a prime, then we could use @Numeric.Natural@ and only store the index of the+ given prime in the infinite sequence of prime numbers.+ This is very safe but also very expensive if we ever want to use the number,+ because we would have to calculcate all the prime numbers until that index.++ Instead we choose to implement @Prime@ by a @newtype Prime = Prime Int@.+ Now we have to maintain the invariant that the @Int@ that we use to represent+ the prime is in fact positive and a prime.++ The @Validity@ typeclass allows us to specify this invariant (and enables+ testing via the @genvalidity@ libraries:+ https://hackage.haskell.org/package/genvalidity):++ > instance Validity Prime where+ > isValid (Prime n) = isPrime n+ -}++module Data.Validity+ ( Validity(..)+ ) where+++-- | A class of types that have additional invariants defined upon them+-- that aren't enforced by the type system+class Validity a where+ isValid :: a -> Bool++-- | Any @Foldable@ of things that can be checked for validity can be checked+-- for validity+--+-- This includes lists, which means that the empty list is considered valid.+-- If the empty list should not be considered valid as part of your custom data+-- type, make sure to write a custom @Validity instance@+--+-- This also includes @Maybe@:+-- It makes sense to assume that 'Nothing' is valid.+-- If Nothing wasn't valid, you wouldn't have used a Maybe+-- in the datastructure.+instance (Validity a, Foldable t) => Validity (t a) where+ isValid = all isValid+
+ validity.cabal view
@@ -0,0 +1,24 @@+name: validity+version: 0.1.0.0+synopsis: Validity typeclass+description: Please see README.md+homepage: https://github.com/NorfairKing/validity#readme+license: MIT+license-file: LICENSE+author: Tom Sydney Kerckhove+maintainer: syd.kerckhove@gmail.com+copyright: Copyright: (c) 2016 Tom Sydney Kerckhove+category: Web+build-type: Simple+-- extra-source-files:+cabal-version: >=1.10++library+ hs-source-dirs: src+ exposed-modules: Data.Validity+ build-depends: base >= 4.7 && < 5+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/NorfairKing/validity