10.07.2015 Views

all FREECO11-workshop-papers.pdf - trese

all FREECO11-workshop-papers.pdf - trese

all FREECO11-workshop-papers.pdf - trese

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

(define eval (lambda (exp env)(cond((symbol? exp) (cdr (assoc exp env)))((atom?(’texp) exp)(let ((fn (eval (car exp) env)))(if (fixed? fn)(apply (-function fn) (cdr exp) env)(apply fn (evlis (cdr exp) env) env)))))))(define apply (lambda (fun args env)(cond((subr? fun) {c<strong>all</strong> (-implementation fun) args env} )((expr? fun) (eval (-body fun) (pairlis (-formals fun) args (-environment fun)))))))(define %type-names (tuple))(define %type-sizes (tuple))(define %type-fields (tuple))Figure 1: Evaluation of symbolic expressions in a minimal functional language(define %<strong>all</strong>ocate-type(let ((last-type number-of-builtin-types ))(lambda (name fields)(let ((type (set last-type (+ 1 last-type))))(set-tuple-at %type-names type name)(set-tuple-at %type-sizes type (list-length fields))(set-tuple-at %type-fields type fields)type))))(define (%<strong>all</strong>ocate-type ’ ’(x y)))(define -x (lambda (value)(and (= (type-of value))(tuple-at value 0))))Figure 2: Adding an aggregate type to the languages are closures carrying the environment in whichthey were defined. Variable lookup is lexic<strong>all</strong>y scoped. (Atrivial change to the last line in Figure 1 gives LISP’s dynamicscoping.) Additional atomic types such as numbers,and primitives that act on them, will be present in a practic<strong>all</strong>anguage of this kind. They are omitted here for brevity.3. SUPPORTING OPEN COMPOSITIONThe language just described supports one combining form(the list). When a list is evaluated it causes a compositionin which each of the element(s) being combined is recursivelyevaluated yielding one or more values, the first ofwhich is then applied (as a function) to the rest (the arguments).“Open composition” means the ability to add newcompositions (or replace existing ones) corresponding to theevaluation of new (or existing) combining forms.Modifications to the language will be made to support:• defining new types, to represent the syntactic operators,state and semantics of composition mechanisms, and• associating meaning with combinations involving thesenew types.3.1 Extensible aggregate typesPredicates in eval and apply use some property of a value todiscriminate between types. The simplest generalisation isto identify each type with a unique integer. Incrementing acounter suffices to <strong>all</strong>ocate a new type. A primitive functiontype-of yields the type identifier for a given value.Three types (, and s for constructinglists) appear in the evaluator that are aggregates of values.Aggregation can be generalised to a single mechanism:infinitely-sized, indexable N-tuples containing undefined at<strong>all</strong> uninitialised indices. 11 Assignment at an uninitialised index extends the tuple asnecessary.With these mechanisms addition of a new aggregate typecan be effected in user code, as shown in Figure 2. 2 Norestrictions have been placed on the structure or semanticsof objects. Intrinsic types (those used by the evaluator)are built from the same parts: there is no disparity betweenbuilt-in and user-defined types and values.3.2 Extensible composition rulesTwo kinds of composition occur in the evaluator. Simpleatomic expressions (symbols in particular) are composedwith the environment by eval to yield a value. Combinationsof one or more values in complex expressions arecomposed by apply by application of a primitive or closurevalue to the remaining values. Both of kinds of compositionare made extensible by applying an appropriate combinationmechanism to every expression according to its type. Twotuples, evaluators and applicators, are indexed by type ineval and apply, respectively.eval(x, e) =apply(evaluators[type(x)], cons(x, nil),e)apply(f, a, e) =apply(applicators[type(f)], cons(f, a),e)Four consequences of this decomposition are:• any applicable value can supply the composition semanticsfor any expression, simple or complex,• any value can be made applicable, with semantics determinedby its type and its value,• the meaning of a complex expression (explicit combinationof values, e.g., as a list) is not fixed, or evensupplied, by the evaluation mechanism, and2 The steps shown <strong>all</strong>ocate the type, record information forprinting and instantiating, and define an accessor for an instancefield. In practice these steps are generated automatic<strong>all</strong>yfrom a single define-type expression, given a typename and a list of field names, along with the implied set of-field accessors.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!