[Grace-core] fixing REPL's

Kim Bruce kim at cs.pomona.edu
Tue Apr 30 17:08:25 PDT 2013


The topic of REPL's and how code typed in there differs from code in a complete object.  I'm not sure they are so different if we understand the code appropriately.  Take a look at the following session from Haskell:

Prelude> let sqr x = x * x
Prelude> let sqq x = (sqr x) + 1
Prelude> sqq 5
26
Prelude> let sqr x = x + 1
Prelude> sqq 5
26

Notice that the redefinition of sqr had no impact on the meaning of the function sqq, which was defined earlier and used the original definition of sqr.  I believe that this is what we should be looking for as well.  Otherwise we have to get into all the complexities of dynamic scoping and all the associated problems with accidentally reusing a name.

The way Haskell (and many/most? languages with REPL's) works is that each new line opens a new embedded scope.  That is, the above code is interpreted as
let sqr x = x * x in let sqq x = (sqr x) + 1 in let sqr x = x + 1 in sqq 5 end end ...

(I left out the first call of sqq 5 for simplicity).

We should treat the Grace REPL similarly.  Each new line should enter the new definitions in a new embedded scope.  Now, I guess we don't have simple mechanisms for opening new scopes, but it seems like we could just be adding new object scopes at each new line:
object{
       method sqr ...
       object {
             method sqq x = ...
             object {
                    sqq 5
                    object{
                         method sqr x = ...
                         object {
                               sqq 5
                         }
                     }
               }
          }
}

Of course the calls have lots of implicit "outer" prefixes, but that shouldn't be a problem.

The only possible problem is the fact that sqr has two defs in the same scope.  We could either allow that or just raise an error and tell the programmer that the function is already defined.  Whatever we decide fro the rest of the language could kick in here.

It seems like this would resolve all the issues with REPL's.  Have I missed anything?

Kim





More information about the Grace-core mailing list