New EEP draft: Pinning operator ^ in patterns

Raimo Niskanen raimo+erlang-questions@REDACTED
Fri Jan 15 15:11:46 CET 2021

On Thu, Dec 24, 2020 at 09:05:03PM -0800, Michael Truog wrote:
> Matching is very important in Prolog too, with Erlang syntax being 
> naturally similar.  A pin operator hasn't been necessary in Prolog 
> syntax, though the matching gets more complex in Prolog (solving rubik's 
> cubes with short examples and other amazing things). There isn't a clear 
> benefit to adding the Elixir pin operator to Erlang, just like there 
> wouldn't be a benefit to adding it to Prolog.

For me this comparison with Prolog does not land completely.  I am going
out on thin ice here, because I have never written any Prolog.

But, as I have understood it, in Prolog; if a variable is used twice there
is no difference between the two uses - they are very symmetrical.  Prolog
unifies these using backtracking.

Erlang, to get better soft real time properties, was designed to not use
backtracking, and to accomplish this adopted the semantics of first
binding a value and if the variable would occur again matching on
the value - an asymmetrical approach.

A pinning operator can not be added to Prolog because multiple uses
of the same variable are symmetrical, but in Erlang I think it would be
reasonable to have a syntax for matching to distinghish it from binding,
since these are fundamentally different operations.

/ Raimo

> Best Regards,
> Michael
> On 12/24/20 12:10 PM, Richard Carlsson wrote:
> > The ^ operator allows you to annotate already-bound pattern variables 
> > as ^X, like in Elixir. This is less error prone when code is being 
> > refactored and moved around so that variables previously new in a 
> > pattern may become bound, or vice versa, and makes it easier for the 
> > reader to see the intent of the code.
> >
> > See also
> >
> > Ho ho ho,
> >
> >         /Richard & the good folks at WhatsApp


/ Raimo Niskanen, Erlang/OTP, Ericsson AB

More information about the erlang-questions mailing list