Replacing Call-Site Code in Functional Programming Languages

I'm just wondering if this is possible in a functional language to replace a site. I imagine this as a special form of laziness.

Basically, I would call somewhere the function f with argument a:

... (fa) ... 

Then the function could replace itself with function g with argument b:

 ... (gb) ... 

So, next time the original call site (fa)
actually called (gb).

Greetings are welcome.

Bye

PS: its for compiling Just-In-Time (JIT).
Polymorphic built-in caches, etc.
See for example:

Optimization dynamically - typed object - oriented
Languages ​​with polymorphic embedded caches
Urs HΓΆlsle, Craig Chambers, David Ungar
ECOOP '91 Proceedings of the European
Object Oriented Programming Conference
http://selflanguage.org/_static/published/pics.pdf

+4
source share
2 answers

Basically arrow machine:

 newtype Auto ab = Auto (a -> (b, Auto ab)) 

In Haskell, it is not possible to replace a function on its own, but the machine arrow is a function that returns a new version of itself along with the result:

 switcher :: Bool -> Auto Bool Bool switcher x = Auto $ \y -> (x, switcher $ if y then not x else x) 

The useful thing about the arrow of an automaton is that it is an arrow, so the Category instance allows you to create such functions. There is also a very useful applicative example.

Side Note: This is the foundation of functional reactive arrow programming (AFRP).

+13
source

In Haskell, you can create custom optimization overrides using the RULES pragma.
See the GHC User Guide> Rewriting Rules for more information.

+2
source

Source: https://habr.com/ru/post/1441346/


All Articles