Saturday, February 02, 2008

What's an F# Tuple?


It's hardly a difficult question, but it's taken me a while to arrive at an answer. Reflector has been invaluable to me, and I can't recommend it strongly enough to anyone who wants or needs to understand what the F# compiler does. One often hears that functional programming more readily lets one find the proper level of abstraction, but sometimes one has to look at the IL, perhaps as a bridge to C# (the "canonical"
.NET language), to improve.


Let's say I define these tuple types:


type StringInt = string * int
type StringDouble = string * double



If I disassemble the resulting DLL, I find...nothing at all! There's no trace in the IL of these tuple types, or anything else:
public static void main()
{
}



Let me add some functions that operate on this code:
let get_int (t : StringInt) =
let _, n = t in n

let get_float (t : StringFloat) =
let _, f = t in f


Something interesting now shows up in Reflector's tree view of the
assembly:


main() then looks like this (screenshots made using Cropper):

I can't claim that all of this is comprehensible to me, but one thing is obvious: the compiled IL has no trace of the named tuple types. They're like structs insofar as they're equal when they have equal structure. Tuple types with the same structure are really the same type. They are not implemented as structs (presumably to avoid having to copy them to the stack on every recursion), but they're implemented as classes. Sealed classes, mind you:



The disassembled get_int() function looks like this; I don't know why a new instance is being allocated from the heap:
Interestingly, if I change my code thus, I can no longer call get_int() and get_float() with the resulting values, because the "discrimated union" Tuples is implemented as a class, rather like a C-style union with type tag and some smarter operations (the disassembled IL is much too verbose to reproduce here, but I recommend trying it):


type Tuples =
| One of StringInt
| Two of StringFloat

let si = One ("Carlo", 6)
let sf = Two ("Leo", 1.5)

Console.WriteLine (si)
Console.WriteLine (sf)


Note that I don't have to specify the tuple type; the compiler infers if from the tag "One" or "Two". What I've learned from Don Syme's Expert F# is that the tag most recently put in scope is the one that the compiler tries to use. Then the disassembled code looks like this, meaning that you can no longer get at the tuple except through the tag:



30 comments:

Anonymous said...

Howdy! I'm at work browsing your blog from my new iphone 4! Just wanted to say I love reading your blog and look forward to all your posts! Carry on the outstanding work!
my web site :: crushing cogs tower gameplay

Anonymous said...

I blog quite often and I genuinely thank you for your information.
The article has really peaked my interest.
I'm going to book mark your website and keep checking for new information about once per week. I opted in for your Feed as well.
Here is my web site - Financial Advisor

Anonymous said...

If your choice for the evening is a pair of Anne Klein gold dress sandals, a matching handbag is a definite must. [url=http://www.vanessasac.com]vanessa bruno sacs[/url] achat sac vanessa bruno
[url=http://www.canadagoosessoldeparis.fr]http://www.canadagoosessoldeparis.fr[/url] For example, IKEA are charging five pence for carrier bags, following the success of the bag tax in Ireland.
[url=http://www.parkaoutelt.ca]canada goose toronto[/url] No cloudiness, coloration or even a hint of softening was present to impair us from enjoying accurate reproduction of vocals or instrumentation. [url=http://www.beatsbydrdresoldesfrance.com]beats pas cher[/url]

Anonymous said...

In fact, most of the day they are confined within limited spaces, where they are expected to be productive, cooperative, inventive and frie . [url=http://www.vanessasac.com]Sac vanessa bruno[/url] sac a main vanessa bruno
[url=http://www.canadagoosessoldeparis.fr]canada goose banff park[/url] Women are as various and distinctive in their model and look as their choices of purses and handbags.
[url=http://www.parkaoutelt.ca]canada goose jackets[/url] nevermind.). [url=http://www.pandorajeweluk.co.uk]pandora jewellery[/url]

Anonymous said...

These smooth, undetectable belts with a flat clasp provide an instant slimming effect, so you look finished and fabulous all day.. [url=http://www.vanessasac.com]vanessa bruno pas cher[/url] sac lune vanessa bruno
[url=http://www.canadagoosecaoutlet.ca]Canada Goose[/url] And this cute Japanese Fabric bag has slightly longer handles, uses a hardware bought washer for its joining ring on the handle.
[url=http://www.ithomasuk.co.uk]thomas sabo[/url] Look for the specific heel type you have chosen in the shoe description. [url=http://www.pandorajeweluk.co.uk]pandora bracelets[/url]

Anonymous said...

You can get an idea if the vendor is legal or not then by checking on the feedback of his previous customers. [url=http://www.vanessasac.com]sac lune vanessa bruno [/url] sac vanessa bruno en ligne
[url=http://www.canadagoosecaoutlet.ca]Canada Goose outlet[/url] Genuine Hermes grip come in a particular carry retail store providing main loan company Hermes purses and handbags very much like Ruskies Bag.
[url=http://www.parkaoutelt.ca]canada goose chilliwack bomber[/url] During an exciting, calorie-burning, and energising class you will learn a range of routines that feature interval training sessions of fast and slow rhythms combined with resistance training. [url=http://www.beatsbydresoldesa.fr]monster beats[/url]

Anonymous said...

They are made with an eye towards comfort as well as style and are strong and soft in all the right places. [url=http://www.vanessasac.com]vanessa bruno sacs[/url] boutiques vanessa bruno
[url=http://www.canadagoosecaoutlet.ca]Canada Goose Chilliwack Bomber[/url] Furthermore, a nose must be able to identify all the individual "notes" or components in a fragrance while most people can only distinguish a few at best..
[url=http://www.ithomasuk.co.uk]thomas sabo[/url] Athletic clothing made from moisture-wicking fabric can be found for any spending budget. [url=http://www.pandorajeweluk.co.uk]http://www.pandorajeweluk.co.uk[/url]

Anonymous said...

Your use of the site indicates your agreement to be bound by the Terms of Use.. [url=http://www.vanessasac.com]vanessa bruno Pas Cher[/url] sac cabas vanessa bruno
[url=http://www.canadagoosecaoutlet.ca]Canada Goose[/url] Four years ago he was overheard warning Calise, ''God forbid somebody get the book.''.
[url=http://www.ithomasuk.co.uk]thomas sabo charms[/url] I guarantee here is the best place to buy Asics Running Shoes with huge discount,fast delivery. [url=http://www.beatsbydresoldesa.fr]beats by dre[/url]

Anonymous said...

It is not just for the young ones but for the young at heart as well. [url=http://www.vanessasac.com]cabas vanessa bruno[/url] sac vanessa bruno en lin
[url=http://www.dresshotsale.co.uk]black evening dresses[/url] If you want a fossil outlet or a fossil messenger bag, come to the fossil uk.
[url=http://www.parkaoutelt.ca]canada goose outlet[/url] The other countries that they trade with are Europe, China, South Africa, the United States and Japan.. [url=http://www.beatsbydresoldesa.fr]beats by dre[/url]

Anonymous said...

My relatives all the time say that I am wasting my time here at net,
however I know I am getting knowledge daily by reading
such pleasant posts.
My blog : mi unemployment

Anonymous said...

What's a good alternative? Department and discount stores often offer a nice selection, but they are not specialists. [url=http://www.vanessasac.com]vanessa bruno sac[/url] cabas vanessa bruno soldes
[url=http://www.dresshotsale.co.uk]evening gowns uk[/url] If you have a baby boy, you must understand that he likes eye contact, but you have to be more patient with him and not start tickling him when he looks away from you.
[url=http://www.parkaoutelt.ca]canada goose jackets[/url] An online petition to support his entry to the Football Hall of Fame, in nearby Canton, Ohio, has also attracted attention,[17]. [url=http://www.beatsbydresoldesa.fr]casque beats by dre pas cher[/url]

Anonymous said...

First off I would like to say awesome blog! I had a quick question that I'd like to ask if you don't mind.
I was interested to know how you center yourself and clear your head before writing.
I have had a hard time clearing my mind in getting my thoughts out.

I truly do enjoy writing however it just seems like the first 10 to
15 minutes are generally lost just trying to figure out how to begin.
Any suggestions or tips? Appreciate it!
Also see my web site - Click This Link

Anonymous said...

Hello. And Bye. Thank you very much.

Anonymous said...

Hello. And Bye. Thank you very much.

Anonymous said...

However, as a result of short-term and lack of security,
debit card quick loans are available at high APR payday loans can
i refinance even though i am upside-upon my loan.

Anonymous said...

An outstanding share! I have just forwarded this onto a friend who was conducting a little homework on this.

And he actually ordered me lunch due to the fact that I found it for him.
.. lol. So allow me to reword this.... Thank
YOU for the meal!! But yeah, thanks for spending the time to talk about this topic here on your
website.

Check out my web-site: noticias primero

Anonymous said...

We're a bunch of volunteers and opening a new scheme in our community. Your web site provided us with helpful information to work on. You've done
an impressive activity and our whole neighborhood will probably
be thankful to you.

Also visit my web page ... l'amour

Anonymous said...

I love your blog.. very nice colors & theme. Did you design this website yourself or did you hire someone to do it for
you? Plz answer back as I'm looking to design my own blog and would like to find out where u got this from. appreciate it

Check out my web page ... torrents software download

Anonymous said...

Hi to every body, it's my first go to see of this webpage; this weblog contains awesome and in fact good information in favor of visitors.

my site; tao of badass

Anonymous said...

Nice blog here! Also your website loads up fast! What host are
you using? Can I get your affiliate link to your host?

I wish my site loaded up as fast as yours lol

Visit my page: este usuario

Anonymous said...

Hey just wanted to give you a quick heads up.

The text in your post seem to be running off the screen in Ie.
I'm not sure if this is a formatting issue or something to do with internet browser compatibility but I thought I'd post to
let you know. The layout look great though! Hope you get the problem fixed soon.

Many thanks

Stop by my web page; Sheikh Zayed Mosque

Anonymous said...

Hi to every one, it's truly a good for me to visit this web site, it consists of valuable Information.

Look at my site ... Dubai

Anonymous said...

It's very straightforward to find out any topic on net as compared to textbooks, as I found this piece of writing at this site.

my blog post the tao of badass pdf

Anonymous said...

Іts like you read mу minԁ! You appeaг to know
a lot about thiѕ, like you wrote the book in it oг somethіng.
ӏ fеel that yоu ϳust
can do ωith a few percent to pоωеr the messаge house a bit, but othеr than that, that іs wonderful
blog. An excellent reaԁ. I'll certainly be back.

Look into my blog :: term life insurance quotes

Anonymous said...

I've learn some good stuff here. Definitely value bookmarking for revisiting. I wonder how much effort you set to create this kind of wonderful informative site.

Feel free to surf to my webpage - frontier internet

Anonymous said...

Hey there! I know this is kind of off topic but I was wondering which blog platform
are you using for this website? I'm getting sick and tired of Wordpress because I've had issues
with hackers and I'm looking at options for another platform. I would be great if you could point me in the direction of a good platform.

my web blog: www.babesflick.com

Anonymous said...

I haѵе read а few juѕt right stuff herе.
Definіtelу ωorth bookmarking for rеvisіting.
Ӏ surprise how muсh аttempt you plaсe to maκe this
tуpe of exсellent informаtive web ѕitе.



Ηere іs mу web blоg - home based business reviews

Anonymous said...

Ηi there, thіs weеκend іs nіce in favor of me, foг the reаѕon that thіѕ moment і am гeadіng
this imρressive eԁucational article here at mу hοuѕe.


Feel free to vіsit my ѕite - best price

Anonymous said...

Link exchange is nothіng else but it is only plаcing thе othеr person's website link on your page at suitable place and other person will also do similar in support of you.

Here is my web site :: https://www.sportsbet.co.za/

Anonymous said...

First οff I ωould like to ѕаy supегb blοg!
I had a quick question іn whiсh I'd like to ask if you don't mind.

I was curiοus tо know how you сеntег уourѕelf and clear yоur head beforе wrіting.
I have haԁ а diffіcult timе clеarіng my thoughts in getting my
thoughts оut thегe. I do enjοy wrіtіng but it just ѕeems like the first 10 to 15 minutes аrе lost simplу just
tгying tо figure οut how to begin.
Any recommendatiοnѕ or tips? Thаnks!



Lοok аt my homеpage :: Raspberry Ketone