johnbender / unraverl

An erlang module built to provide language features and other operations through manipulation of the abstract form.

This URL has Read+Write access

johnbender (author)
Sat Aug 08 18:38:57 -0700 2009
commit  cc8310fab4538cefd035502f95327bf894130292
tree    6c650a303b615b99e0220b92d9bc8459be6dbc10
parent  f66c0ed3801b3aa8aa34eaaefee06f06086f0744
unraverl / partial_application.erl
100644 53 lines (37 sloc) 1.663 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
-module(partial_application).
-export([partial_application/1]).
 
-define(compare_fun, fun({call, _, {atom, _, FuncName}, ArgsList}) ->
is_partial(Form, FuncName, ArgsList);
(_) -> false
end).
 
-define(replace_fun, fun({call, LineNum, {atom, _, FuncName}, ArgsList}) ->
RealArity = real_arity(Form, FuncName, ArgsList),
ArityDiff = RealArity - length(ArgsList),
FunArgList = util:args_list_form(ArityDiff, LineNum, "FunArg"),
?partialy_applied_function
end).
 
-define(partialy_applied_function, {'fun',LineNum,
{clauses,
[{clause,LineNum,
FunArgList, %fun arg list [{var,52,'Arg3'}],
[], %guards
[{call,LineNum,
{atom,LineNum,FuncName},
ArgsList ++ FunArgList }]}]}}). %Args to pass [{var,52,'Arg1'}]
 
 
%% figure out why least_arity is returning 0
 
 
partial_application(Form) ->
    util:replace(Form, ?compare_fun, ?replace_fun).
 
is_partial(Form, FuncName, ArgsList) ->
    Arity = length(ArgsList),
    real_arity(Form, FuncName, ArgsList) > Arity.
 
least_arity(Funcs) ->
    least_arity(Funcs, 0).
 
least_arity([{_, _, _, Arity, _}|T], Max) when Arity >= Max ->
    least_arity(T, Arity);
 
least_arity([{_, _, _, Arity, _}|T], Max) when Arity < Max ->
    least_arity(T, Max);
 
least_arity([], Max) -> Max.
 
real_arity(Form, FuncName, ArgsList) ->
    least_arity(find_greater_equal_arity(Form, FuncName, length(ArgsList))).
     
 
find_greater_equal_arity(Form, Name, Arity) ->
    [{SType, LineNum, SName, SArity, Clauses} || {SType, LineNum, SName, SArity, Clauses} <- Form, Name == SName, Arity =< SArity].