data-checked (empty) → 0.1
raw patch · 4 files changed
+106/−0 lines, 4 filesdep +basesetup-changed
Dependencies added: base
Files
- LICENSE +27/−0
- Setup.hs +2/−0
- data-checked.cabal +33/−0
- src/Data/Checked.hs +44/−0
+ LICENSE view
@@ -0,0 +1,27 @@+Copyright (c) 2013 Mikhail Vorozhtsov+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 names of the copyright owners nor the names of the + 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
+ data-checked.cabal view
@@ -0,0 +1,33 @@+Name: data-checked+Version: 0.1+Category: Data+Stability: experimental+Synopsis: Type-indexed runtime-checked properties +Description:+ This package provides a (phantom) type-indexed newtype evidence-wrapper for+ values that are checked to satisfy the property associated with the type. ++Homepage: https://github.com/mvv/data-checked+Bug-Reports: https://github.com/mvv/data-checked/issues++Author: Mikhail Vorozhtsov <mikhail.vorozhtsov@gmail.com>+Maintainer: Mikhail Vorozhtsov <mikhail.vorozhtsov@gmail.com>+Copyright: 2013 Mikhail Vorozhtsov <mikhail.vorozhtsov@gmail.com>+License: BSD3+License-File: LICENSE++Cabal-Version: >= 1.6.0+Build-Type: Simple++Source-Repository head+ Type: git+ Location: https://github.com/mvv/data-checked.git++Library+ Build-Depends:+ base >= 4 && < 5+ Hs-Source-Dirs: src+ GHC-Options: -Wall+ Exposed-Modules:+ Data.Checked+
+ src/Data/Checked.hs view
@@ -0,0 +1,44 @@+{-# LANGUAGE UnicodeSyntax #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE MultiParamTypeClasses #-}++-- | Type-indexed runtime-checked properties.+module Data.Checked+ ( Checked+ , trustMe+ , checked+ , Property(..)+ , maybeHolds+ , check+ ) where++-- | Wrapper-evidence for property /p/.+newtype Checked p v = Checked v++-- | Use when the property can be deduced without a runtime check.+trustMe ∷ v → Checked p v+trustMe = Checked+{-# INLINE trustMe #-}++-- | Unwrap the checked value.+checked ∷ Checked p v → v+checked (Checked v) = v+{-# INLINE checked #-}++class Property p v where+ -- | Test if the property holds for the given value.+ -- The first argument is supposed to be ignored.+ holds ∷ p → v → Bool++-- | Return 'Just' /v/ if /p/ holds and 'Nothing' overwise.+maybeHolds ∷ Property p v ⇒ p → v → Maybe v+maybeHolds p v | holds p v = Just v+ | otherwise = Nothing+{-# INLINABLE maybeHolds #-}++-- | Wrap the value if the property holds.+check ∷ ∀ p v . Property p v ⇒ v → Maybe (Checked p v)+check v | holds (undefined ∷ p) v = Just (Checked v)+ | otherwise = Nothing+{-# INLINABLE check #-}+