Created
October 12, 2017 20:39
-
-
Save CarstenKoenig/3b9057e10039a990cf4a7a9bf18548f6 to your computer and use it in GitHub Desktop.
some type-level stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE PartialTypeSignatures #-} | |
{-# LANGUAGE KindSignatures #-} | |
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE GADTs #-} | |
module FinVec where | |
-- simple representation of naturals | |
-- DataKinds lifts this to the type-level | |
data Nat = Z | S Nat | |
-- a FinVec is a list with maximal n items that currently has i items | |
data FinVec (n :: Nat) (i :: Nat) a where | |
Empty :: FinVec n Z a | |
Cons :: a -> FinVec n i a -> FinVec (S n) (S i) a | |
instance Show a => Show (FinVec n i a) where | |
show Empty = "[]" | |
show (Cons x xs) = show x ++ " : " ++ show xs | |
---------------------------------------------------------------------- | |
-- Examples : first one type checks the secons wont | |
type Three = S (S (S Z)) | |
-- 3 Elements in a List limited to 3max: | |
threeOfThree :: FinVec Three _ Int | |
threeOfThree = 1 `Cons` (2 `Cons` (3 `Cons` Empty)) | |
-- one more and it won't TypeCheck | |
fourOfThree :: FinVec Three _ Int | |
fourOfThree = 0 `Cons` threeOfThree |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment