Skip to content

Commit 92e1151

Browse files
committed
[ re #155 ] Add vecToList + vector reverse examples to CompileTo.agda
1 parent 16fe7b2 commit 92e1151

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

test/CompileTo.agda

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@ listToVec (x ∷ xs) = x ∷ listToVec xs
2323

2424
{-# COMPILE AGDA2HS listToVec transparent #-}
2525

26+
vecToList : Vec a n List a
27+
vecToList [] = []
28+
vecToList (x ∷ xs) = x ∷ vecToList xs
29+
30+
{-# COMPILE AGDA2HS vecToList transparent #-}
31+
2632
test2 : Vec Int 3
27-
test2 = listToVec (map (_+ 1) (123 ∷ []))
33+
test2 = listToVec (map (_+ 1) (vecToList (123 ∷ [])))
2834

2935
{-# COMPILE AGDA2HS test2 #-}
3036

37+
-- Not using `length` since compile-to is not yet compatible with type classes
3138
llength : List a Nat
3239
llength [] = 0
3340
llength (x ∷ xs) = 1 + llength xs
@@ -46,3 +53,20 @@ test3 : ∃ Nat (_≡ 3)
4653
test3 = vlength test1
4754

4855
{-# COMPILE AGDA2HS test3 #-}
56+
57+
lfoldl : (b a b) b List a b
58+
lfoldl f v [] = v
59+
lfoldl f v (x ∷ xs) = lfoldl f (f v x) xs
60+
61+
{-# COMPILE AGDA2HS lfoldl #-}
62+
63+
vfoldl : (b : @0 Nat Set) ( {@0 n} b n a b (suc n)) b 0 Vec a n b n
64+
vfoldl b f v [] = v
65+
vfoldl b f v (x ∷ xs) = vfoldl (λ n b (suc n)) f (f v x) xs
66+
67+
{-# COMPILE AGDA2HS vfoldl to lfoldl #-}
68+
69+
vreverse : Vec a n Vec a n
70+
vreverse {a = a} = vfoldl (Vec a) (λ xs x x ∷ xs) []
71+
72+
{-# COMPILE AGDA2HS vreverse #-}

test/golden/CompileTo.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@ llength (x : xs) = 1 + llength xs
1515
test3 :: Natural
1616
test3 = llength test1
1717

18+
lfoldl :: (b -> a -> b) -> b -> [a] -> b
19+
lfoldl f v [] = v
20+
lfoldl f v (x : xs) = lfoldl f (f v x) xs
21+
22+
vreverse :: [a] -> [a]
23+
vreverse = lfoldl (\ xs x -> x : xs) []
24+

0 commit comments

Comments
 (0)