Chapter 1Foundationsslides.en.pdf:99-101

Declarations and Scope

How identifiers are introduced and where they live

Def 2.6DeclarationDef 2.7ScopeDef 2.8Naming complex objects
Concept

A declaration introduces an identifier into scope. In mathematics (and SML):

  • Simple definition: gcd(a,b):=\text{gcd}(a, b) := (some expression).
  • Pattern definition: succ(0):=1,succ(s(n)):=s(succ(n))\text{succ}(0) := 1, \text{succ}(s(n)) := s(\text{succ}(n)).
  • Implicit definition / description: "the smallest xx such that P(x)P(x)".

The scope of an identifier is the part of an expression where it refers to
its declaration. Outside the scope, the name is either unbound or shadows
another declaration.

Examples/counterexamples: To prove a x.P(x)\forall x. P(x) statement, a single
xx where P(x)P(x) fails is enough — a counterexample.

Animation — declarations scope
Transcript — click a line to jump8 cues
  1. 0.0sDeclarations and Scope
  2. 1.1sAn outer cyan rectangle: let outer in ... end
  3. 2.2sAn inner amber rectangle: let inner in ... end
  4. 3.6sOuter binding: x = 5
  5. 4.7sInner binding: x = 99 (shadows the outer)
  6. 6.3sArrow from outer binding to its use x = 5
  7. 7.5sArrow from inner binding to its use x = 99
  8. 9.0sInner binding shadows the outer one
Practice — score 100% to advance
Multiple choice
Q1
What is a declaration?
Q2
What is the scope of an identifier?
Q3
Which is a counterexample to 'every prime is odd'?
Q4
Pattern definitions are useful when…
Loading…