Wednesday, September 6, 2006

Simple ML5 Example

More nerdy stuff. If you don't like computers, consider yourself warned.

So it has taken kind of a while for me to really appreciate Tom's language. I have had to go back and forth with him a little bit, but here is an example that I wanted to be able to express in my own language, and now can use his to do.

Here's the set-up:
  1. This function takes in a list of integers that resides on another host.
  2. The local host wants to know if the sum of that list is greater than twelve. However, the summation should occur remotely, and only the boolean value should be returned over the network.
  3. <> means 'diamond,' the 'exists at some other world' type from modal logic.
Here's the code:

fun remote_sum( l: <>(int list)@W ) : bool =
letd <wr, a, y,> = l
in
get[wr; a]
(let
fun sum il = foldl op+ 0 il
in
if sum y > 12 then true else false
end)
end;


Here are the types (for variables and at various points in the code):
y: int list @ Wr
Wr: Wr world, whichever remote host the list is on.
W: W world, AKA this host.
a: Wr addr @ W
l: <>(int list) @ W

sum: (int list @ Wr) -> int @ Wr

*inner let expression*: bool @ Wr
*get expression*: bool @ W

Notes:
The 'get' statement takes this remote bool and makes it into a local bool. This is where the network transfer actually occurs, and this is acceptable because bool is a mobile type (like all base types).

1 comment:

  1. Hmm... this sucked.
    Anyone know how to format code in a nice way on Livejournal?

    ReplyDelete