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 

Error with nested tuples

 
Post new topic   Reply to topic    bluespec.com Forum Index -> Tools: BSC (Bluespec Compiler)
View previous topic :: View next topic  
Author Message
mehultikekar



Joined: 29 Oct 2011
Posts: 22

PostPosted: Tue Oct 29, 2013 9:32 pm    Post subject: Error with nested tuples Reply with quote

I have two typedefs in my code -
Code:

typedef Tuple2#(A, B) AB;
typedef Tuple2#(C, AB) C_AB;


and I was getting type errors on
Code:

C_AB x = ...;
let y = tpl_2(x);


because I was expecting y's type to be AB whereas the compiler seemed to use C_AB as Tuple3#(C, A, B) and inferred y's type as A.

I fixed it by specifying y's type, but perhaps the compiler could print a warning for those who don't know about the type equivalence in tuples.
Back to top
View user's profile Send private message
quark
Site Admin


Joined: 02 Nov 2007
Posts: 496

PostPosted: Thu Oct 31, 2013 1:19 pm    Post subject: Re: Error with nested tuples Reply with quote

Ah, yes, this is a "feature" of tuples that occurs because tuples are implemented as chains of pairs. The type:
Code:
Tuple4#(a,b,c,d)
is really just an alias for:
Code:
Tuple2#(a,Tuple2#(b,Tuple2#(c,d)))

The function "tpl_2" is an overloaded typeclass function. When you use it, BSC has to figure out which version you intended, and as you observed, for Tuple2#(a,Tuple2#(b,c)) there are two possible versions -- the one for Tuple2 and the one for Tuple3. When multiple instances are possible, BSC picks the "most specific instance", which is the version for Tuple3. By providing more type information, you are removing the ambiguity so that only one version is appropriate, and BSC uses that one.

This only happens when the last element of the tuple is another tuple. There's no ambiguity when nesting tuples in the middle of other tuples. For instance, like this:
Code:
Tuple2#(Tuple2#(a,b), c)
This is not the same as a larger tuple, so applying "tpl_1" will always return the first element, which is Tuple2#(a,b).

Anyway, yes, we'll see about documenting this better. We could also change the implementation to not expose this feature; however, the advantage of implementing tuples as pairs is that you can define one typeclass instance for Tuple2 and it can recursively work for all tuples, without having to manually write an instance for every size.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    bluespec.com Forum Index -> Tools: BSC (Bluespec Compiler) 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