[erlang-questions] Floating guard sequences

Richard O'Keefe ok@REDACTED
Wed Feb 18 01:51:15 CET 2009


On 18 Feb 2009, at 11:13 am, Michael Radford wrote:

> This is a very preliminary proposal to extend Erlang's guard
> syntax (based on an idea I briefly mentioned in another thread).

1.  The proposal does not increase expressiveness.
     All it does is move constraints from after the head
     to inside the head.

2.  It is far from clear to me that this improves readability.
     (I am being polite here.)

3.  There is an existing problem which such proposals should
     address.  Consider
	f(X, ...) when is_integer(X), X >= 0, ... ->
	f(..., Y) when is_integer(Y), Y >= 0, ... ->
	...
     The concept "is a natural number" cannot be defined once
     and thereafter used by name.  Just moving the guards around
     doesn't begin to help with this.

Abstract patterns, however, do increase expressiveness,
can improve readability, and do solve the "no way to name
a repeated condition" problem, *without* introducing
potentially unbounded or side-effective computation into
matching.  And they're hygienic, not like macros.

	#natural(X) when is_integer(X), X >= 0 -> X.

	f(#natural(X), ...) when ... -> ...
	f(..., #natural(Y)) when ... -> ...

Abstract patterns really are a good idea.

Having said this, the nicest way to implement them -- though it
isn't always doable -- is via inline expansion, which would
mean that an Erlang compiler might have a use for something
very like Michael Radford's suggestion *as an internal form*.





More information about the erlang-questions mailing list