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 

case with nested tagged unions

 
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: Wed Nov 18, 2015 2:55 pm    Post subject: case with nested tagged unions Reply with quote

Could someone tell me how I should be writing the following code to make it legal?

Code:
typedef union tagged {
   FooA Integer;
} Foo;

function Bool bar(Maybe#(Foo) baz) =
   case (baz) matches
      tagged Invalid: True;
      tagged Valid FooA .x: False;
   endcase;


The compiler complains about the second case target, specifically the ".x" term, with the message "Unexpected `.'; expected `&&&' or `:'".

On a related note, what are the constraints on when to use the "tagged" label and when is it ok to omit? It would be great to hear about a technique that allows pattern matching over tagged unions (especially nested ones) without them being littered with "tagged" boilerplate everywhere...
Back to top
View user's profile Send private message
tspalink



Joined: 05 Nov 2015
Posts: 12

PostPosted: Wed Nov 18, 2015 5:15 pm    Post subject: Reply with quote

The following looks to work:

Code:
typedef union tagged {
   void FooA;
   Integer FooB;
} Foo;

function Bool bar(Maybe#(Foo) baz) =
   case (baz) matches
      tagged Invalid: True;
      tagged Valid FooA: True;
      tagged Valid (tagged FooB .x): False;
   endcase;


Note that I got the type ordering wrong in my typedef example as well.

Which leaves only my second question -- it looks like you can sometimes leave off the "tagged" for RHS values, but maybe never on the LHS of a pattern match?
Back to top
View user's profile Send private message
quark
Site Admin


Joined: 02 Nov 2007
Posts: 500

PostPosted: Tue Nov 24, 2015 4:27 pm    Post subject: Reply with quote

Always use 'tagged'.

It is indeed a 'feature' of BSC that it will allow you to omit 'tagged' when creating a value. RHS vs LHS isn't the way to think about it; it's pattern vs value. The reason this works:
Code:
case (baz) matches
  tagged Valid FooA: True;
is because 'FooA' is a value, on the LHS. It's like writing this:
Code:
case (x) matches
  tagged Valid 17: ...;
where 17 is an ordinary value (inside a pattern). You've just replaced 17 with 'FooA'. But I recommend always writing 'tagged FooA' and not relying on this 'feature' of BSC.
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