Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a BuildList utility #723

Merged
merged 4 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Libraries/Base3-Misc/BuildList.bs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package BuildList where

import List

-- A typeclass used to implement a vector construction function which can take
-- any number of arguments (>0).
-- The type parameter `a` is the type of the elements in the list.
-- The type parameter `r` is the return type of the function, which can be a
-- list (base case) or a function (recursive case) that takes another element
-- and returns a new function.
-- The list here is built in reverse for efficiency, and then reversed.
class BuildList a r | r -> a where
lst' :: List a -> a -> r

instance BuildList a (List a) where
lst' l x = reverse $ x :> l

instance (BuildList a r) => BuildList a (a -> r) where
lst' l x y = lst' (x :> l) y

-- Example usage:
-- lst 1 2 3 4 5 => 1 :> 2 :> 3 :> 4 :> 5 :> nil
-- lst False => False :> nil
lst :: (BuildList a r) => a -> r
lst x = lst' nil x
1 change: 1 addition & 0 deletions src/Libraries/Base3-Misc/Misc.bsv
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package Misc;
import Arbiter::*;
import BRAM::*;
import BRAMFIFO::*;
import BuildList::*;
import BuildVector::*;
import BUtils::*;
import BypassReg::*;
Expand Down
8 changes: 8 additions & 0 deletions testsuite/bsc.lib/BuildList/BuildList.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

# Test functionality
test_veri_only TestBuildList

# Test type check error messages
compile_fail TestBuildListFail.bs
compare_file TestBuildListFail.bs.bsc-out

5 changes: 5 additions & 0 deletions testsuite/bsc.lib/BuildList/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# for "make clean" to work everywhere

CONFDIR = $(realpath ../..)

include $(CONFDIR)/clean.mk
42 changes: 42 additions & 0 deletions testsuite/bsc.lib/BuildList/TestBuildList.bs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package TestBuildList where

import List
import BuildList

{-# properties sysTestBuildList = { synthesize } #-}

sysTestBuildList :: Module Empty
sysTestBuildList =
module
let
v1 :: List Bool
v1 = lst True False True True

v2 :: List (UInt 8);
v2 = lst 7 32

v3 :: List Bool
v3 = lst False True True

v4 :: List (UInt 4)
v4 = lst 3

done :: Reg Bool <- mkReg False

rules
"r": when not done
==> action
$display "v1[0] -> %b" (v1!!0)
$display "v1[1] -> %b" (v1!!1)
$display "v1[2] -> %b" (v1!!2)
$display "v1[3] -> %b" (v1!!3)
$display "v2[0] -> %d" (v2!!0)
$display "v2[1] -> %d" (v2!!1)
$display "v3[0] -> %b" (v3!!0)
$display "v3[1] -> %b" (v3!!1)
$display "v3[2] -> %b" (v3!!2)
$display "v4[0] -> %d" (v4!!0)
done := True

"r2": when done
==> $finish 0
27 changes: 27 additions & 0 deletions testsuite/bsc.lib/BuildList/TestBuildListFail.bs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package TestBuildListFail where

import List
import BuildList

-- Test error messages

-- Wrong element type, Literal
fn1 :: Bool
fn1 =
let v :: List Bool
v = lst 0 1 2
in v == v

-- Wrong element type, concrete
fn2 :: Bool
fn2 =
let v :: List Integer
v = lst True False True
in v == v

-- Wrong return type
fn3 :: Bool
fn3 =
let v :: Bit 3
v = lst True False True
in v == v
24 changes: 24 additions & 0 deletions testsuite/bsc.lib/BuildList/TestBuildListFail.bs.bsc-out.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
checking package dependencies
compiling TestBuildListFail.bs
Error: "TestBuildListFail.bs", line 12, column 13: (T0031)
The contexts for this expression could not be resolved because there are no
instances of the form:
Prelude.Literal Prelude.Bool
The context was implied by expressions at the following positions:
"TestBuildListFail.bs", line 12, column 17
Error: "TestBuildListFail.bs", line 18, column 8: (T0032)
This expression requires the following context which could not be resolved:
BuildList.BuildList Prelude.Bool (Prelude.Bool -> Prelude.Bool -> Prelude.List Prelude.Integer)
The context was implied by expressions at the following positions:
"TestBuildListFail.bs", line 19, column 13
An instance for this context exists, but it depends on the following context
for which there is no instance:
BuildList.BuildList Prelude.Bool (Prelude.List Prelude.Integer)
Error: "TestBuildListFail.bs", line 25, column 8: (T0032)
This expression requires the following context which could not be resolved:
BuildList.BuildList Prelude.Bool (Prelude.Bool -> Prelude.Bool -> Prelude.Bit 3)
The context was implied by expressions at the following positions:
"TestBuildListFail.bs", line 26, column 13
An instance for this context exists, but it depends on the following context
for which there is no instance:
BuildList.BuildList Prelude.Bool (Prelude.Bit 3)
10 changes: 10 additions & 0 deletions testsuite/bsc.lib/BuildList/sysTestBuildList.out.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
v1[0] -> 1
v1[1] -> 0
v1[2] -> 1
v1[3] -> 1
v2[0] -> 7
v2[1] -> 32
v3[0] -> 0
v3[1] -> 1
v3[2] -> 1
v4[0] -> 3
Loading