Up to Main Index                            Up to Journal for August, 2024

                    JOURNAL FOR SATURDAY 31ST AUGUST, 2024
______________________________________________________________________________

SUBJECT: WolfMUD and The Matrix Reloaded + Variable scoping
   DATE: Sat 31 Aug 22:11:14 BST 2024

I finished preparing my presentation on AI, ready for yesterday. I spent a lot
of time choosing prompts that showcase an AI’s capabilities. Unfortunately,
all my hard work was unexpectedly rescheduled for next week :(

I’m going to chat about progress on Mote, Mere’s successor, in a bit. Before I
do, I’d like to answer a question that often comes up: why am I so fixated on
Mere and putting a scripting language into WolfMUD. It seems my previous
attempts at answering this question were not very successful… let’s try again…

The original inspiration came from watching The Matrix Reloaded (2003). In the
film there is a scene, just before Neo fights an army of Smiths, where Neo and
the Oracle are talking:


  Neo: Are there other programs like you?

  The Oracle: Oh, well, not like me. But… look, see those birds? At some point
  a program was written to govern them. A program was written to watch over
  the trees, and the wind, the sunrise, and sunset. There are programs running
  all over the place. The ones doing their job, doing what they were meant to
  do, are invisible. You’d never even know they were here. But the other ones,
  well, we hear about them all the time.

  (Source: IMDB[1])


The instant I heard those lines, something deep in my mind resonated. I had a
sudden vision of all these little programs running within WolfMUD. Tiny bits
of code running independently, instead of a huge monolithic codebase with
everything hard-coded. I’ve held on to these ideas for a very long time. By
creating Mere and having Mere scripts within WolfMUD’s zone files I think I
will be able to finally realise those ideas.

Thanks to the Wachowskis for the inspiration! :)

After that brief and nicely weird detour, a quick Mote progress update.

Over the past week I’ve been addressing an issue with how Mote’s scoping
behaves. I thought I’d nailed it already, but there is one corner case that
threw a huge spanner in the works. Consider the following:


    call fn 0

    func fn c
      is kind a == "undefined"; a = 0
      a++
      c++
      is c < 5; call fn c
      println "a: " a " c: " c
    end


It should be noted that this line in the code:

    is kind a == "undefined"; a = 0

is an odd way to initialise ‘a’. It is used here in order to illustrate the
problem only. Specifying a normal initialisation without the check ‘a = 0’
would mask the issue. When run, the above code should output:


    a: 1 c: 5
    a: 1 c: 4
    a: 1 c: 3
    a: 1 c: 2
    a: 1 c: 1


The variable ‘a’ in this function ‘fn’ is local and should be initialised to 0
on each call. The parameter ‘c’ is local and passed on each call.

However, this is the actual output produced by the above code:


    a: 5 c: 5
    a: 5 c: 4
    a: 5 c: 3
    a: 5 c: 2
    a: 5 c: 1


From the output, the local variable ‘a’ seems to be behaving more like a
static variable. This was because the variable ‘a’ was visible to itself on
the call stack when the scopes were being traversed.

After tip-toeing around the problem for ages, I reimplemented a lot of the
scoping code. Now in Mote all variables are local, constants are global within
scope. In order to reach a variable in an outer scope the variable name should
be prefixed with a dollar ‘$’ symbol. If a constant is redefined locally it’s
outer definition can be reached by also using a dollar ‘$’.

For example, consider the following code:


    const a = 3  # define global constant
          b = 5  # define local variable
    call fn

    func fn
      const a = 7  # local constant, overrides global
            b = 11 # local variable

      println " local a: " a
      println " local b: " b

      println "global a: " $a  # global constant
      println "global b: " $b  # outer scope
    end


When run, the above code produces the output:


     local a: 7
     local b: 11
    global a: 3
    global b: 5


The dollar ‘$’ notation can also be used to update variables in an outer
scope. For example, consider the following:


    a = 5
    println "before call a: " a
    call fn
    println " after call a: " a

    func fn
      $a++
    end


When run, the above code produces:


    before call a: 5
     after call a: 6


I’m not overly keen on having to use the dollar ‘$’ to reference outer scopes.
Syntactic details aside, the solution itself seems to be working quite well.

Fun fact: To an AI, Mere/Mote code resembles a curious mixture of Pascal,
Fortran, Modula-3 and Obliq. It’s actually more inspired by BASIC, BASH, Perl
and Go ;)

--
Diddymus

  [1] IMDB, The Matrix Reloaded (2003), Neo/Oracle quote:
      https://www.imdb.com/title/tt0234215/quotes/?item=qt0283096&ref_=ext_shr_lnk


  Up to Main Index                            Up to Journal for August, 2024