bluespec.com Forum Index bluespec.com
Bluespec Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Defining a type function

 
Post new topic   Reply to topic    bluespec.com Forum Index -> Designing with BSV's Rules, Interfaces, ...
View previous topic :: View next topic  
Author Message
mehultikekar



Joined: 29 Oct 2011
Posts: 22

PostPosted: Mon Oct 03, 2016 4:14 pm    Post subject: Defining a type function Reply with quote

How do I define a type function (like SizeOf for example)? I need the largest size in an HList, so I defined a typeclass (similar to HLength):

Code:
typeclass HMaxSize#(type t, numeric type n)
   dependencies (t determines n);
endtypeclass

instance HMaxSize#(HNil, 0);
endinstance

instance HMaxSize#(HCons#(e, l), n1)
   provisos (HMaxSize#(l, n), Max#(n, SizeOf#(e), n1));
endinstance


But what I really need is a type function in order to define an interface like:

Code:
interface SomeIfc#(type t);
    method Bit#(HMaxSizeOf#(t)) value;
endinterface
Back to top
View user's profile Send private message
quark
Site Admin


Joined: 02 Nov 2007
Posts: 495

PostPosted: Mon Oct 03, 2016 4:34 pm    Post subject: Re: Defining a type function Reply with quote

BSC doesn't currently have a way to create a type function like you're asking for.

Also, unfortunately, BSC doesn't currently support provisos on interface types. Otherwise, you could write this:
Code:
interface SomeIfc#(type t) provisos(HMaxSizeOf#(t, sz));
    method Bit#(sz) value();
endinterface

At the moment, what you can do is to put the proviso on any module (or function) that returns this interface, and add an additional parameter to the interface itself:
Code:
interface SomeIfc#(type t, type sz);
    method Bit#(sz) value();
endinterface

module mkMod(SomeIfc#(t,sz) provisos(HMaxSizeOf#(t,sz));
   ...
endmodule
Back to top
View user's profile Send private message
mehultikekar



Joined: 29 Oct 2011
Posts: 22

PostPosted: Mon Oct 03, 2016 6:12 pm    Post subject: Reply with quote

But now the module that instantiates this module also needs to use this proviso:

Code:
module mkTopMod() provisos(HMaxSize#(HList#(...), sz));
    SomeIfc#(HList#(...), sz) inst <- mkMod;
endmodule


At which point, the type parameter t to the interface is basically redundant. I might as well do

Code:
interface SomeIfc#(numeric type sz)
    method Bit#(sz) value;
endinterface

module mkTopMod() provisos(HMaxSize#(HList#(...), sz));
    SomeIfc#(sz) inst <- mkMod;
endmodule



What I am ultimately trying to do is serialize many different types onto a 1-bit wire. So my interface is:

Code:
interface Serializer#(numeric type n);
    method Action bits_in(Bit#(n) bits, UInt#(TLog#(n)) num_bits);
    method Bit#(1) bit_out;
endinterface


So I need "n" to be the size of the largest type.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    bluespec.com Forum Index -> Designing with BSV's Rules, Interfaces, ... All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
bluespec.com topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP