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 

Pattern Matching with Maybe Type

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



Joined: 14 Aug 2007
Posts: 41

PostPosted: Tue Sep 04, 2012 8:56 am    Post subject: Pattern Matching with Maybe Type Reply with quote

What are some clever alternatives, perhaps more concise and/or using pattern matching to testing a Maybe type for validity than what we've done here
Code:

  Reg#(Maybe#(EtherType))    etyp        <- tagged Invalid;
...
  Bool isDcpType = isValid(etyp) && (fromMaybe(?,etyp)==16'hF040);


I know one could write a function accepting a Maybe type and the object to compare against and return a Bool True when it is Valid and matches; but I also think there are other ways.
Back to top
View user's profile Send private message
jnewbern



Joined: 18 Jul 2007
Posts: 71

PostPosted: Tue Sep 04, 2012 10:27 am    Post subject: Reply with quote

Shep,

What about:

Code:
Bool isDcpType = etyp == tagged Valid 16'hF040;


or possibly:

Code:
Maybe#(EtherType) dcpType = tagged Valid 16'hF040;


and then just use
Code:
x == dcpType
instead of
Code:
isDcpType(x)


The function is better if you want to pass it as a predicate to other functions, but the dcpType value is nicer if you want to use the same term when assigning to etyp, etc.

Jeff
Back to top
View user's profile Send private message
ShepSiegel



Joined: 14 Aug 2007
Posts: 41

PostPosted: Tue Sep 04, 2012 11:30 am    Post subject: Reply with quote

That's pretty concise, thanks Jeff.
Back to top
View user's profile Send private message
quark
Site Admin


Joined: 02 Nov 2007
Posts: 500

PostPosted: Tue Sep 04, 2012 12:03 pm    Post subject: Reply with quote

Jeff's suggestions are best, but for completeness note that you can also do this:
Code:
Bool isDcpType = (etyp matches tagged Valid .v ? (v == 16'hF040) : False);
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