Figure 5: Evaluation Rules Evaluation s −→ t E[s] −→ E[t] if s −→ t [E-Compat] (λx:S. t) v −→ t[x := v] [E-App] let x = v : S in t −→ t[x := v] [E-Let] not true −→ false [E-Not1] not false −→ true [E-Not2] if T true v 1 v 2 −→ v 1 unit [E-If1] if T false v 1 v 2 −→ v 2 unit [E-If2] + n 1 n 2 −→ n n = (n 1 + n 2) [E-Add] = v 1 v 2 −→ c c = (v 1 ≡ v 2) [E-Eq] 〈Bool〉 true −→ true [E-Cast-Bool1] 〈Bool〉 false −→ false [E-Cast-Bool2] 〈Unit〉 unit −→ unit [E-Cast-Unit] 〈Int〉 n −→ n [E-Cast-Int] 〈Dynamic〉 v −→ v [E-Cast-Dyn] 〈x:S → T 〉 v −→ [E-Cast-Fn] λx:S. 〈T 〉 (v (〈D〉 x)) where D = domain(v) 〈Refine T f〉 v −→ 〈T 〉 v [E-Refine] if f (〈T 〉 v) −→ ∗ true 〈*〉 v −→ v [E-Cast-Type] if v ∈ {Int, Bool, Unit, Dynamic, x:S → T, fix * f} S[fix U v] −→ S[v (fix U v)] [E-Fix] E ::= • | E t | v E Evaluation Contexts S ::= • v | 〈•〉 v Strict Contexts u, v, U, V ::= Values λx:S. t abstraction x:S → T function type c constant c v 1 . . . v n constant, 0 < n < arity(c) Refine U v refinement fix U v recursive type (Here, Sum <strong>and</strong> Pair are the usual type constructors for sums <strong>and</strong> pairs, respectively.) The Sage language includes two constants that are crucial for enabling hybrid type checking: Dynamic <strong>and</strong> cast. The type constant Dynamic [1, 39] can be thought <strong>of</strong> as the most general type. Every value has type Dynamic, <strong>and</strong> casts can be used to convert values from type Dynamic to other types (<strong>and</strong> <strong>of</strong> course such downcasts may fail if applied to inappropriate values). The constant cast performs dynamic checks or coercions between types. It takes as arguments a type T <strong>and</strong> a value (<strong>of</strong> type Dynamic), <strong>and</strong> it attempts to cast that value to type T . We use the shorth<strong>and</strong> 〈T 〉 t to abbreviate cast T t. Thus, for example, the expression 〈{x:Int | x ≥ 0}〉 y casts the integer y to the refinement type <strong>of</strong> natural numbers, <strong>and</strong> fails if y is negative. 3.2 Operational Semantics We formalize the execution behavior <strong>of</strong> Sage programs with the small-step operational semantics shown in Figure 5. Evaluation is performed inside evaluation contexts E. Application, let expressions, <strong>and</strong> the basic integer <strong>and</strong> boolean operations behave as expected. Rule [E-Eq] uses syntactic equality (≡) to test equivalence <strong>of</strong> all values, including function values 1 . The most interesting reduction rules are those for casts 〈T 〉 v. Casts to one <strong>of</strong> the base types Bool, Unit, or Int succeed if the value v is <strong>of</strong> the appropriate type. Casts to type Dynamic always succeed. Casts to function <strong>and</strong> refinement types are more complex. First, the following partial function domain returns the domain <strong>of</strong> a function value, <strong>and</strong> is defined by: domain : Value → Term domain(λx:T. t) = T domain(fix (x:T → T ′ ) v) = T domain(c v 1 . . . v i−1) = type <strong>of</strong> i th argument to c The rule [E-Cast-Fn] casts a function v to type x : S → T by creating a new function: λx:S. 〈T 〉 (v (〈D〉 x)) where D = domain(v) is the domain type <strong>of</strong> the function v. This new function takes a value x <strong>of</strong> type S, casts it to D, applies the given function v, <strong>and</strong> casts the result to the desired result type T . Thus, casting a function to a different function type will always succeed, since the domain <strong>and</strong> range values are checked lazily, in a manner reminiscent <strong>of</strong> higher-order contracts [13]. For a cast to a refinement type, 〈Refine T f〉 v, the rule [E-Refine] first casts v to type T via the cast 〈T 〉 v <strong>and</strong> then checks if the predicate f holds on this value. If it does, the cast succeeds <strong>and</strong> returns 〈T 〉 v. Casts to type * succeed only for special values <strong>of</strong> type *, via the rule [E-Cast-Type]. The operation fix is used to define recursive functions <strong>and</strong> types, which are considered values, <strong>and</strong> hence fix U v is also a value. However, when this construct fix U v appears in a strict position (i.e., in a function position or in a cast), the rule [E-fix] performs one step <strong>of</strong> unrolling to yield v (fix U v). 4. Type System The Sage type system is defined via the type rules <strong>and</strong> judgments shown in Figure 6. Typing is performed in an environment E that binds variables to types <strong>and</strong>, in some cases, to values. We assume that variables are bound at most once in an environment <strong>and</strong>, as usual, we apply implicit α- renaming <strong>of</strong> bound variables to maintain this assumption <strong>and</strong> to ensure that substitutions are capture-avoiding. The Sage type system guarantees progress (i.e., that well-typed programs can only get stuck due to failed casts) <strong>and</strong> preservation (i.e., that evaluation <strong>of</strong> a term preserves its type). The pro<strong>of</strong>s appear in a companion report [22]. The main typing judgment E ⊢ t : T 1 A semantic notion <strong>of</strong> equality for primitive types could provide additional flexibility, although such a notion would clearly be undecidable for higher-order types. In practice, syntactic equality has been sufficient. 98 <strong>Scheme</strong> <strong>and</strong> <strong>Functional</strong> <strong>Programming</strong>, <strong>2006</strong>
Figure 6: Type Rules Figure 7: Subtype Rules E ::= Environments: ∅ empty environment E, x : T environment extension E, x = v : T environment term extension Subtype rules E ⊢ T
- Page 1:
Scheme and Functional Programming 2
- Page 4 and 5:
4 Scheme and Functional Programming
- Page 6 and 7:
6 Scheme and Functional Programming
- Page 8 and 9:
• A web browser that plays the ro
- Page 10 and 11:
and requests. When a client request
- Page 12 and 13:
above prevents pages from these dom
- Page 14 and 15:
14 Scheme and Functional Programmin
- Page 16 and 17:
2. Explaining Macros Macro expansio
- Page 18 and 19:
For completeness, here is the macro
- Page 20 and 21:
onment is extended in the original
- Page 22 and 23:
expand-term(term, env, phase) = emi
- Page 24 and 25:
Derivation ::= (make-mrule Syntax S
- Page 26 and 27:
True derivation (before macro hidin
- Page 28 and 29:
We assume that the reader has basic
- Page 30 and 31:
the value of the %eax register by 4
- Page 32 and 33:
Code generation for the new forms i
- Page 34 and 35:
we introduced in 3.11. The only dif
- Page 36 and 37:
the user code from interfering with
- Page 38 and 39:
38 Scheme and Functional Programmin
- Page 40 and 41:
mization and on creating efficient
- Page 42 and 43:
(a) stage (b) fifo (c) split (d) me
- Page 44 and 45:
This tagging is used later in the c
- Page 46 and 47:
(let ((clo_25 (%closure (lambda (y)
- Page 48 and 49: nb. cycles per element 1400 1200 10
- Page 50 and 51: 50 Scheme and Functional Programmin
- Page 52 and 53: a ∗ ❅ left right · b ❅ a S
- Page 54 and 55: T ([spec], w) = { {w}, if w ∈ L([
- Page 56 and 57: A([spec]): ✓✏ (where L([spec])
- Page 58 and 59: construction commands. It is possib
- Page 60 and 61: ; Regular expression for Scheme num
- Page 62 and 63: References [1] A. V. Aho, R. Sethi,
- Page 64 and 65: (let ((n (cond ((char? var0) ) ((sy
- Page 66 and 67: 3. Survey An incomplete survey of a
- Page 68 and 69: case monster 10 literals 100 litera
- Page 70 and 71: 70 Scheme and Functional Programmin
- Page 72 and 73: modest programming requirements, an
- Page 74 and 75: development of incomplete subsystem
- Page 76 and 77: the previous request. This method a
- Page 78 and 79: could be run indefinitely. This fun
- Page 80 and 81: programmers reject Scheme without r
- Page 82 and 83: (define interp (λ (env e) (case e
- Page 84 and 85: Before describing the run-time sema
- Page 86 and 87: Figure 5. Cast Insertion Figure 6.
- Page 88 and 89: Figure 7. Evaluation Figure 8. Eval
- Page 90 and 91: catching type errors, as we do here
- Page 92 and 93: [34] J. C. Reynolds. Types, abstrac
- Page 94 and 95: let id (T:*) (x:T) : T = x; The tra
- Page 96 and 97: Figure 3: Regular Expressions and N
- Page 100 and 101: 5. Exact Substitution: E, (x = v :
- Page 102 and 103: Figure 9: Subtyping Algorithm Algor
- Page 104 and 105: for most type variables, and that m
- Page 106 and 107: 2. miniKanren Overview This section
- Page 108 and 109: 3. Pseudo-Variadic Relations Just a
- Page 110 and 111: Replacing run 10 with run ∗ cause
- Page 112 and 113: As might be expected, we could use
- Page 114 and 115: Of course there are still infinitel
- Page 116 and 117: To ensure that streams produced by
- Page 118 and 119: 118 Scheme and Functional Programmi
- Page 120 and 121: On the other hand, we can use a hig
- Page 122 and 123: (ev* (Q (lambda (x) (+ x 1)))) # >
- Page 124 and 125: (term ’lam (lambda (x) (if (equal
- Page 126 and 127: a message: there is no guarantee th
- Page 128 and 129: (! (self) 3) (?) =⇒ 1 (?? odd?) =
- Page 130 and 131: (define new-server (spawn (lambda (
- Page 132 and 133: The abstraction shown in this secti
- Page 134 and 135: Erlang Termite List length (µs) (
- Page 136 and 137: 136 Scheme and Functional Programmi
- Page 138 and 139: page of j contains the URL of the p
- Page 140 and 141: 4. Solution The failed attempts abo
- Page 142 and 143: · ; · ; · :: Store × Frame Stac
- Page 144 and 145: logically creates a sub-session of
- Page 146 and 147: on this work, including Ryan Culpep
- Page 148 and 149:
ing application operation. 1 As a r
- Page 150 and 151:
such as image glyphs corresponding
- Page 152 and 153:
Phone For clarity, this code pr
- Page 154 and 155:
A. Porting TinyScheme to Qualcomm B
- Page 156 and 157:
156 Scheme and Functional Programmi
- Page 158 and 159:
ware installers have to install any
- Page 160 and 161:
defines a module named circle-lib i
- Page 162 and 163:
Figure 2. Sometimes special cases a
- Page 164 and 165:
Considering all of these issues tog