Formal Model¶
Environment¶
1. Name-Value Mapping¶
Environment (Γ) maps names to values:
# Environment example:
# Γ = {x: 42, y: "hello", z: [1, 2, 3]}
x = 42
y = "hello"
z = [1, 2, 3]
2. Notation¶
Formal notation:
Γ[x ↦ v] # Bind x to value v in Γ
Γ(x) # Lookup x in Γ
Binding Rules¶
1. Simple Binding¶
# Before: Γ = {}
x = 42
# After: Γ = {x: 42}
# Before: Γ = {x: 42}
y = "hello"
# After: Γ = {x: 42, y: "hello"}
2. Rebinding¶
# Before: Γ = {x: 42}
x = 100
# After: Γ = {x: 100}
Evaluation¶
1. Expression Eval¶
Γ ⊢ e ⇓ v
Means: "In environment Γ, expression e evaluates to value v"
2. Example¶
# Γ = {x: 10, y: 20}
# Γ ⊢ x + y ⇓ 30
x = 10
y = 20
result = x + y # 30
Scope Chain¶
1. Nested Environments¶
# Global: Γ_global = {x: 10}
x = 10
def f():
# Local: Γ_local = {y: 20}
# Parent: Γ_global
y = 20
return x + y
2. Lookup Chain¶
Γ_local → Γ_enclosing → Γ_global → Γ_builtin
Assignment Semantics¶
1. Create Binding¶
# Γ ⊢ x = e ⇓ Γ[x ↦ v]
# where Γ ⊢ e ⇓ v
x = 42
# Evaluates 42, binds x
2. Multiple Assignment¶
# Γ ⊢ x, y = e1, e2 ⇓ Γ[x ↦ v1, y ↦ v2]
x, y = 1, 2
Function Application¶
1. Environment Extension¶
def f(x, y):
return x + y
# Call: f(10, 20)
# Creates: Γ_f = Γ_global[x ↦ 10, y ↦ 20]
2. Formal Rule¶
Γ ⊢ f(e1, e2) ⇓ v
where:
Γ ⊢ e1 ⇓ v1
Γ ⊢ e2 ⇓ v2
Γ' = Γ[x ↦ v1, y ↦ v2]
Γ' ⊢ body ⇓ v
Summary¶
1. Core Concepts¶
- Environment: name → value mapping
- Binding: Γ[x ↦ v]
- Lookup: Γ(x)
- Evaluation: Γ ⊢ e ⇓ v
2. Operations¶
- Create binding
- Update binding
- Lookup value
- Extend environment