In a 'pure' language like Haskell I gather you would normally do this with a monad. Create a transform which works modulo some state wrapped in the monad, and hide the monad by returning the result after the monad was ran to completion. I have never programmed a Haskell monad in my life but somehow the C++ impure solution looks better to me.
I want to do something similar like C++/Haskell in Hi and am thinking of extending the language with namespace local implicits. The idea is that it should work like below, a namespace local implicit is a constant threaded implicitly through the functions in a namespace.
namespace implicit [ 0: int ] (
using system
def inc: int =
this := this + 1; this
def map_inc: list int -> list int =
[ nil -> nil
| cons x xx -> cons (inc + x) (map_inc xx) ]
)
The idea would be that on calling a function in the namespace implicit, it is augmented with the implicit constant. A function within the namespace can refer to that constant with the 'this' keyword, and also manipulate the value threaded with something which looks like an assignment (but in it's operational semantics simply means: thread this new value along instead of the old value, although it may be implemented as a variable); every function called from within that namespace receives the (updated) implicit; leaving the namespace stops the threading and the implicit is lost.
Given these semantics, running the above function 'map_inc' on the list '0,1' should deliver the following result '1,3':
implicit.map_inc (cons 0 (cons 1 nil))
=
(cons 1 (cons 3 nil))
No comments:
Post a Comment