The inverse of a function...
Thomas Arts
thomas.arts@REDACTED
Wed Mar 10 12:31:40 CET 2004
Indeed Erik, it is possible, but not without analysing
source or binary. What you describe is re-interpretation of
the source (even though you do it on a binary level).
Functions with side-effects have no well-defined inverse.
Functions without side-effects have an inverse, but
even if one is given the source code, it is not always
easy to identify the inverse. If one could, well, then
one solved half the type-checking problem; just compute
all the domains for all functions.
/Thomas
---
Dr Thomas Arts
Program Manager
Software Engineering and Management
IT-university in Gothenburg
Box 8718, 402 75 Gothenburg, Sweden
http://www.ituniv.se/
Tel +46 31 772 6031
Fax +46 31 772 4899
----- Original Message -----
From: "Erik Stenman" <Erik.Stenman@REDACTED>
To: "'Question Erlang'" <erlang-questions@REDACTED>
Sent: Wednesday, March 10, 2004 11:09 AM
Subject: RE: The inverse of a function...
> Corrado Santoro wrote:
> > Hi Erlang gurus,
> > I have a function like this:
> >
> > f(caesar) -> ...;
> > f(caius) -> ....;
> > f(giulius) -> ....
> >
> > I would like to have a function that, given the name 'f', returns the
> > list [caesar, caius, giulius] (or similar).
> > Is it possibile to do this (obviously without re-interpreting the
> > source file)?
>
> Actually, contrary to what has been said before on this list
> by very knowledgeable persons, it is possible, at least as long
> as the function f has no side effects, and the arguments are only
> atoms.
>
> -module(hack).
> -export([find_keys/2,f/1]).
>
> find_keys(Module,FunctionName) ->
>
>
{value,{atoms,As}}=lists:keysearch(atoms,1,element(2,beam_disasm:file(Module
> ))),
> Atoms = [A || {_,A} <- As],
> [A || A <- Atoms,
> case catch apply(Module,FunctionName,[A]) of
> {'EXIT',_} -> false;
> _ -> true
> end].
>
> f(caesar) -> 1;
> f(caius) -> 2;
> f(giulius) -> 3.
>
>
> ---
> 19> c(hack).
> {ok,hack}
> 20> hack:find_keys(hack,f).
> [giulius,caius,caesar]
> 21>
>
>
> OK, I know it is a hack, but what the heck, I could not resist.
>
> Erik
>
>
More information about the erlang-questions
mailing list