Skip to main content

Command Palette

Search for a command to run...

A Language with context

Updated
5 min read
A Language with context

Have you ever heard about Rebol?

It is programming language born nearly 30 years ago and it is the invention of a genius: Carl Sassenrath, the author of the Amiga multitasking microkernel.

Now it has reached release 3 and it is mantained by Oldes in his R3 Fork

Why is Rebol so special? Because it mimics the our way of using words.

The human way

Humans are interpreters of the language and they understaning it and its words (symbols), giving them a meaning depening on the context. A word may have different meaning if the context is different. Take as an example the word PLAY, we don’t use it in the same way everywere but its meaning is inferred from the context it is used.

What is a context?

If someone starts talking about an instrument, an auditorium and then about a musician, when you hear to word PLAY, you think about him playing a song; if someone starts talking about a football team, a stadium and a competition, a team will PLAY a game for their amusement and the spectators one.

So, depending on the context built in your mind by the flow of words, PLAY has a different meaning.

It can be a verb

  • To engage in recreational activities: This includes playing games, sports, or other forms of amusement.

  • To act, deal, or touch carelessly: This definition implies a light, speculative, or sportive manner.

  • To perform on a musical instrument: This refers to producing music with an instrument.

  • To act or perform in a play: This refers to acting a part or role in a dramatic performance.

  • To give out sounds, especially musical sounds: This definition focuses on the sound aspect of playing an instrument.

It can be a noun

  • A dramatic performance: This refers to a play performed on stage or in a theater.

  • A written work: This refers to the script of a play, including dialogue and story.

  • A game or other recreational activity: This refers to the activity of engaging in a game or other form of amusement.

  • A skill or trick: This definition refers to a deliberate coordinated movement requiring dexterity and skill.

  • The time during which a game proceeds: This refers to the duration of a game or performance.

  • Verbal wit or mockery: This definition refers to a pun or other form of playful mockery

A context can be inferred by the use of the words and a word has meaning in a contex.

Rebol and contexts

While all the other languages usually had some kind of scope for their words, Rebol implements contexts: vocabulary of words specialized in certain areas. So if you are creating a program to play a song and also to show the text of a dialogue, Rebol is able to maintain 2 meanings to the word PLAY. In one context, you define a function that starts the audio playback of a song; in another context the word PLAY is a container with a dialogue.

Is this difficult to implement? No, it is very simple!

ctx1: context [
    play: function [song-number][...code-here...]
    Performers: "Pink Floyd"
]

ctx2: context [
    play: {
        This is a drama written 18th century.
        The story if a princess....
    }
    performer: "The Actors of Hollywood"
    Roles: [
        MAIN-CHARACTER "Jennifer"
        SECONDARY "Romeo"
        OTHERS [
            "Michael"
            "Lucy"
            "Andrea"
    ]
    imdb: "0120384301"
]

A context may contain infinite words or zero.

A Vocabulary

As written, basically a context is a vocabulary. In Rebol you may have as many vocabularies as you want. Your script starts always with the Rebol default one. While you code perform actions, you can create anothe vocabulary where words have different meaning and do or contain different things. They are specialized into a different task and form a different context.

Lets focus on creating a vocabulary to work with songs:

ctx-audio: context [
    play: function [song-number] [...code-here...]
    get-title: func [song-number] [...code-here...]
    current-song: 2
    album-title: "The Dark Side of the Mood"
    artist: "Pink Floyd"
    tracks: [
        "Speak to Me"
        "Breathe (In the Air)"
        "On the Run"
        "Time"
        "The Great Gig in the Sky"
        "Money"
        "Us and Them"
        "AnyColour You Like"
        "Brain Damage"
        "Eclipse"
    ]
]

As you can see, the vocabulary forming this contexts, has words meaningful for the task of playing the songs of a musical album

For example, if we want to get the tittle of the song 3 and print it on screen, using this vocabulary you can write as follows:

print get-title 3

How we tell Rebol to use this context for this phrase?

Binding phrases to a vocabulary

Each Rebol program is a block with words and other datatypes like numbers

So you express the above code as:

[print get-title 3]

Now you need to instruct Rebol to use the words in the ctx-audio vocabulary

To connect the words of a block to a specific vocabulary so they are searched there, you use the word bind, it contains the code that performs the connection we need.

[bind [print get-title 3] ctx-audio]

Using bind, all the words of the block will be connected to the ones in ctx-audio context if defined there.

At the end of the connection process, bind returns the code block [print get-title 3]

bind is defined in the default Rebol vocabulary and available at start.

Running the code

Now we can run the code as follow:

[do bind [print get-title 3] ctx-audio]

do is another word available in the default vocabulary and it executes the code following it.

Note: it does not execute bind but the result of it, which is:

[print get-title 3]

The final code that will run is

[do [print get-title 3]]

Final words

We have written that ctx-audio is the vocabulary of words specialized into managing audio content.

The vocabulary and the context refers to the same Rebol element but they are not equivalent. A context is something more, it is a reflection of the vocabulary in your mind and the knowledge about the elements and the dynamic you have about something.
In my articles I will talk about context and vocabularies in a mutually exclusive way.

The Context

Part 1 of 1

Here we cover the basic of Context as implemented in Rebol and similar languages like Red