type-level-natural-number (empty) → 1.0
raw patch · 4 files changed
+148/−0 lines, 4 filesdep +basesetup-changed
Dependencies added: base
Files
- LICENSE +22/−0
- Setup.hs +2/−0
- TypeLevel/NaturalNumber.hs +89/−0
- type-level-natural-number.cabal +35/−0
+ LICENSE view
@@ -0,0 +1,22 @@+Copyright (c) 2010, Gregory M. Crosswhite+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.++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
+ TypeLevel/NaturalNumber.hs view
@@ -0,0 +1,89 @@+{- Copyright (c) 2008, Scott E. Dillard. All rights reserved. -}+{- Copyright (c) 2010, Gregory M. Crosswhite. All rights reserved. -}++{-# LANGUAGE EmptyDataDecls #-}++module TypeLevel.NaturalNumber where++data Zero+data SuccessorTo n++type N0 = Zero+type N1 = SuccessorTo N0+type N2 = SuccessorTo N1+type N3 = SuccessorTo N2+type N4 = SuccessorTo N3+type N5 = SuccessorTo N4+type N6 = SuccessorTo N5+type N7 = SuccessorTo N6+type N8 = SuccessorTo N7+type N9 = SuccessorTo N8+type N10 = SuccessorTo N9+type N11 = SuccessorTo N10+type N12 = SuccessorTo N11+type N13 = SuccessorTo N12+type N14 = SuccessorTo N13+type N15 = SuccessorTo N14++type One = N1+type Two = N2+type Three = N3+type Four = N4+type Five = N5+type Six = N6+type Seven = N7+type Eight = N8+type Nine = N9+type Ten = N10+type Eleven = N11+type Twelve = N12+type Thirteen = N13+type Fourteen = N14+type Fifteen = N15++n0 :: N0 ; n0 = undefined+n1 :: N1 ; n1 = undefined+n2 :: N2 ; n2 = undefined+n3 :: N3 ; n3 = undefined+n4 :: N4 ; n4 = undefined+n5 :: N5 ; n5 = undefined+n6 :: N6 ; n6 = undefined+n7 :: N7 ; n7 = undefined+n8 :: N8 ; n8 = undefined+n9 :: N9 ; n9 = undefined+n10 :: N10 ; n10 = undefined+n11 :: N11 ; n11 = undefined+n12 :: N12 ; n12 = undefined+n13 :: N13 ; n13 = undefined+n14 :: N14 ; n14 = undefined+n15 :: N15 ; n15 = undefined++predecessorOf :: SuccessorTo n -> n+predecessorOf _ = undefined++successorTo :: n -> SuccessorTo n+successorTo _ = undefined++class NaturalNumber n where+ naturalNumberAsInt :: n -> Int+instance NaturalNumber Zero where+ naturalNumberAsInt _ = 0+instance NaturalNumber n => NaturalNumber (SuccessorTo n) where+ naturalNumberAsInt x = 1 + naturalNumberAsInt (predecessorOf x)++instance Show Zero where+ show _ = "0"+instance NaturalNumber n => Show (SuccessorTo n) where+ show = show . (+1) . naturalNumberAsInt . predecessorOf++instance Eq Zero where+ (==) _ _ = True+ (/=) _ _ = False+instance NaturalNumber n => Eq (SuccessorTo n) where+ (==) _ _ = True+ (/=) _ _ = False++instance Ord Zero where+ compare _ _ = EQ+instance NaturalNumber n => Ord (SuccessorTo n) where+ compare _ _ = EQ
+ type-level-natural-number.cabal view
@@ -0,0 +1,35 @@+Name: type-level-natural-number+Version: 1.0+License: BSD3+License-file: LICENSE+Author: Gregory Crosswhite+Maintainer: Gregory Crosswhite <gcross@phys.washington.edu>+Stability: Provisional+Synopsis: Simple, Haskell 2010-compatible type level natural numbers+Description: This is a simple, Haskell 2010 compatible implementation+ of type-level natural numbers. Operations requiring+ non-Haskell 2010 language extensions have been split+ into a separate package.++ The difference between this package and the many+ others on Hackage implementing type-level+ naturals is its emphasis on simplicity. It only+ supports non-negative natural numbers, and only+ the successor and predicessor operations. It+ represents natural numbers using a type-level+ linked list, so it is not intended to be used for+ representing large numbers. Pre-defined aliases+ for natural numbers up to 15 are provided.++ The code for this package was largely inspired by+ the type-level natural numbers in the excellent+ Vec package.++Cabal-version: >=1.6+Build-type: Simple+Category: Type System,Data++Library+ Build-depends: base >= 3 && < 5+ Exposed-modules: TypeLevel.NaturalNumber+ Extensions: EmptyDataDecls