Last active
January 2, 2019 10:07
-
-
Save merijn/39dc86e345e87276c523 to your computer and use it in GitHub Desktop.
Index list in haskell
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 ConstraintKinds #-} | |
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE GADTs #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE TypeOperators #-} | |
import GHC.Prim (Constraint) | |
data Nat = Succ Nat | Zero | |
data Vec n a where | |
VNil :: Vec Zero a | |
VCons :: a -> Vec n a -> Vec (Succ n) a | |
data Index n where | |
IZero :: Index Zero | |
ISucc :: Index n -> Index (Succ n) | |
type family LessThan (x :: Nat) (y :: Nat) :: Constraint where | |
LessThan Zero (Succ x) = () | |
LessThan (Succ a) (Succ b) = LessThan a b | |
LessThan (Succ a) x = ("Error" ~ "Inequality doesn't hold") | |
index :: (LessThan x n) => Vec n a -> Index x -> a | |
index (VCons x _) IZero = x | |
index (VCons _ xs) (ISucc n) = index xs n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment