add finite sets

This commit is contained in:
rnhmjoj 2016-11-28 19:25:10 +01:00
parent 1123ebe83b
commit 4cc8bc1f24
No known key found for this signature in database
GPG Key ID: 362BB82B7E496B7C

46
src/Data/Fin.hs Normal file
View File

@ -0,0 +1,46 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeInType #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE UnicodeSyntax #-}
-- | Finite types
module Data.Fin where
import Data.Singletons.TH
import Data.TypeClass (Eq, Show)
import Data.Kind (Type)
import Data.Nat
-- | Fin n is a type with n inhabitants
data Fin Type where
Zero Fin (S n) -- ^ Type with no inhabitants
Suc Fin n Fin (S n) -- ^ Suc construct a type with n+1 inhabitants
deriving instance Eq (Fin n)
deriving instance Show (Fin n)
-- * Conversions
-- | Convert a finite set to a natural
to Fin n
to Zero = Z
to (Suc n) = S (to n)
-- | Convert a natural to a finite set
from S n Fin (S n)
from SZ = Zero
from (SS n) = Suc (from n)