<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><br></div><div>For deeper excavation, you might get something out of meditating over ct_expand.erl:</div><div><br></div><div><a href="https://github.com/uwiger/parse_trans/blob/master/src/ct_expand.erl">https://github.com/uwiger/parse_trans/blob/master/src/ct_expand.erl</a></div><div><br></div><div>Its uses are exemplified in </div><div><br></div><div><a href="https://github.com/uwiger/parse_trans/blob/master/examples/ct_expand_test.erl">https://github.com/uwiger/parse_trans/blob/master/examples/ct_expand_test.erl</a></div><div><br></div><div>What it does is expand expresssions at compile-time, and if those expressions make use of e.g. function calls defined in the same module (i.e. not yet compiled), it finds the abstract code for those functions and evaluates it.</div><div><br></div><div>If one wanted to pull a self-contained abstract-code definition of a fun, this sort of thing, combined perhaps with inlining, might come in handy.</div><div><br></div><div>(That is, if the extracted abstract code contains a call to a non-exported function, it will be incomplete when pulled out of its context).</div><div><br></div><div>BR,</div><div>Ulf W</div><br><div><div>On 23 Jan 2013, at 11:35, Tyron Zerafa wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr">Fantastic, this is exactly what I had in mind :) I will surely give it a try (and some thought about the effect of free vars) <br><div style="">Thanks</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
On Wed, Jan 23, 2013 at 11:07 AM, Ulf Wiger <span dir="ltr"><<a href="mailto:ulf@feuerlabs.com" target="_blank">ulf@feuerlabs.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word"><br><div><div class="im"><div>On 22 Jan 2013, at 20:11, Tyron Zerafa wrote:</div><br><blockquote type="cite"><span style="border-collapse:separate;font-family:Helvetica;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;font-size:medium">Erlang:fun_info(F) is returning the function body because it is constructed in the shell which essentially passes the entire code tree to the erl_eval module for execution. If you call this for a function constructed in a module, you won't get this tree. <div>
<br></div><div>I need something along these lines that work for functions constructed in modules rather than the shell. </div></span></blockquote><div><br></div></div>Mind you, this is just example code - not intended for mission-critical use.<br>
<br></div><div><a href="http://erlang.org/pipermail/erlang-questions/2007-December/031992.html" target="_blank">http://erlang.org/pipermail/erlang-questions/2007-December/031992.html</a></div><div><br></div><div>I updated the program a little bit, since the internal naming of funs seems to have changed:</div>
<div><br></div><div><br></div><div><div>-module(extract).</div><div><br></div><div>-export([f/1]).</div><div><br></div><div><br></div><div>f(F) -></div><div>   {module, Mod} = erlang:fun_info(F, module),</div><div>   {name, Name} = erlang:fun_info(F, name),</div>
<div>   {ok, Abst} = get_abstract_code(Mod),</div><div>   extract_fun(Name, Abst).</div><div><br></div><div>get_abstract_code(Module) -></div><div>   {module,_} = code:ensure_loaded(Module),</div><div>   Beam = code:which(Module),</div>
<div>   case beam_lib:chunks(Beam, [abstract_code]) of</div><div><span style="white-space:pre-wrap">        </span>{ok,{_,[{abstract_code,{_,AC}}]}} -></div><div><span style="white-space:pre-wrap">  </span>    {ok, AC};</div><div>
<span style="white-space:pre-wrap">     </span>Other -></div><div><span style="white-space:pre-wrap">      </span>    Other</div><div>   end.</div><div><br></div><div>extract_fun(Name, AC) -></div><div>   {F,Arity,Rel} = split_name(Name),</div>
<div>   Clauses = [Cs || {function,_,F1,Arity1,Cs} <- AC,</div><div><span style="white-space:pre-wrap">          </span>     F1 == F, Arity1 == Arity],</div><div>   Funs = pick_funs(lists:concat(Clauses)),</div><div>   lists:nth(Rel, Funs).</div>
<div><br></div><div>split_name(Name) -></div><div>    [Fs, As, _, Rs] = string:tokens(atom_to_list(Name),"/-"),</div><div>   {list_to_atom(Fs), list_to_integer(As), list_to_integer(Rs)+1}.</div><div><br></div>
<div>pick_funs(L) -></div><div>   Flatten = fun({'fun',_,_} = Fun) -> [Fun];</div><div><span style="white-space:pre-wrap">                </span> (T) when is_tuple(T) -> tuple_to_list(T);</div><div><span style="white-space:pre-wrap">            </span> (_) -> []</div>
<div><span style="white-space:pre-wrap">  </span>      end,</div><div>   L1 = [X || X <- lists:flatten(lists:map(Flatten, L)),</div><div><span style="white-space:pre-wrap">    </span>       is_tuple(X)],</div><div>   case [F || F <- L1,</div>
<div><span style="white-space:pre-wrap">  </span>       element(1, F) =/= 'fun'] of</div><div><span style="white-space:pre-wrap">        </span>[] -> L1;</div><div><span style="white-space:pre-wrap">     </span>[_|_] -></div>
<div><span style="white-space:pre-wrap">  </span>    pick_funs(L1)</div><div>   end.</div></div><div><br></div><div><br></div><div>Example:</div><div><br></div><div><div>-module(m1).</div><div><br></div><div>-export([f/2]).</div>
<div><br></div><div>f(N, X) -></div><div>   if  N==1; N==2 -></div><div><span style="white-space:pre-wrap">        </span>    element(N, {fun() -></div><div><span style="white-space:pre-wrap">                    </span>        X + 1</div>
<div><span style="white-space:pre-wrap">          </span>        end,</div><div><span style="white-space:pre-wrap">         </span>        fun() -></div><div><span style="white-space:pre-wrap">                  </span>        X - 1</div><div><span style="white-space:pre-wrap">                </span>        end});</div>
<div><span style="white-space:pre-wrap">  </span>N == 3 -></div><div class="im"><div><span style="white-space:pre-wrap">     </span>    fun(Y) -></div><div><span style="white-space:pre-wrap">               </span>    X + Y</div><div>
<span style="white-space:pre-wrap">     </span>    end</div></div><div>   end.</div></div><div><br></div><div><div>Eshell V5.9.2  (abort with ^G)</div><div>1> [m1:f(N,17) || N <- [1,2,3]].</div><div>[#Fun<m1.0.1800644>,#Fun<m1.1.1800644>,#Fun<m1.2.1800644>]</div>
<div>2> [extract:f(m1:f(N,17)) || N <- [1,2,3]].</div><div>[{'fun',7,</div><div>        {clauses,[{clause,7,[],[],</div><div>                          [{op,8,'+',{var,8,'X'},{integer,8,1}}]}]}},</div>
<div> {'fun',10,</div><div>        {clauses,[{clause,10,[],[],</div><div>                          [{op,11,'-',{var,11,'X'},{integer,11,1}}]}]}},</div><div> {'fun',14,</div><div>        {clauses,[{clause,14,</div>
<div>                          [{var,14,'Y'}],</div><div>                          [],</div><div>                          [{op,15,'+',{var,15,'X'},{var,15,'Y'}}]}]}}]</div><div><br></div><div>
<br></div><div>The module m1 needs to be compiled with debug_info, of course.</div><div><br></div><div>BR,</div><div>Ulf W</div></div><br><div>
<div><div>Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.</div><div><a href="http://feuerlabs.com/" target="_blank">http://feuerlabs.com</a></div></div><div><br></div><br>
</div>
<br></div></blockquote></div><br><br clear="all"><div><br></div>-- <br>Best Regards,<div>Tyron Zerafa</div>
</div>
</blockquote></div><br><div apple-content-edited="true">
<div><div>Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.</div><div><a href="http://feuerlabs.com">http://feuerlabs.com</a></div></div><div><br></div><br class="Apple-interchange-newline">
</div>
<br></body></html>