LtU Forum

Monitoring whether a function happens

I'm fairly new to functional programming. I found myself gravitating towards it without knowing what it was (oddly enough, I did the same with object-orientated programming about 10 years ago).

I'm most interested in avoiding destructive updates, but there's something I can't get.

In a test, I want to know whether a particular function has been run. I'm not sure how to go about that.

In the Clean way, the function that calls that function would return a new World (I think) that somehow returns 'yes' when asked whether that function has been run.

In the Haskell way, I would use monads to emulate a mutable variable (I think).

I'm actually using Java, the program is event-driven (Swing). I think perhaps the World-passing metaphor is invalid because I don't control the code that calls my event handlers (and they ultimately return nothing [void]) anyway.

The actual case is that when a particular label on the screen is changed, I want it to flash. I have a little animation routine that changes its background colour appropriately, but in one case, it doesn't happen. So my test should detect this. I can only see an imperative way of solving that - by adding a PropertyChangeListener that modifies an array element or equivalent to determine whether the 'background' property was changed.

Is there another way - a suggested non-imperative way? I don't mind if the suggestion isn't related to Java, or if it ultimately is invalid in Java - I'm really more interested in the concepts at the moment - obviously I can do imperative stuff in Java to make this test fail (and then fix the bug so that it passes).

Want to learn something new

Hello LtU's,

I'm new here, although I've been visiting this site for more than a year. I'am a CS student, interested in programming languages and compiler construction. I'm good at C, Java, C#, doesn't matter. I love Lisp and read about it more than I did with any other language, but I never really used it much. I also love Ruby and try to use it everywhere I can.

The thing is, ever since I learned Ruby, I never had the urge to learn any other programming language, and I'm starting to feel like I'm missing something.

So, is it Scala? Haskell? Nemerle, OCaml? Anything else? Which one should I learn now to become a better programmer?

Thanks,

Luís Pureza

P.S.: By the way... any Book recommendations? Thanks again.
P.S.2: Wondering if this is the right place to ask this... Please, feel free to delete this post if it isn't.

Continuations, yield, ES4

My language of choice for everyday coding is EcmaScript, thus I am very interested in everything new in this area. I'm mostly happy with the language, the only thing I miss in it seems to continuation support. I need them to simplify my work with asynchronous processing, that seems to happen very often in my development. I want them in order to convert this async processing to look like synchronous.

So for example when I need to perform a bunch of animations one after one I have to write just

for(var i=0;i<animations.length;i++)
  performAmination(animations[i])

What I need to write now instead is some hard to understand code.

Another asynchronous thing is communication with server (AJAX).

var someResult = serverProxy.callSomeFunc(arg);
var otherResult = serverProxy.callOtherFunc(someResult+1);

Now I am writing this in CPS stile, but it is a bit tedious.

serverProxy.callSomeFunc(arg,function(someResult){
  serverProxy.callOtherFunc(someResult+1, function(otherResult){
  //process something here
  });
})

With continuations I could hide all the async-sync conversion in proxy and all the code will just use it in synchronous way.
So basically, I want SJAX instead of AJAX :-)

I know that JavaScript 1.7 in Firefox 2.0 supports yield keyword and generators.
But I do not know whether it is possible to achieve such kind of conversion using only yield. So that is my first question - is that possible at all.

Rhino already has full continuations support, and such things is possible to do in Rhino, but there is no Rhino in web-browser and in Flash player.

And second question is - does somebody know whether ES4 will support continuations or just yield. Somewher I read that Brendan Eich promised continuations, but I didn't find this in ES4 draft.

Cat Version 0.9

I've made a major update to the Cat manual and posted a new version of the Cat interpreter and source code which is now compatible with Mono (but so far only tested on Mono 1.13.8 under Windows XP). The type checker / type inference engine is now much more mature and well-tested.

The Cat interepreter and source code is all released entirely into the public domain.

Any comments, suggestions, or bug reports much appreciated!

Growing interest in Smalltalk?

I thought this was interesting - maybe there is more interest in Smalltalk now that Strongtalk has been open-sourced?

Cadence Design Systems, the leader in Electronic Design Automation, is seeking outstanding software engineers for a novel technology project at its San Jose, California campus.

We are seeking developers with the following qualifications: Object Oriented design and modeling Advanced degree in Computer Science Working experience with Smalltalk/Squeak desirable Exposure to formal methods Exposure to system design languages (UML, Mathlab/Simulink, System- C, Esterel), and system design flows an advantage Ability to work in a fast paced industrial environment Current US work permit.

Interested parties should contact Gilad Bracha (Gilad@Cadence.com).

Also interesting is Ian Piumarta's work on Pepsi, although I haven't had a chance to look at all the details yet.

The Type of 42

The following is a reposting from my blog at http://www.artima.com/weblogs/viewpost.jsp?thread=182140.

When I was designing Cat I started with the assumption that the type of a number such as 42, must be a function which pushes a primitive integer type onto the stack. So the type of 42 would be:

42 : ()->(int)

The type of a function which yielded 42 would be:

[42] : ()->(()->(int))

In Joy the operation for evaluating functions is "i". The equivalent in Cat is "eval". The type of eval in Cat currently is:

eval : (A (A:any*)->(B:any*))->(B)

This meant that the following was badly typed in Cat:

42 eval

This is because eval expected a function on the top of the stack, but 42 produced a primitive on the top of the stack. You would have to instead write:

[42] eval

The decision for this type design was based on two things:

  1. That kind of type system was more familiar to me
  2. The programs more closely mapped to assembly language as-is

In assembly an operation which yields 42 and the value 42 are very different things. My thought was that I wanted to be able to trivially map Cat functions to assembly code. My concern is that the execution loop of an assembly program where values were executable would be non-trivial. I assumed you would need a switch statement and this would signficiantly hurt performance (Cat is striving to become a full competitor with real assembly code, so performance is a big concern). I am no longer convinced that this would actually be a problem.

The first issue is writing the type of numbers. It appears that what is needed is the ability to name new types and express them recursively:

42 : int;
int = ()->(int);

The next challenge is converting to assembly code. I haven't worked out all of the details, but I believe that when generating assembly, I need to simply insert type conversion routines at key places (i.e. where an integer is being executed, or treated like a function).

Do all this seem sane and rational?

GADT vs. Type Classes

I'm looking for a paper or tutorial which compares/contrasts generalized algebraic data types and type classes (as found in GHC/Haskell). I get the feeling that they are some how duals of each other, and that there may be some overlap of the capabilities of each, but I don't know if I understand the essential differences which makes both desirable. For example, the standard GADT example of a tagless interpreter looks pretty similar to an equivalent built using type classes. In fact, the code for "eval" is identical, only the type annotations differ. Any thoughts, comments?
data Term a where
    Lit  :: Int -> Term Int
    Inc  :: Term Int -> Term Int
    IsZ  :: Term Int -> Term Bool
    If   :: Term Bool -> Term a -> Term a -> Term a
    Pair :: Term a -> Term b -> Term (a,b)
    Fst  :: Term (a,b) -> Term a
    Snd  :: Term (a,b) -> Term b 
  
eval :: Term a -> a
eval (Lit i)    = i
eval (Inc t)    = eval t + 1
eval (IsZ t)    = eval t == 0
eval (If b t e) = if eval b then eval t else eval e
eval (Pair a b) = (eval a, eval b)
eval (Fst t)    = fst (eval t)
eval (Snd t)    = snd (eval t)
data Inc a    = Inc a    
data Lit a    = Lit a   
data IsZ a    = IsZ a   
data Pair a b = Pair a b
data If p t e = If p t e
data Fst a    = Fst a   
data Snd a    = Snd a  

class    Eval a b | a -> b                      where eval :: a -> b
instance Eval (Lit a) a                         where eval (Lit i)    = i
instance (Num b, Eval a b) => Eval (Inc a) b    where eval (Inc t)    = eval t + 1 
instance (Num b, Eval a b) => Eval (IsZ a) Bool where eval (IsZ t)    = eval t == 0
instance (Eval a Bool, Eval b d, Eval c d) => 
          Eval (If a b c) d                     where eval (If p t e) = if eval p 
                                                                         then eval t
                                                                         else eval e
instance (Eval a c, Eval b d) =>    
          Eval (Pair a b) (c,d)                 where eval (Pair a b) = (eval a, eval b)
instance (Eval a (b,c)) => Eval (Fst a) b       where eval (Fst t)    = fst (eval t)
instance (Eval a (b,c)) => Eval (Snd a) c       where eval (Snd t)    = snd (eval t)
...and here's a driver program which will work for either snippet...
{-# OPTIONS -fglasgow-exts #-}  
{-# OPTIONS -fallow-undecidable-instances #-}  

main = print $ (eval stuff)

stuff = Pair (If (IsZ (Inc (Inc (Lit 5))))
                 (Lit 6)
                 (Lit 7))
             (Fst (Snd (Pair (Lit 42)
                             (Pair (Lit 8) (Lit 9)))))

Clorox - AJAX shared memory

Clorox is shared memory for AJAX applications. It provides data structures that look exactly like ordinary JavaScript objects, but that make RPC calls behind the scenes to fetch data. This means that programmers don't need to think about callback functions, and Clorox also makes it very easy to define policies for caching and prefetching of data to boost performance. An open-source (Mozilla License) implementation is available at http://clorox.csail.mit.edu, and feedback is very much appreciated!

Question on terminology

Is there a generic term for deriving lower-order concept form a higher-order concept? I'll illustrate this question with the following example:

A language has concept of class. A class is 'defined' by notion:

class myclass ...;

A class is instantiated by notion

myclass myvar = new myclass();

Instance of a class is an object and it appears to be a first-order concept. A particular class is a second-order concept, and 'class' as a notion of a class concept is a third-order concept.

In MPS their are giving ability to "define" (?) third-order concepts with notion of fourth-order concept noted as "concept", e.g.:

concept class;

Thus, we may say that in each of these examples a lower-order concept is "derived" from a higher-order concept.

What is the right term for this act?

Constraint based hybrid typing and the Curry Howard Isomorphism

I'm working on a constraint based hybrid type system. The idea is to use a constraint as the type of all the value that it could be bound to. For example a finite domain range constraint like 1..7 would be the type of integers between 1 and 7. By default this test would be done at runtime, but as there are some fairly strong ties between type inference and checking, and constraint propagation I think that it should be posible to move a lot of the checking to compile time.

But the problem is that 1..7 would not ony have integers as members but also other ranges like 2..4, and most importantly, it would be a member of it self. Actually as any value could be considerd as a constraint, every term vould be a type (and a kind) and a member of itself. This would wreck havoc on the Curry Howard Isomorphism, as every proposition would be a prof of itself, and thats a rather meaningless logic language.

But im not that good at type theory so I wonder exactly what do I lose by not being able to use CHI and other type theoretical works based on it?

XML feed