Skip to content

Commit b44c7e7

Browse files
committed
translating glossary for EuroPython 2022
1 parent f497548 commit b44c7e7

15 files changed

+214
-42
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#lang racket/base
2+
3+
(require rackunit
4+
"concrete2.rkt")
5+
6+
(check-false (ASSQ 'x '()) "Empty bindings")
7+
(check-equal? (ASSQ 'x '([x . 8])) '(x . 8) "First binding")
8+
(check-equal? (ASSQ 'x '([y . 7] [x . 8])) '(x . 8) "Older binding")
9+
(check-equal? (ASSQ 'z '([x . 8])) #f "No binding")
10+
11+
(check-equal? (MAKE-FRAME '(a) '(1)) '([a . 1]) "Smalest frame")
12+
(check-equal? (MAKE-FRAME '(x y z) '(21 22 23)) '((x . 21) (y . 22) (z . 23)) "3 bindings")
13+
14+
(check-equal? (EXTEND-ENVIRONMENT '(a b) '(1 2) '()) '(([a . 1] [b . 2])))
15+
(check-equal? (EXTEND-ENVIRONMENT '() '() '()) '(()) )
16+
17+
(check-equal? (EXTEND-ENVIRONMENT '(a b) '(1 2)
18+
(EXTEND-ENVIRONMENT '(c d) '(3 4) '()))
19+
'(((a . 1) (b . 2))
20+
((c . 3) (d . 4))) "Two frames, each with two bindings.")
21+
22+
(check-exn (regexp "Unbound variable 'x") (lambda ()
23+
(LOOKUP-VARIABLE-VALUE 'x '())))
24+
25+
(check-equal? (LOOKUP-VARIABLE-VALUE 'x '(([x . 8]))) 8)
26+
27+
(check-equal? (EVAL 1 '()) 1)
28+
(check-equal? (EVAL '(QUOTE symbol) '()) 'symbol)
29+
(check-equal? (EVAL '(QUOTE (1 2 3)) '()) '(1 2 3))
30+
(check-equal? (EVAL 'x (EXTEND-ENVIRONMENT '(x) '(8) '())) 8)
31+
(check-equal? (EVAL '(+ 3 4) (MAKE-BUILTINS)) 7)
32+
33+
; WIP: replace MAKE-BUILTINS with MAKE-BUILTIN-ENVIRONMENT
34+
(check-equal? (car (MAKE-BUILTIN-ENVIRONMENT)) '(+ . +))
35+
(check-equal? (caar (MAKE-BUILTIN-ENVIRONMENT)) '+)
36+
;(check-equal? (EVAL '(+ 3 4) (MAKE-BUILTIN-ENVIRONMENT)) 7)
37+
38+
(check-equal? (EVAL '(+ (* 3 4) (* 5 6)) (MAKE-BUILTINS)) 42)
39+
(check-equal? (EVAL '((LAMBDA (x) (* x x)) 111) (MAKE-BUILTINS)) 12321)
40+
(check-equal? (EVAL '(((LAMBDA (x)
41+
(LAMBDA (y) (+ x y)))
42+
3)
43+
4)
44+
(MAKE-BUILTINS)) 7)
45+
46+
(check-equal? (EVAL '((LAMBDA (x y)
47+
((LAMBDA (y) (+ x y))
48+
(* x y)))
49+
3 4)
50+
(MAKE-BUILTINS)) 15)
51+
52+
(check-equal? (EVAL '((LAMBDA (x) (IF (< x 0) (- 0 x) x)) 2)
53+
(MAKE-BUILTINS)) 2)
54+
55+
(check-equal? (EVAL '((LAMBDA (x) (IF (< x 0) (- 0 x) x)) -3)
56+
(MAKE-BUILTINS)) 3)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#lang racket/base
2+
3+
; code adapted from the "evaluator with concrete syntax"
4+
; listed on pages 121-122 of the "Instructor's Manual to
5+
; accompany Structure and Interpretation of Computer Programs"
6+
; by Julie Sussman with Harold Abelson and Gerald Jay Sussman.
7+
8+
; Tested with Racket 8.3.
9+
; This version adds DEFINE, not part of the code in the manual.
10+
11+
(define (EVAL exp environ)
12+
;; dispatch on expression type
13+
(cond [(number? exp) exp] ; self-evaluating
14+
[(symbol? exp) ; variable?
15+
(LOOKUP-VARIABLE-VALUE exp environ)]
16+
[(eq? (car exp) 'QUOTE) (cadr exp)] ; quoted?
17+
;; rule for procedure objects in the environment model
18+
[(eq? (car exp) 'LAMBDA) ; lambda?
19+
(list 'procedure ; make-procedure
20+
(cadr exp) ; lambda-parameters
21+
(caddr exp) ; lambda-body
22+
environ)]
23+
[(eq? (car exp) 'IF) ; if?
24+
(EVAL-IF exp environ)]
25+
[else
26+
(APPLY (EVAL (car exp) environ) ; operator
27+
(LIST-OF-VALUES (cdr exp) ; operands
28+
environ))]))
29+
30+
(define (APPLY procedure arguments)
31+
;; dispatch on procedure type
32+
(cond [(and (list? procedure) (eq? (car procedure) 'procedure)) ; compoud-procedure?
33+
(EVAL
34+
(caddr procedure) ; procedure-body
35+
(EXTEND-ENVIRONMENT
36+
(cadr procedure) ; procedure-parameters
37+
arguments
38+
(cadddr procedure)))] ; procedure-environment
39+
[else (apply procedure arguments)])) ; (apply ...) from host interpreter
40+
41+
(define (LIST-OF-VALUES exps environ)
42+
(if (null? exps)
43+
'()
44+
(cons (EVAL (car exps) environ)
45+
(LIST-OF-VALUES (cdr exps) environ))))
46+
47+
(define (EVAL-IF exp environ)
48+
(if (EVAL (cadr exp) environ) ; if-predicate
49+
(EVAL (caddr exp) environ) ; if-consequent
50+
(EVAL (cadddr exp) environ))) ; if-alternative
51+
52+
(define (EXTEND-ENVIRONMENT vars values base-environ)
53+
(cons (MAKE-FRAME vars values) base-environ))
54+
55+
(define (MAKE-FRAME vars values)
56+
(cond [(and (null? vars) (null? values)) '()]
57+
[(null? vars) (error "Too many arguments" values)]
58+
[(null? values) (error "Too few arguments" vars)]
59+
[else
60+
(cons (cons (car vars) (car values)) ; make binding
61+
(MAKE-FRAME (cdr vars) (cdr values)))]))
62+
63+
(define (LOOKUP-VARIABLE-VALUE var environ)
64+
(if (null? environ)
65+
(error "Unbound variable" var)
66+
(let ([binding (ASSQ var (car environ))]) ; look in first frame
67+
(if (eq? binding #f)
68+
(LOOKUP-VARIABLE-VALUE var (cdr environ))
69+
(cdr binding)))))
70+
71+
(define (ASSQ var bindings)
72+
(cond [(null? bindings) #f]
73+
[(eq? var (caar bindings)) (car bindings)]
74+
[else (ASSQ var (cdr bindings))]))
75+
76+
(define (MAKE-BUILTINS)
77+
(EXTEND-ENVIRONMENT
78+
'(+ - * / <)
79+
(list + - * / <)
80+
'()))
81+
82+
(define (MAKE-BUILTIN-ENVIRONMENT)
83+
(list '[+ . +]
84+
'[- . -]
85+
'[* . *]
86+
'[/ . /]
87+
'[< . <]
88+
))
89+
90+
91+
92+
; Needed to support rackunit tests in concrete-test.rkt.
93+
(provide ASSQ
94+
LOOKUP-VARIABLE-VALUE
95+
EXTEND-ENVIRONMENT
96+
MAKE-FRAME
97+
EVAL
98+
MAKE-BUILTINS
99+
MAKE-BUILTIN-ENVIRONMENT)

workshops/2022-01/GLOSSÁRIO.md

-42
This file was deleted.
File renamed without changes.
File renamed without changes.

workshops/europython2022/GLOSSARY.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# GLOSSARY
2+
3+
Programming language terms used in this workshop:
4+
5+
**AST**: Abstract Syntax Tree; a data structure that represents the program in
6+
a way that is suitable for processing by an _interpreter_ or _compiler_.
7+
Python's lists, dictionaries, and/or nested objects are useful to build an AST.
8+
Python's own internal AST is built from nested objects using classes defined in
9+
the [`ast` module](https://docs.python.org/3/library/ast.html) of the standard library.
10+
11+
**compiler**: a program which reads source code and produces binary code that
12+
can be executed by a CPU or a virtual machine.
13+
CPython includes a compiler that generates Python bytecode which is then
14+
executed by a bytecode _interpreter_ at runtime.
15+
This is similar to the way Java works,
16+
but most Python users don't need to know about the compiler because
17+
it runs automatically, generating bytecode in memory or on disk—for imported modules.
18+
19+
**host language**: the language in which an interpreter or compiler is written,
20+
as oposed to the language processed by the interpreter or compiler.
21+
The host language of `lis.py` is Python, and the host language of CPython is C.
22+
23+
**infix notation**: expression syntax with operators used between operands: `a + b`.
24+
Contrast with _prefix notation_.
25+
26+
**interpreter**: a program that executes code in memory,
27+
without generating a binary executable.
28+
Simple interpreters like `lis.py` read source code, produce an AST,
29+
and execute the user program by traversing the AST directly.
30+
Python's "interpreter" automatically compiles the AST to bytecode,
31+
and its runtime environment interprets the bytecode.
32+
See _compiler_.
33+
34+
**parser**: function which transforms source code into an _AST_,
35+
built with objects in the _host language_.
36+
For example, the `lis.py` function transforms `(* 2 pi)` into `['*', 2, 'pi']`.
37+
38+
**prefix notation**: expression syntax with operators preceding operands.
39+
For example `+ 1 2 3 4` is the sum of those four numbers.
40+
In Scheme, prefix expressions are delimited by parenthesis: `(+ a b c d)`
41+
Contrast with _infix notation_
42+
43+
**prompt**: a symbol displayed by an interactive interpreter to indicate it
44+
is ready to accept input. In Python, the standard prompt is `>>>`;
45+
in the Bash shell, the default is `$`.
46+
47+
**REPL**: Read-Eval-Print-Loop; an interactive program which reads user input,
48+
evaluates (i.e. executes), prints a result, and loops back to accept more
49+
input, often displaying a _prompt_.
50+
51+
**variadic**: a function parameter that accepts zero or more arguments.
52+
For example: `def add(*p): return sum(p)` creates a Python function
53+
accepting one or more arguments that are collected into the `p` parameter,
54+
so we can call it as `add()`, `add(3, 4)`, `add(1, 2, 3 4)`, or even
55+
`add(*range(1_000_000))`.
56+
In every case, the `p` parameter in `add` gets a tuple of zero or more values.
57+
The prefix notation of Scheme supports variadic operators,
58+
so `(+ 1 2 3 4)` evaluates to `10`.
59+
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)