Skip to content

Commit c3d3c2e

Browse files
committed
Add SS' :: SNat n -> SNat (S n)
1 parent 4cfb8b1 commit c3d3c2e

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

fin/ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Version history for fin
22

3+
## 0.3.2
4+
5+
- Add `SS' :: SNat n -> SNat (S n)`, pattern synonym with explicit argument.
6+
37
## 0.3.1
48

59
- Support GHC-8.6.5...9.10.1

fin/fin.cabal

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
cabal-version: 2.2
22
name: fin
3-
version: 0.3.1
4-
x-revision: 1
3+
version: 0.3.2
54
synopsis: Nat and Fin: peano naturals and finite numbers
65
category: Data, Dependent Types, Singletons, Math
76
description:

fin/src/Data/Type/Nat.hs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
{-# LANGUAGE EmptyCase #-}
44
{-# LANGUAGE GADTs #-}
55
{-# LANGUAGE KindSignatures #-}
6+
{-# LANGUAGE PatternSynonyms #-}
67
{-# LANGUAGE RankNTypes #-}
78
{-# LANGUAGE ScopedTypeVariables #-}
89
{-# LANGUAGE StandaloneDeriving #-}
910
{-# LANGUAGE Trustworthy #-}
1011
{-# LANGUAGE TypeFamilies #-}
1112
{-# LANGUAGE TypeOperators #-}
1213
{-# LANGUAGE UndecidableInstances #-}
14+
{-# LANGUAGE ViewPatterns #-}
1315
-- | 'Nat' numbers. @DataKinds@ stuff.
1416
--
1517
-- This module re-exports "Data.Nat", and adds type-level things.
@@ -23,7 +25,7 @@ module Data.Type.Nat (
2325
explicitShow,
2426
explicitShowsPrec,
2527
-- * Singleton
26-
SNat(..),
28+
SNat(SZ,SS,SS'),
2729
snatToNat,
2830
snatToNatural,
2931
-- * Implicit
@@ -177,6 +179,35 @@ snatToNatural :: forall n. SNat n -> Natural
177179
snatToNatural SZ = 0
178180
snatToNatural SS = unKonst (induction (Konst 0) (kmap succ) :: Konst Natural n)
179181

182+
-------------------------------------------------------------------------------
183+
-- Explicit constructor
184+
-------------------------------------------------------------------------------
185+
186+
data SNat_ (n :: Nat) where
187+
SZ_ :: SNat_ 'Z
188+
SS_ :: SNat n -> SNat_ ('S n)
189+
190+
snat_ :: SNat n -> SNat_ n
191+
snat_ SZ = SZ_
192+
snat_ SS = SS_ snat
193+
194+
-- | A pattern with explicit argument
195+
--
196+
-- >>> let predSNat :: SNat (S n) -> SNat n; predSNat (SS' n) = n
197+
-- >>> predSNat (SS' (SS' SZ))
198+
-- SS
199+
--
200+
-- >>> reflect $ predSNat (SS' (SS' SZ))
201+
-- 1
202+
--
203+
--
204+
-- @since 0.3.2
205+
pattern SS' :: () => (m ~ 'S n) => SNat n -> SNat m
206+
pattern SS' n <- (snat_ -> SS_ n)
207+
where SS' n = withSNat n SS
208+
209+
{-# COMPLETE SZ, SS' #-}
210+
180211
-------------------------------------------------------------------------------
181212
-- Equality
182213
-------------------------------------------------------------------------------

0 commit comments

Comments
 (0)