carp-docs

Dynamic Semantics

This document will help us rewrite Carp’s dynamic evaluator (defined in Eval.hs) to make it behave in a more principled and non-surprising way.

Goals of the rewrite

Relevant issues

Desired features (currently missing)


Index

[TODO]

0. Terms used in this document

1. Scoping Rules

Related issues:

Questions:

How does Carp figure out what the value of the symbol X is?

Lexical scoping (look in the current scope, then any enclosing scope, up until global scope). Things that create scopes:

How do you set the value for symbol X?

(set! <symbol> <value>)

Are there any reserved names?

Yes (see the Parsing module for more info)

More things should be moved to the reserved list, actually. The :rest token in defmacro is also reserved.

What is a keyword?

There are no keywords. Maybe will be in the macros, see this implementation https://gist.github.com/sdilts/73a811a633bb0ef3dd7e31b84a138a5a.

Are there different namespaces for dynamic and static Carp?

They use the same modules but dynamic lookup will only find dynamic functions, and static lookup will only find static functions.

1.1 Global Variables

Questions:

Are global variables mutable?

Yes.

How are they mutated? When do these mutations come into affect?

Using set!. The mutation comes into effect immedately (using IORefs internally).

Do global variables have lexical or dynamic scope?

Lexical (no dynamic scope for anything).

1.2 Local variables

Questions:

Are local variables mutable?

Yup.

When do local variables come in and out of scope?

Lexical scoping rules, functions and let create new variables.

What is a closure? What are the important rules for variables inside closures?

No captured variables are mutable. The dynamic lambdas captures the whole environment at the time the closure is created (when the (fn ...) form is evaluated).

1.3. Namespace Rules

Questions:

Given symbols a in the Foo module and a in the Bar module, how do I refer to each of them?

Using ., Foo.a and Bar.a. By using (use <module name>) you can avoid having to specify the module.

What happens if multiple modules are imported and they contain the same symbol?

Runtime error when looking up the symbol.

Given the symbolsFoo.a and Bar.a, exist, which symbol does a refer to?

Neither, unless any single one of the modules (Foo/Bar) is imported with use. If both are imported the lookup is an error since it can’t be resolved to a single value.

Do functions and variables live in the same namespace?

Yes. Types live in a different namespace.

1.4 Definitions

Questions:

What kinds of definitions are there and how are they created?

Dynamic context:

Static context:

All contexts:

2. Evaluation Rules

Related issues:

Questions:

When are macros evaluated?

When are symbols evaluated?

When are forms evaluated?

Are forms evaluated left-to-right or right-to-left?

How does error reporting work?

2.1 Macros

Questions:

2.2 REPL

Questions:

3. Types

Issues:

Questions: