OneTuple (empty) → 0.1.0
raw patch · 5 files changed
+153/−0 lines, 5 filesdep +basesetup-changed
Dependencies added: base
Files
- Data/Tuple/OneTuple.hs +73/−0
- FAQ +27/−0
- LICENSE +34/−0
- OneTuple.cabal +15/−0
- Setup.hs +4/−0
+ Data/Tuple/OneTuple.hs view
@@ -0,0 +1,73 @@++-- |OneTuple fills the /tuple gap/ with a singleton tuple.+-- +-- OneTuple /does not support/ the usual parenthesized tuple syntax.+-- +-- OneTuple+--+-- * has the expected laziness properties+--+-- * can be pattern-matched+--+-- * ships with instances for several standard type classes,+-- including all those supported by H98-standard tuples+--+-- * requires no language extensions, except for hierarchical modules++module Data.Tuple.OneTuple (OneTuple(OneTuple), only) where++import Control.Applicative+import Control.Monad+import Data.Foldable+import Data.Ix+import Data.Monoid+import Data.Traversable++-- |OneTuple is the singleton tuple data type.+data OneTuple a+ = OneTuple a -- ^ singleton tuple constructor+ deriving (Eq,Ord,Bounded,Show,Read)++-- |The 'only' function extracts the OneTuple's only member.+-- (Compare to 'fst' and 'snd'.)+only :: OneTuple a -- ^ takes a singleton tuple argument+ -> a -- ^ returns the only element in the tuple+only (OneTuple x) = x++instance (Enum a) => Enum (OneTuple a) where+ succ = liftA succ+ pred = liftA pred+ toEnum = pure . toEnum+ fromEnum (OneTuple x) = fromEnum x++instance (Ix a) => Ix (OneTuple a) where+ range (OneTuple x, OneTuple y) = map OneTuple (range (x,y))+ index (OneTuple x, OneTuple y) (OneTuple z) = index (x,y) z+ inRange (OneTuple x, OneTuple y) (OneTuple z) = inRange (x,y) z++instance Foldable OneTuple where+ fold (OneTuple m) = m+ foldMap f (OneTuple x) = f x+ foldr f b (OneTuple x) = f x b+ foldl f a (OneTuple x) = f a x+ foldr1 _f (OneTuple x) = x+ foldl1 _f (OneTuple x) = x++instance Traversable OneTuple++instance Functor OneTuple where+ fmap f (OneTuple x) = OneTuple (f x)++instance Applicative OneTuple where+ pure = return+ (<*>) = ap++instance Monad OneTuple where+ (OneTuple x) >>= f = f x+ return = OneTuple++instance (Monoid a) => Monoid (OneTuple a) where+ mempty = OneTuple mempty+ mappend (OneTuple x) (OneTuple y) = OneTuple (mappend x y)+ mconcat = Prelude.foldr1 mappend+
+ FAQ view
@@ -0,0 +1,27 @@++This is a list of Frequently Asked Questions about OneTuple, the+optional Haskell singleton tuple type.++Some of these are, of course, merely anticipated FAQs.++Q 1. What's the point, if you can't support the same syntax as+ 'normal' tuples?+A 1. You can hack it up in TH if you want -- patches are welcome!+ Simplicity and (near) H98 compatibility were goals in building+ OneTuple.++Q 2. Why define 'data OneTuple' instead of just a type alias?+A 2. For laziness and pattern matching similar to other tuples.++Q 3. Isn't this the same as <something> ?+A 3. Could be, but this one's sold as a tuple!++Q 4. Singleton tuples don't make sense! They're isomorphic to their+ component type!+A 4. Yes, that's true, up to laziness.++Q 5. What's NST?+A 5. "Need Singleton Tuple". It's the class of problems that can't+ be solved without using a singleton tuple. Also conveniently+ stands for "No Such Thing".+
+ LICENSE view
@@ -0,0 +1,34 @@++Copyright (c) 2008, John A. Dorsey.+All rights reserved.++Redistribution and use of this software 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 John Dorsey nor the names of its+ 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.+
+ OneTuple.cabal view
@@ -0,0 +1,15 @@+Name: OneTuple+Version: 0.1.0+Synopsis: OneTuple, the Singleton Tuple+Description: OneTuple, the Singleton Tuple+Copyright: (c) John Dorsey 2008+License: BSD3+License-file: LICENSE+Author: John Dorsey <haskell@colquitt.org>+Maintainer: John Dorsey <haskell@colquitt.org>+Cabal-version: > 1.4+Stability: experimental+Build-type: Simple+Build-depends: base+Exposed-Modules: Data.Tuple.OneTuple+
+ Setup.hs view
@@ -0,0 +1,4 @@+ +import Distribution.Simple+main = defaultMain+