Skip to content

Commit 7145ec6

Browse files
authored
Add a BuildList library
1 parent ad57f50 commit 7145ec6

8 files changed

+142
-0
lines changed

src/Libraries/Base3-Misc/BuildList.bs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package BuildList where
2+
3+
import List
4+
5+
-- A typeclass used to implement a vector construction function which can take
6+
-- any number of arguments (>0).
7+
-- The type parameter `a` is the type of the elements in the list.
8+
-- The type parameter `r` is the return type of the function, which can be a
9+
-- list (base case) or a function (recursive case) that takes another element
10+
-- and returns a new function.
11+
-- The list here is built in reverse for efficiency, and then reversed.
12+
class BuildList a r | r -> a where
13+
lst' :: List a -> a -> r
14+
15+
instance BuildList a (List a) where
16+
lst' l x = reverse $ x :> l
17+
18+
instance (BuildList a r) => BuildList a (a -> r) where
19+
lst' l x y = lst' (x :> l) y
20+
21+
-- Example usage:
22+
-- lst 1 2 3 4 5 => 1 :> 2 :> 3 :> 4 :> 5 :> nil
23+
-- lst False => False :> nil
24+
lst :: (BuildList a r) => a -> r
25+
lst x = lst' nil x

src/Libraries/Base3-Misc/Misc.bsv

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package Misc;
33
import Arbiter::*;
44
import BRAM::*;
55
import BRAMFIFO::*;
6+
import BuildList::*;
67
import BuildVector::*;
78
import BUtils::*;
89
import BypassReg::*;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
# Test functionality
3+
test_veri_only TestBuildList
4+
5+
# Test type check error messages
6+
compile_fail TestBuildListFail.bs
7+
compare_file TestBuildListFail.bs.bsc-out
8+

testsuite/bsc.lib/BuildList/Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# for "make clean" to work everywhere
2+
3+
CONFDIR = $(realpath ../..)
4+
5+
include $(CONFDIR)/clean.mk
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package TestBuildList where
2+
3+
import List
4+
import BuildList
5+
6+
{-# properties sysTestBuildList = { synthesize } #-}
7+
8+
sysTestBuildList :: Module Empty
9+
sysTestBuildList =
10+
module
11+
let
12+
v1 :: List Bool
13+
v1 = lst True False True True
14+
15+
v2 :: List (UInt 8);
16+
v2 = lst 7 32
17+
18+
v3 :: List Bool
19+
v3 = lst False True True
20+
21+
v4 :: List (UInt 4)
22+
v4 = lst 3
23+
24+
done :: Reg Bool <- mkReg False
25+
26+
rules
27+
"r": when not done
28+
==> action
29+
$display "v1[0] -> %b" (v1!!0)
30+
$display "v1[1] -> %b" (v1!!1)
31+
$display "v1[2] -> %b" (v1!!2)
32+
$display "v1[3] -> %b" (v1!!3)
33+
$display "v2[0] -> %d" (v2!!0)
34+
$display "v2[1] -> %d" (v2!!1)
35+
$display "v3[0] -> %b" (v3!!0)
36+
$display "v3[1] -> %b" (v3!!1)
37+
$display "v3[2] -> %b" (v3!!2)
38+
$display "v4[0] -> %d" (v4!!0)
39+
done := True
40+
41+
"r2": when done
42+
==> $finish 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package TestBuildListFail where
2+
3+
import List
4+
import BuildList
5+
6+
-- Test error messages
7+
8+
-- Wrong element type, Literal
9+
fn1 :: Bool
10+
fn1 =
11+
let v :: List Bool
12+
v = lst 0 1 2
13+
in v == v
14+
15+
-- Wrong element type, concrete
16+
fn2 :: Bool
17+
fn2 =
18+
let v :: List Integer
19+
v = lst True False True
20+
in v == v
21+
22+
-- Wrong return type
23+
fn3 :: Bool
24+
fn3 =
25+
let v :: Bit 3
26+
v = lst True False True
27+
in v == v
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
checking package dependencies
2+
compiling TestBuildListFail.bs
3+
Error: "TestBuildListFail.bs", line 12, column 13: (T0031)
4+
The contexts for this expression could not be resolved because there are no
5+
instances of the form:
6+
Prelude.Literal Prelude.Bool
7+
The context was implied by expressions at the following positions:
8+
"TestBuildListFail.bs", line 12, column 17
9+
Error: "TestBuildListFail.bs", line 18, column 8: (T0032)
10+
This expression requires the following context which could not be resolved:
11+
BuildList.BuildList Prelude.Bool (Prelude.Bool -> Prelude.Bool -> Prelude.List Prelude.Integer)
12+
The context was implied by expressions at the following positions:
13+
"TestBuildListFail.bs", line 19, column 13
14+
An instance for this context exists, but it depends on the following context
15+
for which there is no instance:
16+
BuildList.BuildList Prelude.Bool (Prelude.List Prelude.Integer)
17+
Error: "TestBuildListFail.bs", line 25, column 8: (T0032)
18+
This expression requires the following context which could not be resolved:
19+
BuildList.BuildList Prelude.Bool (Prelude.Bool -> Prelude.Bool -> Prelude.Bit 3)
20+
The context was implied by expressions at the following positions:
21+
"TestBuildListFail.bs", line 26, column 13
22+
An instance for this context exists, but it depends on the following context
23+
for which there is no instance:
24+
BuildList.BuildList Prelude.Bool (Prelude.Bit 3)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
v1[0] -> 1
2+
v1[1] -> 0
3+
v1[2] -> 1
4+
v1[3] -> 1
5+
v2[0] -> 7
6+
v2[1] -> 32
7+
v3[0] -> 0
8+
v3[1] -> 1
9+
v3[2] -> 1
10+
v4[0] -> 3

0 commit comments

Comments
 (0)