Types
Pseudo function that by means of a parse_transform translates the literal fun() typed as parameter in the function call to a match specification as described in the match_spec manual of ERTS users guide. (With literal I mean that the fun() needs to textually be written as the parameter of the function, it cannot be held in a variable which in turn is passed to the function).
The parse transform is implemented in the module ms_transform and the source must include the file ms_transform.hrl in STDLIB for this pseudo function to work. Failing to include the hrl file in the source will result in a runtime error, not a compile time ditto. The include file is easiest included by adding the line -include_lib("stdlib/include/ms_transform.hrl"). to the source file.
The fun() is very restricted, it can take only a single parameter (the parameter list to match), a sole variable or a list. It needs to use the is_XXX guard tests and one cannot use language constructs that have no representation in a match_spec (like if, case, receive etc). The return value from the fun will be the return value of the resulting match_spec.
Example:
1> dbg:fun2ms(fun([M,N]) when N > 3 -> return_trace() end).
[{['$1','$2'],[{'>','$2',3}],[{return_trace}]}]
Variables from the environment can be imported, so that this works:
2> X=3. 3 3> dbg:fun2ms(fun([M,N]) when N > X -> return_trace() end). [{['$1','$2'],[{'>','$2',{const,3}}],[{return_trace}]}]
The imported variables will be replaced by match_spec const expressions, which is consistent with the static scoping for Erlang fun()s. Local or global function calls cannot be in the guard or body of the fun however. Calls to builtin match_spec functions of course is allowed:
4> dbg:fun2ms(fun([M,N]) when N > X, is_atomm(M) -> return_trace() end). Error: fun containing local erlang function calls ('is_atomm' called in guard)\ cannot be translated into match_spec {error,transform_error} 5> dbg:fun2ms(fun([M,N]) when N > X, is_atom(M) -> return_trace() end). [{['$1','$2'],[{'>','$2',{const,3}},{is_atom,'$1'}],[{return_trace}]}]
As you can see by the example, the function can be called from the shell too. The fun() needs to be literally in the call when used from the shell as well. Other means than the parse_transform are used in the shell case, but more or less the same restrictions apply (the exception being records, as they are not handled by the shell).
If the parse_transform is not applied to a module which calls this pseudo function, the call will fail in runtime (with a badarg). The module dbg actually exports a function with this name, but it should never really be called except for when using the function in the shell. If the parse_transform is properly applied by including the ms_transform.hrl header file, compiled code will never call the function, but the function call is replaced by a literal match_spec.
More information is provided by the ms_transform manual page in STDLIB.