Skip to content

Commit

Permalink
Add a BuildList utility
Browse files Browse the repository at this point in the history
  • Loading branch information
krame505 committed Jul 22, 2024
1 parent e721480 commit 4836f16
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
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

0 comments on commit 4836f16

Please sign in to comment.