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 

non-monadic returns from a monadic 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
tspalink



Joined: 05 Nov 2015
Posts: 12

PostPosted: Thu Nov 12, 2015 2:27 pm    Post subject: non-monadic returns from a monadic function Reply with quote

Is there a more elegant way of writing this?

Code:
function ActionValue#(Bool) foo(Bool cond, Get#(Bool) bar) =
   (cond ? bar.get() : actionvalue return False; endactionvalue);


I am seeing this pattern (of needing to promote a non-monadic return value in a monadic function) a few times in my code and would like to find a more readable representation (ie, less boilerplate)...
Back to top
View user's profile Send private message
quark
Site Admin


Joined: 02 Nov 2007
Posts: 500

PostPosted: Thu Nov 12, 2015 2:46 pm    Post subject: Re: non-monadic returns from a monadic function Reply with quote

Unfortunately, the Monad typeclass's member 'return' has the same name as the keyword, so the parser isn't allowing you to refer to it (except in certain contexts that it knows about -- module, actionvalue). You can bypass the parser by escaping variable names, with a backslash. So this code works:
Code:
function ActionValue#(Bool) foo3(Bool cond, Get#(Bool) bar) =
   (cond ? bar.get() : \return (False));

Every non-space character after the backslash is part of the escaped name, so you need to make sure to put a space after the name and before the function parentheses (which are needed, since this is an ordinary function call). If you want to avoid escaped names, you can give an alternate name to Monad 'return':
Code:
function m#(t) returnM(t x) provisos (Monad#(m)) = \return (x);

This 'returnM' function could probably be added to the BSC Prelude, or the members of Monad could be renamed in the Prelude to not conflict with keywords.
Back to top
View user's profile Send private message
tspalink



Joined: 05 Nov 2015
Posts: 12

PostPosted: Thu Nov 12, 2015 2:55 pm    Post subject: Reply with quote

Guess I should have thought about this more. To answer my own question, it looks like the following seems to be what I want:

Code:
function m#(a) lift(a x) provisos(Monad#(m));
   actionvalue return x; endactionvalue
endfunction

function ActionValue#(Bool) foo(Bool cond, Get#(Bool) bar) =
   (cond ? bar.get() : lift(False));


Where "lift" should actually be called "return" and ... apparently already is?

It looks like the following also works:

Code:
function ActionValue#(Bool) foo(Bool cond, Get#(Bool) bar) =
   (cond ? bar.get() : \return(False));


Does this all sound reasonable?
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