Skip to content

Commit 6e59931

Browse files
committed
dependent type example
1 parent aa5ed5e commit 6e59931

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

examples/DependentSafeHead.gr

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- A dependent-type like example in Granule, using GADTs to capture a
2+
-- relation-like constraint, with an explicit proof witness
3+
data Vec (n : Nat) (a : Type) where
4+
Nil : Vec 0 a;
5+
Cons : forall {n : Nat} . a -> Vec n a -> Vec (n+1) a
6+
7+
data UseOrNot (n : Nat) (k : Nat) where
8+
Use : UseOrNot 0 1;
9+
Not : forall {m : Nat} . UseOrNot (m+1) 0
10+
11+
safeHead : forall {a : Type, n k : Nat} . Vec n a -> UseOrNot n k -> a [k] -> (a, Vec (n - 1) a)
12+
safeHead Nil Use [y] = (y, Nil);
13+
safeHead (Cons x xs) Not [_] = (x, xs)

0 commit comments

Comments
 (0)