lude

Haskell prelude alternative
git clone git://git.rr3.xyz/lude | git clone gits://git.rr3.xyz/lude
Log | Files | Refs | README | LICENSE

Natural.hs (654B)


      1 module Lude.Haskell.Natural (
      2     Natural,
      3     pattern NaturalZero,
      4     pattern NaturalSucc,
      5     naturalPred,
      6 ) where
      7 
      8 import Numeric.Natural (Natural)
      9 import Prelude (Maybe (..), fromInteger, (+), (-), (==))
     10 
     11 --------------------------------------------------------------------------------
     12 
     13 pattern NaturalZero :: Natural
     14 pattern NaturalZero <- (naturalPred -> Nothing) where NaturalZero = 0
     15 pattern NaturalSucc :: Natural -> Natural
     16 pattern NaturalSucc n <- (naturalPred -> Just n) where NaturalSucc n = n + 1
     17 {-# COMPLETE NaturalZero, NaturalSucc #-}
     18 
     19 naturalPred :: Natural -> Maybe Natural
     20 naturalPred = \case
     21     0 -> Nothing
     22     n -> Just (n - 1)