Sunday, December 13, 2009

Reia: now with "Magic Rebinding"

In Ruby, thanks to its first class syntax for "hashes" and mutable state, it's quite easy to do:
h = {}
h[key] = value
The equivalent code in Erlang is noisier, thanks to immutable state and single assignment:
Dict1 = dict:new(),
Dict2 = dict:store(Key, Value, Dict1).
Since Reia lacks mutable state, it never before had syntax as simple as Ruby's... but now it does!

I have been trying to hold off on adding syntactic sugar like this to my new "minimalist" branch of Reia. However, this is a feature I meant to add to the old implementation, and tried to retrofit it in long after the implementation had grown quite complex, never managing to succeed. I decided to tackle it now, and I'm happy to announce that it works! Furthermore, it can be used in complex pattern matching expressions:
>> m = {}
=> {}
>> (m[:foo], m[:bar], m[:baz]) = (1,2,3)
=> (1,2,3)
>> m
=> {:bar=>2,:baz=>3,:foo=>1}
So what is going on here exactly? Reia is an immutable state language, so surely I'm not mutating the value that "m" references.

In these cases, Reia is "altering" the local variable binding. Each time you change a member of a map ("hash" for you Ruby folks, "dict" for you Erlang folks), a new version of that map is calculated, then bound to "m". Behind the scenes, the Reia compiler is translating these calls into destructive assignments.

Maps, Tuples, and even Lists now support assignments in this way (although Lists only for consistency's sake... I hate to think of people actually setting values in lists by index). Tuples and Lists even support Ruby-style negative indexing:
>> t = (1,2,3)
=> (1,2,3)
>> t[-1] = 42
=> 42
>> t
=> (1,2,42)
I plan on eventually exposing this functionality to user-defined types as well, in the form of "bang methods" on immutable objects. Users of Ruby are likely familiar with them:
>> arr = [1,2,3]
=> [1,2,3]
>> arr.reverse; arr
=> [1,2,3]
>> arr.reverse!; arr
=> [3,2,1]
Here you can see that calling the "reverse" method on an array (without the !) does not modify the original array in-place. Instead, it returns a new array in reverse order. The complimentary "reverse!" method performs an in-place modification of the array.

The "method!" idiom in Ruby is generally used to indicate methods that modify their receivers as opposed to versions which do not. However this is not a solid requirement, and "!" is often added to any methods considered "dangerous". There's no specific meaning to putting "!" on the end of a method and certainly nothing Ruby does differently at the language level.

In Reia, "bang methods" will be a first class language construct, and will always rebind the receiver with the return value of the method. This will provide a simple way to allow "in place" modifications of immutable objects, by having "bang methods" create and return a new immutable object.

It's the best of both worlds: the ease of use that comes from mutable state, with the concurrency benefits of being completely immutable.

38 comments:

Knodi said...

Reia is shaping out to be really good.

Unknown said...

"!" methods in ruby don't strictly mean that they modify the receiver. "!" means the method is to be considered "more dangerous" than it's non-! counterpart.

David Black has an excellent and succinct statement on this, however, it's in a standalone html file somewhere on his domain, where I can't remember.

What I have to wonder is the motivation for this, it seems to be dangerous, at least there are some alarm bells going off in my mind - although I have not used it, so my intuition may be far wrong.

It seems to me that the idea of a method receiver having control of local variables for the scope in which the method was called, seems inverted. Moreover the gains that are made for immutability seem to be lost once this occurs. Sure this is only locals, but the same class of errors has just been opened from this as exist for general shared state using other variable types, it's just restricted to local scope. - That's all good until closures.

I'm not sure, I totally understand why you want it, and my intuition could be very wrong, but if it really turns out to be useful, I have to wonder why the hold on to immutability. I don't generally have issues with mutable state in other languages, because I am careful to architect around the issues that can be raised from it - here I get the feeling I'd be using the same kind of care the other way around. Paying for the cost of immutability seems undesirable when the programmer has to also put conceptual effort in.

Tony said...

Hi raggi,

Indeed you'll see I noted that Ruby's "bang methods" don't strictly modify the receiver but also perform "dangerous" actions in my post.

As implemented in Reia, "bang methods" do not directly allow methods to modify the binding. In fact, as presently called, the method invoked isn't even aware it's being used as a "bang method". In that regard, ! functions more like an operator in the local scope.

Regarding your comments as to abandoning immutability, ideally I'd like to (in favor of process-local mutable state, using COW when messages are sent between processes). However this would require a far different virtual machine than BEAM.

A great deal of optimization work has already gone into Erlang, and I am trying to build on that. So, for now, I am stuck with immutability.

essay reviews said...

Reia comes to shape out well and it is becoming popular. In ruby the "!" methods need to be considered and it seems to be more dangerous than the other part.
Assignments are the hectic task that we face in our academic career and get the best help from best dissertation writing service to make it easier

Unknown said...

cctv camera dealers in delhi
cp plus cctv camera online
hikvision camera online
cctv camera installation services in delhi
cctv camera installation services in gurugram
cctv camera installation services in gurgaon
cctv camera installation services in noida
cctv camera security systems
cctv installation services
security systems for home

Blogger said...

we are expert in Digital Marketing and web designing work. if someone looking for these services they can hire us.

Prachi Kalra said...

Awesome Blog!
Keep up the great work going.
Web Design Company in India

Prachi Kalra said...

Incredible blog! Very helpful.Web Development Company in India

dadyar said...

Hello colleagues, good piece of writing and good arguments commented at this place,
I am truly enjoying by these.
johnajany.doodlekit

OGEN Infosystem (P) Limited said...

Very nice, it’s really amazing and informative also. Visit OGEN Infosystem for Best Website Designing Company and get creative and responsive website designing.
Top 5 Website Development Company in Delhi

geekymr said...

hyperx cloud2 pubg gaming headphone review is best gaming headphone to play pubg mobile.
esports team name ideas article is belongs to clan name here you find lots of unique clan names for you team.

No #1 Gurgaon Escorts Service Agency-Gurugram said...



After this brings up its clients get entertained well with its premium services, the Female Escorts in Goais liable to increase the level of fun by its quality services dissimilar to get anywhere. So, let us forget everything and take pleasure in what you wait for a long time. Check our other Services...
Female Escorts in Lucknow
Housewife Escorts in Lucknow
Independent Call Girls in Lucknow
Lucknow Call Girls Service
Lucknow Escorts Service
Russian Escorts in Lucknow

digitals said...

Best Social Bookmarking Submission Sites
social bookmarking sites list
top social bookmarking sites
new social bookmarking sites list
top social bookmarking sites for seo
social bookmarking websites list
social bookmarking site list
best social bookmarking sites
social bookmarking submission sites
social bookmarking submission site list
best new social bookmarking sites
top social bookmarking sites
social bookmarking website list

LL said...

http://www.unlimitednovelty.com/2009/04/why-i-dont-like-scala.html

John said...

bio
showing

Aditi Gupta said...

Such a fantastic post and article you shared! Thank you so much for sharing a wonderful post.
Golden Triangle India Tour

Ethan jurk said...

HP DESKJET 2600 WIRELESS SETUP involves driver download and network connectivity. For enjoying multi-features through the proper printer connection.

Best Home Service said...

Thanks for this informative post. Please keep sharing these useful posts.


چیلر دستگاهی است که با یک سیکل تبرید-تراکم بخار، حرارت را یک مایع میگیرد. از مایع سرد بوجود آمده در یک مبدل حرارتی، در دستگاه های خنک کننده و هواسازها استفاده می شود. چیلرها انواع مختلفی دارند از جمله چیلرهای جذبی، چیلرهای تراکمی، چیلرهای صنعتی، مینی چیلرها و ... . چیلرها از اجزا گران قیمتی ساخته می شوند و به همین دلیل قیمت بالایی دارند.
نصب و راه اندازی و تعمیر چیلر کاری تخصصی است که انجام آن بدون شک به تجربه و مهارت نیاز دارد. نکات نصب بسته به نوع چیلر متفاوت است. به عنوان مثال چیلرهای تراکمی هوا خنک باید در هوای آزاد باشند که اغلب روی پشت بام ساختمان قرار داده می شوند. در مقابل چیلرهای تراکمی آب خنک بایستی در مکان مسقف باشند؛ به همین دلیل در موتورخانه وقسمت تأسیسات ساختمان نصب و راه اندازی می شوند.

BEST SITE said...

Adapted to new systems and processes well and seeks out training to enhance knowledge, skills and abilities.

야동
오피헌터
출장안마
타이마사지
바카라사이트

izspa.net said...

web have to set aat body massage near me

james said...

It's the best of both worlds: the ease of use that comes from mutable state, with the concurrency benefits of being completely immutable. Now its time to avail dry van dispatch servicesfor more details.

John Hardy said...

I am very thankful for this great information in this article. I hope you will share ore good updates. Now it's time to avail luxury airport transfer for more information.

bahman said...

How to get rid of toilet, house and cabinet cockroaches. To know this, you can click on the link below.https://www.zupyak.com/p/3539490/t/how-to-get-rid-of-toilet-house-and-cabinet-cockroaches

arta said...

In چاه بازکنی در بلوار فردوس, we have the possibility to do well draining well using machines that are equipped with very strong pumps and basic suckers. The important thing about draining a well is that in many cases the well needs to be dredged, which can only be recognized by the person draining the well.

arta said...

تحصیل در کانادا and studying immigration to this country has increased significantly in the past years. The suitable facilities provided by the government for immigrants have placed Canada in the list of ideal countries for immigrants to live in. If you intend to immigrate to this country for study, or you want to enjoy the benefits of studying in Canada, stay with us until the end of this article. Here, we will review the benefits of studying in Canada, the conditions for studying in Canada, and everything you need to know about studying in this country.

Priya Reddy said...

This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to article very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge. Click Here

lolehbazkonitehran said...

Purple masterbatch is used in product packaging such as bottles, containers and films to increase their visual appeal and brand recognition. Purple packaging is usually used for luxury and expensive products such as perfume, cosmetics and wine.

arta said...

The templates that are used in the process of designing a website and purchasing a cheap خرید سایت are all optimized and SEO friendly. It doesn't matter if you have chosen plan 1 or plan 4 of purchasing the site, the same templates are used in all plans. This means that the plan you choose has no effect on the type of template used, and the templates are all of high quality.

buysitecheep said...

Building برنامه نویسی بلاکچین in Python: This is a comprehensive course on building a blockchain from scratch using Python. It covers topics such as hashing, block mining, and transaction processing. The course also covers advanced topics such as consensus algorithms and smart contracts.

asiapanel said...

Sandwich panel is one of the best prefabricated building materials. Sandwich panels are used in the construction of sheds, prefabricated buildings, condos, clean rooms and cold storage.
ساندویچ پانل
ساندویچ پانل دیواری
محصولات ساندویچ پانل

kianpanel said...

نصب ساندویچ پانل همان پیچ کردن پانل ها بر روی زیرسازی فلزی ساختمان یا سازه است. که در نهایت باید به طور کامل ایزوله و آبند بند باشد و هیچگونه راه نفوذی به داخل سازه وجود نداشته باشد. اما شاید کار نصب برای یک نصاب حرفه ای ساندویچ پانل هم اینچنین که توضیح دادیم ساده نباشد

کیان پانل

shanjanaarora said...

During this massage, your therapist nude massage in chennai
will use small body range motions in order to loosen up tightened tendons

johnscena said...

Unlimited Novelty appears to be a phrase that suggests an endless supply of new and interesting things or concepts. It could be used to describe an environment, a creative project, or a mindset that embraces constant innovation, creativity, and the introduction of novel ideas.
FLSA Abogado en Virginia

lishasingh said...

nice and welco spa near me

rennasweety said...

This is because breath is the only function in the body that can be done totally consciously, or totally unconsciously. Read more body to body massage in hyderabadabout the power of breath in tantric therapy.

nancysweety said...

There isn’t strong evidence that reflexology works female to male body massage in pune any better than a foot massage, but reflexologists believe reflexology

کبیرپانل said...

Kabir Panel Group has started its activity since 2013 with the aim of producing all kinds of ceiling sandwich panels, wall sandwich panels and cold storage panels with polyurethane foam.
ساندویچ پانل
ساندویچ پانل دیواری
کبیرپانل

trustaqua said...

Trust Aqua water treatment system
Trust Aqua wastewater treatment system
تراست آکوا
پکیج تصفیه فاضلاب بیمارستانی
پکیچ تصفیه فاضلاب کشتارگاه