On May 18, 2009, at 11:21, Anand Patil wrote:

> Huh, sounds like just the thing. Security is going to be especially  
> difficult in Clojure, though. For example, say I stuck the array  
> into a ref from within the monad, then carried on overwriting it. A  
> consumer who gets the array out of the ref from another thread  
> might get it while I'm still mutating it.

That is not possible: the monad provides operations on the array, but  
all references to the array are implicit. There is no way to get the  
reference out of the monad and pass it to a function or assign it to  
a var. Well, *almost* no way: as always in Clojure, you can reverse- 
engineer the implementation to figure out how the reference is  
stored, and then access it directly. But that is not likely to happen  
by mistake.

Here is an example of how this might look, using 1D Java arrays for  
simplicity:

(def an-array
   (initalize-array Integer/TYPE 10
     [_ (set-element 0 1)
      x (get-element 0)
      _ (set-element 1 (inc x)]))

This would create an integer array of length 10 (make-array Integer/ 
TYPE 10) and then execute the sequence of monadic operations on it.  
The available operations are (get-element i) for getting an element  
and (set element i v) for changing an element's value. As you can  
see, these operations do not have an explicit reference to the array.

Using the standard operations on monads, one could then define more  
complex array initializers in terms of get-element and set-element.  
None of them would ever contain an explicit array reference, and none  
of them could be applied to an already existing array, except by  
using "forbidden knowledge".

BTW, the above expression would expand into

(def an-array
   (let [a make-array Integer/TYPE 10]
          init (domonad state-m
                  [_ (set-element 0 1)
                   x (get-element 0)
                   _ (set-element 1 (inc x)]
                  nil)]
     (second (init a))))

but that is already forbidden knowledge, as it shows how to inject an  
array into a function that modifies it. The monad could be made  
private for additional protection.

Konrad.




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to