[erlang-questions] Why does fun2ms produce this particular result?

Daniel Dormont dan@REDACTED
Thu May 5 05:17:30 CEST 2011


On Wed, May 4, 2011 at 3:03 AM, Mazen Harake <mazen.harake@REDACTED> wrote:

> Well, yes.
>
> 1) Quoting documentation:
>
> "ETS match specifications are there to produce a return value. Usually the
> MatchBody contains one single ConditionExpression which defines the return
> value without having any side effects. Calls with side effects are not
> allowed in the ETS context.
>
> When tracing there is no return value to produce, the match specification
> either matches or doesn't. The effect when the expression matches is a trace
> message rather then a returned term. The ActionTerm's are executed as in
> an imperative language, i.e. for their side effects. Functions with side
> effects are also allowed when tracing.
>
> In ETS the match head is a tuple() (or a single match variable) while it
> is a list (or a single match variable) when tracing."
> So in other words (in the context of dbg): yes, it is a list because you
> can have several "actions" in it. But in the context of ets there is usually
> only 1 namely what you want to return, if you have more than 1 then the last
> one will be returned.
>
>
Got it.


> 2) This is correct.
>
> I would say the opposite; I see it as semantics of both are similar enough
> to use a "mini language" with exceptions rather than 2 different
> specifications all together, but I guess that is just a matter of personal
> taste.
>
> /M
>
>
>
Fair enough. So far the trick I've been using is to write my function out,
use fun2ms in a shell to generate the MatchSpec, copy that into my code and
leave the original function as a comment. This works well enough for me.

dan


>
> On 4 May 2011 05:18, Daniel Dormont <dan@REDACTED> wrote:
>
>> Thanks, let me summarize to see if this is correct:
>>
>> 1) In general (though not in the case of ETS/Mnesia), the MatchBody might
>> contain actions to be performed before returning the final result. This is
>> why it's a list.
>> 2) Actions may be specified in the form of a tuple, the first element of
>> which describes the action to be performed. Therefore, if you want to have
>> your "action" be a value which is itself a tuple, you have to wrap it in
>> another tuple.
>>
>> Is this right? If so, I guess I understand what the rules are, but I'm
>> still not really clear on why, since it seems like dbg and ets are different
>> enough that you'd never really use the same kind of MatchSpec for both, so
>> there isn't much benefit in having the structure be compatible.
>>
>> dan
>>
>> On Mon, May 2, 2011 at 2:35 AM, Mazen Harake <mazen.harake@REDACTED>wrote:
>>
>>> Check out chapter 2 in my dbg tutorial, it describes matchspecs and how
>>> to use them. You will also understand what it is that fun2ms actually
>>> translates to.
>>>
>>>
>>> http://www.erlang-factory.com/conference/testingtutorialworkshop2010/speakers/MazenHarake
>>>
>>> Dbg matchspecs aren't exactly like the mnesia ones, there is a small
>>> difference but the concept is exactly the same.
>>>
>>> /M
>>>
>>> On 1 May 2011 21:06, Daniel Dormont <dan@REDACTED> wrote:
>>>
>>>> I'm trying to get the hang of match specs. Here's a pretty simple one
>>>> but it has a feature I'm not getting. It's intended for use with Mnesia:
>>>>
>>>> 1> rd(person, {name, address, email, phone}).
>>>> person
>>>> 2> ets:fun2ms(fun(#person{name=Name, email=Email}) -> {Name, Email}
>>>> end).
>>>> [{#person{name = '$1',address = '_',email = '$2',
>>>>           phone = '_'},
>>>>   [],
>>>>   [{{'$1','$2'}}]}]
>>>>
>>>> My first question was going to be why the whole thing is a
>>>> single-element list, but I think I figured that out: it's because there
>>>> could be multiple clauses of the match as a whole. The part I don't get is
>>>> in the result: [{{'$1','$2'}}]
>>>>
>>>> 1) Why is it a list at all, since a particular head in the function can
>>>> only have one result?
>>>> 2) Why does the tuple have to be wrapped in another tuple? ie why isn't
>>>> it just {'$1','$2'}
>>>>
>>>> For now I'm happy to just trust that fun2ms works, but I'd like to
>>>> understand it a little better.
>>>>
>>>> thanks,
>>>> Dan
>>>>
>>>>
>>>> _______________________________________________
>>>> erlang-questions mailing list
>>>> erlang-questions@REDACTED
>>>> http://erlang.org/mailman/listinfo/erlang-questions
>>>>
>>>>
>>>
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20110504/5868dd24/attachment.htm>


More information about the erlang-questions mailing list