[erlang-questions] Questions on match specifications

26 views
Skip to first unread message

wrp

unread,
Nov 10, 2011, 9:33:56 PM11/10/11
to erlang-q...@erlang.org
Match specifications don't appear to be complicated, but the
documentation on them is sparse. I found the description in Cesarini &
Thompson to be much clearer than in the ERTS User's Guide, but I still
have some points of confusion.

For starters:

1. Quoting seems incosistent. Why is 'and' quoted but andalso is not?

2. Why is float division (/) not accepted in guards?

3. In the case of infix operators, does {'op','$1','$2'} always mean
"$1 op $2" (vs. "$2 op $1")?

4. The ERTS User's Guide lists get_tcw as an allowed function in ets
matches. Is that correct?
_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

OvermindDL1

unread,
Nov 11, 2011, 12:13:24 AM11/11/11
to wrp, erlang-q...@erlang.org


On Nov 10, 2011 7:34 PM, "wrp" <i3t...@gmail.com> wrote:
>
> Match specifications don't appear to be complicated, but the
> documentation on them is sparse. I found the description in Cesarini &
> Thompson to be much clearer than in the ERTS User's Guide, but I still
> have some points of confusion.
>
> For starters:
>
> 1. Quoting seems incosistent. Why is 'and' quoted but andalso is not?

'and' and and are the same.
'andalso' and andalso are the same.
Just atoms is all it is.

>
> 2. Why is float division (/) not accepted in guards?

No clue, I did not know it was, I am curious too.

>
> 3. In the case of infix operators, does {'op','$1','$2'} always mean
> "$1 op $2" (vs. "$2 op $1")?

I think so, but do not quote me, wait for another to answer, I do not have the source right now.

>
> 4. The ERTS User's Guide lists get_tcw as an allowed function in ets
> matches. Is that correct?
>

No clue.

Magnus Henoch

unread,
Nov 11, 2011, 9:44:43 AM11/11/11
to wrp, erlang-q...@erlang.org
> Match specifications don't appear to be complicated, but the
> documentation on them is sparse. I found the description in Cesarini &
> Thompson to be much clearer than in the ERTS User's Guide, but I still
> have some points of confusion.
>
> For starters:
>
> 1. Quoting seems incosistent. Why is 'and' quoted but andalso is not?

Probably a typo. Both need to be quoted, otherwise you get a syntax
error.

> 2. Why is float division (/) not accepted in guards?

Seems like an omission. It works for me:

1> ets:fun2ms(fun({A,B}) when A / B > 1 -> {A,B} end).
[{{'$1','$2'},[{'>',{'/','$1','$2'},1}],[{{'$1','$2'}}]}]
2> ets:new(foobar, [named_table]).
foobar
3> ets:insert(foobar, {1, 4}).
true
4> ets:insert(foobar, {10, 3}).
true
5> ets:select(foobar, v(1)).
[{10,3}]

> 3. In the case of infix operators, does {'op','$1','$2'} always mean
> "$1 op $2" (vs. "$2 op $1")?

Yes.

> 4. The ERTS User's Guide lists get_tcw as an allowed function in ets
> matches. Is that correct?

It doesn't seem so. ets:fun2ms accepts it, but using it in a select gives
a badarg:

8> ets:fun2ms(fun({_,_}) -> get_tcw() end).
[{{'_','_'},[],[{get_tcw}]}]
9> ets:select(foobar, v(8)).
** exception error: bad argument
in function ets:select/2
called as ets:select(foobar,[{{'_','_'},[],[{get_tcw}]}])

--
Magnus Henoch
Erlang Solutions Ltd
http://www.erlang-solutions.com/

Daniel Dormont

unread,
Nov 11, 2011, 7:48:39 PM11/11/11
to erlang-questions
Regarding 4). This is not abundantly clear on first read in the HTML
version because the header doesn't stand out, but get_tcw is listed
under "Functions allowed only for tracing", not "Functions allowed in
all types of match specifications".

http://www.erlang.org/doc/apps/erts/match_spec.html#id273975

dan

wrp

unread,
Nov 11, 2011, 9:05:03 PM11/11/11
to erlang-q...@erlang.org
Ah, yes...and just two lines above the main header is an illustration
of the evils of copy&paste.

On Nov 11, 4:48 pm, Daniel Dormont <d...@greywallsoftware.com> wrote:
> Regarding 4). This is not abundantly clear on first read in the HTML
> version because the header doesn't stand out, but get_tcw is listed
> under "Functions allowed only for tracing", not "Functions allowed in
> all types of match specifications".
>
> http://www.erlang.org/doc/apps/erts/match_spec.html#id273975
>
> dan
>

wrp

unread,
Nov 11, 2011, 9:21:44 PM11/11/11
to erlang-q...@erlang.org
Thanks. That clear things up some. But I'm not sure about this one:

On Nov 11, 6:44 am, Magnus Henoch <magnus.hen...@erlang-solutions.com>
wrote:


> > 1. Quoting seems incosistent. Why is 'and' quoted but andalso is not?
>
> Probably a typo.  Both need to be quoted, otherwise you get a syntax
> error.

In the MatchConditions and MatchBody, aren't all the terms passed as
atoms?

Here are all the functions listed as allowed for ETS matches:

abs is_atom is_record '=:=' 'and'
element is_binary is_reference '=/=' 'or'
hd is_constant is_seq_trace '==' 'not'
length is_float is_tuple '/=' 'xor'
node is_function '+' '<' 'band'
round is_integer '-' '=<' 'bor'
size is_list '*' '>' 'bnot'
tl is_number 'div' '>=' 'bxor'
trunc is_pid 'rem' andalso 'bsl'
self is_port orelse 'bsr'

According to the Erlang rules for atoms, "An atom should be enclosed
in single quotes (') if it does not begin with a lower-case letter or
if it contains other characters than alphanumeric characters,
underscore (_), or @", so only the non-alphanumeric operators should
need quoting. Why do the arithmetic and logical operators need to be
quoted?

Daniel Dormont

unread,
Nov 11, 2011, 11:29:01 PM11/11/11
to wrp, erlang-q...@erlang.org
Erlang also has reserved words. If I were revising the manual, I would
modify section 2.3 to explicitly say "or if it is a reserved word"
with a link to the list of reserved words, which includes both 'and'
and 'andalso'.

This has nothing to do with match specs specifically; as already
mentioned, there's a typo in that part of the manual. But in fact, you
cannot use 'and' or 'andalso' unquoted *anywhere* in Erlang if you
want them to be literal atoms rather than operators.

dan

wrp

unread,
Nov 12, 2011, 12:07:01 AM11/12/11
to erlang-q...@erlang.org

On Nov 11, 8:29 pm, Daniel Dormont <d...@greywallsoftware.com> wrote:
> Erlang also has reserved words. If I were revising the manual, I would
> modify section 2.3 to explicitly say "or if it is a reserved word"
> with a link to the list of reserved words, which includes both 'and'
> and 'andalso'.

AH, HA! That's it, and the list of reserved words is:
after begin bsr cond if or rem
and bnot bxor div let orelse try
andalso bor case end not query when
band bsl catch fun of receive xor

Lukas Larsson

unread,
Nov 12, 2011, 8:12:37 AM11/12/11
to wrp, erlang-q...@erlang.org
Hello!

We love patches which makes the documentation clearer for others to understand. If you have the time, please contribute to make Erlang/OTP better by submitting a patch expanding the relevant documentation. You can find the section here: https://github.com/erlang/otp/blob/master/erts/doc/src/match_spec.xml

Info about how to submit a patch can be found here: https://github.com/erlang/otp/wiki/Submitting-patches

Lukas
Erlang/OTP Team
Reply all
Reply to author
Forward
0 new messages