Custom Specializers in Object-Oriented Lisp

Similar documents
Custom Specializers in Object-Oriented Lisp

Custom Specializers in Object-Oriented Lisp

Multi-Methods in Racket

Method Combinators. e ELS 2018 E. Didier Verna. EPITA / LRDE. Introduction Issues SBCL Combinators CGFs Alt. MCs Perfs Conclusion

A Brief Introduction to Common Lisp

A little bit of Lisp

Common Lisp Object System Specification. 1. Programmer Interface Concepts

1 CLWEB INTRODUCTION 1

A Genetic Algorithm Implementation

LP/LispLite: Trivial Lisp Org Mode Conversion

Object Oriented Programming (OOP)

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Object oriented programming

(defun fill-nodes (nodes texts name) (mapcar #'fill-node (replicate-nodes nodes (length texts) name) texts))

User-defined Functions. Conditional Expressions in Scheme

Artificial Intelligence Programming

2. Reasons for implementing clos-unit

Parsing Scheme (+ (* 2 3) 1) * 1

NST: A Unit Test Framework for Common Lisp

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Pattern Matching for Scheme

Robot Programming with Lisp

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

Lecture #2 Kenneth W. Flynn RPI CS

Compiler principles, PS1

Project 2: Scheme Interpreter

Lecture 16: Object Programming Languages

Common LISP Tutorial 1 (Basic)

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

Using a waiting protocol to separate concerns in the mutual exclusion problem

COSI: Adding Constraints to the object-oriented paradigm

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Evaluating Scheme Expressions

Filtered Dispatch ABSTRACT. Categories and Subject Descriptors. Keywords

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp

Principles of Programming Languages, 2

Structure Programming in Lisp. Shared Structure. Tailp. Shared Structure. Top-Level List Structure

This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions.

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Inferred System Descriptions ELS <

CS 360 Programming Languages Interpreters

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

The results for a few specific cases below are indicated. allequal ([1,1,1,1]) should return true allequal ([1,1,2,1]) should return false

Functional Programming. Pure Functional Programming

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

6.945 Adventures in Advanced Symbolic Programming

Semantic Analysis and Type Checking

Binary Methods Programming: the Clos Perspective

Environments

COP4020 Spring 2011 Midterm Exam

Mid-Term 2 Grades


SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Code Generation & Parameter Passing

Carnegie Mellon University. Pittsburgh, PA April This research has been supported in part by the Advanced Research

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

ESA: A CLIM Library for Writing Emacs-Style Applications

CSCE476/876 Fall Homework 3: Programming Assignment Using Emacs and Common Lisp. 1 Exercises (15 Points) 2. 2 Find (6 points) 4

Functional Programming. Big Picture. Design of Programming Languages

Introduction to Functional Programming and basic Lisp

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Generic Function Parameter and Method Parameter Metaobjects: A Proposed Enhancement to the CLOS Metaobject Protocol. Eric L.

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Berkeley Scheme s OOP

Infinite transducers on terms denoting graphs

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

SOFTWARE ARCHITECTURE 6. LISP

Inheritance and Substitution (Budd chapter 8, 10)

Aspect-Oriented Programming On Lisp

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

INF4820. Common Lisp: Closures and Macros

FP Foundations, Scheme

Structure and Interpretation of Computer Programs

LoGS. October 21st 2007 James E. Prewett PGP: F F 5FB8 918E C1F

CMSC 331 Final Exam Section 0201 December 18, 2000

CSCC24 Functional Programming Scheme Part 2

Department of Computer and information Science Norwegian University of Science and Technology

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

CMPUT325 Extensions to Pure Lisp. Extensions to Pure Lisp. Bob Price and Russ Greiner. 5th October 2004

AADL Graphical Editor Design

Section 10: LISP to Scheme. Evolution of Software Languages

Imperative, OO and Functional Languages A C program is

CS61A Midterm 2 Review (v1.1)

Reflection for the Masses

Context-oriented Programming. Pascal Costanza (Vrije Universiteit Brussel, Belgium) Robert Hirschfeld (Hasso-Plattner-Institut, Potsdam, Germany)

Documentation for LISP in BASIC

An Explicit Continuation Evaluator for Scheme

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

19 Machine Learning in Lisp

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY,

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Joshua Reference Manual

Implementing Symmetric Multiprocessing in LispWorks

Transcription:

Custom Cadence Design Systems Goldsmiths, University of London 23rd May 2008

Common : Skill: Unification of various dialects; Not dead yet. Common Skill Internal dialect from Cadence Design Systems; Extension language for Integrated Circuit software; Optional C-style syntax; Simple object system (Skill++, inspired by CLOS).

oriented CLOS VCLOS CL structures Skill++ objects: single inheritance single dispatch CL VCLOS stard-objects generic functions multiple inheritance multiple dispatch method combination [ lots more goodies] Both CLOS VCLOS are (almost) implementable as extensions to the base language In Common, CLOS is specified fully-integrated; In Skill, VCLOS is an extension library.

oriented CLOS VCLOS CL structures Skill++ objects: single inheritance single dispatch CL VCLOS stard-objects generic functions multiple inheritance multiple dispatch method combination [ lots more goodies] Both CLOS VCLOS are (almost) implementable as extensions to the base language In Common, CLOS is specified fully-integrated; In Skill, VCLOS is an extension library.

oriented CLOS VCLOS CL structures Skill++ objects: single inheritance single dispatch CL VCLOS stard-objects generic functions multiple inheritance multiple dispatch method combination [ lots more goodies] Both CLOS VCLOS are (almost) implementable as extensions to the base language In Common, CLOS is specified fully-integrated; In Skill, VCLOS is an extension library.

oriented Metaobject s VCLOS is implemented in itself; CLOS is encouraged to be Metaobject s Introspect intercede......using the (V)CLOS mechanisms themselves. : customize slot access: integration with foreign object systems; customize generic function call: integration with other languages; customize class precendence computation: run old codebases.

Example Subclassing the specializer metaobject class (defclass class (specializer) ((name :initarg :name :reader class-name) (direct-slots :initarg :direct-slots :reader class-direct-slots)...)) (defclass eql-specializer (specializer) ((object :initarg :object :reader eql-specializer-object))) What if we could do... (defclass equal-specializer (specializer) ((object :initarg :object :reader equal-specializer-object)))... how hard is it to make this work?

Example Subclassing the specializer metaobject class (defclass class (specializer) ((name :initarg :name :reader class-name) (direct-slots :initarg :direct-slots :reader class-direct-slots)...)) (defclass eql-specializer (specializer) ((object :initarg :object :reader eql-specializer-object))) What if we could do... (defclass equal-specializer (specializer) ((object :initarg :object :reader equal-specializer-object)))... how hard is it to make this work?

(defmethod walk ((expr list) env call-stack) (let ((call-stack (cons expr call-stack))) (walk (car expr) env call-stack) (walk (cdr expr) env call-stack))) Example Code walker dispatch (defmethod walk ((form (cons (eql quote))) env call-stack) nil) (defmethod walk ((form (cons (eql lambda))) env call-stack) (destructuring-bind (lambda lambda-list &rest body) form (let ((bs (derive-bindings-from-ll lambda-list))) (dolist (form body) (walk form (make-env bs env) (cons form call-stack))) (dolist (bind bs) (unless (used bind) (format t "unused: ~A: ~A~%" var call-stack))))))

(defmethod walk ((expr list) env call-stack) (let ((call-stack (cons expr call-stack))) (walk (car expr) env call-stack) (walk (cdr expr) env call-stack))) Example Code walker dispatch (defmethod walk ((form (cons (eql quote))) env call-stack) nil) (defmethod walk ((form (cons (eql lambda))) env call-stack) (destructuring-bind (lambda lambda-list &rest body) form (let ((bs (derive-bindings-from-ll lambda-list))) (dolist (form body) (walk form (make-env bs env) (cons form call-stack))) (dolist (bind bs) (unless (used bind) (format t "unused: ~A: ~A~%" var call-stack))))))

Example Symbolic simplification (defgeneric simplify (x) (:method (x) x)) ;;; in plus.lisp (defmethod simplify ((x (+ _ 0))) (simplify (second x))) (defmethod simplify ((x (+ 0 _))) (simplify (third x))) ;;; in times.lisp (defmethod simplify ((x (* _ 0))) 0) (defmethod simplify ((x (* _ 1))) (simplify (second x)))... (simplify (+ 0 (+ 1 0))) ; => 1

;;; default method: do nothing (defmethod process-node ((node t) strip-space-p) (declare (ignore strip-space-p))) ;;; strip this text node if it contains whitespace only (defmethod process-node ((node stp:text) (strip-space-p (eql t))) (when (whitespace-only-p (stp:data node)) (stp:detach node))) ;;; process children recursively for document element nodes (defmethod process-node ((node stp:parent-node) strip-space-p) (mapc (lambda (child) (process-node child strip-space-p)) (stp:list-children node))) Example XML whitespace normalization ;;; override the stripping mode when declared explicitly on a element: (defmethod process-node ((node (xpattern "*[@xml:space = preserve ]")) strip-space-p) (declare (ignore strip-space-p)) (call-next-method node nil)) (defmethod process-node ((node (xpattern "*[@xml:space = strip ]")) strip-space-p) (declare (ignore strip-space-p)) (call-next-method node t))

Run-time generic functions: parse-specializer-using-class gf name unparse-specializer-using-class gf spec for find-method, debugger, tracer defmethod-time generic function Details Parsing unparsing make-method-specializers-form gf method names env (minimum necessary: more fine-grained protocol needed for convenience)

Run-time generic functions: parse-specializer-using-class gf name unparse-specializer-using-class gf spec for find-method, debugger, tracer defmethod-time generic function Details Parsing unparsing make-method-specializers-form gf method names env (minimum necessary: more fine-grained protocol needed for convenience)

Details Discriminating functions Discriminating functions are the function part of a generic function. (defmethod compute-discriminating-function ((gf generic-function)) (lambda (&rest args) (let* ((ams (compute-applicable-methods gf args)) (mc (generic-function-method-combination gf)) (em (compute-effective-method gf mc ams)) (emf (generate-effective-method-function em))) (apply emf args)))) We need to override at least compute-applicable-methods

Details Discriminating functions Discriminating functions are the function part of a generic function. (defmethod compute-discriminating-function ((gf generic-function)) (lambda (&rest args) (let* ((ams (compute-applicable-methods gf args)) (mc (generic-function-method-combination gf)) (em (compute-effective-method gf mc ams)) (emf (generate-effective-method-function em))) (apply emf args)))) We need to override at least compute-applicable-methods

Details Computing applicable methods (defmethod compute-applicable-methods ((gf generic-function) args) (sort (remove-if-not (applicable-predicate args) (generic-function-methods gf)) (ordering-function gf args))) Doing this on every generic function call would be slow. Regular CLOS MOP has paired operators: 1 compute-applicable-methods 2 compute-applicable-methods-using-classes cacheing is done if c-a-m-using-classes can work out the answer.

Details Computing applicable methods (defmethod compute-applicable-methods ((gf generic-function) args) (sort (remove-if-not (applicable-predicate args) (generic-function-methods gf)) (ordering-function gf args))) Doing this on every generic function call would be slow. Regular CLOS MOP has paired operators: 1 compute-applicable-methods 2 compute-applicable-methods-using-classes cacheing is done if c-a-m-using-classes can work out the answer.

Details Computing cacheing effective methods (defmethod compute-discriminating-function ((gf generic-function)) (lambda (&rest args) (let* ((ams (compute-applicable-methods gf args)) (mc (generic-function-method-combination gf)) (em (compute-effective-method gf mc ams)) (emf (generate-effective-method-function em))) (apply emf args)))) CLOS MOP specifies compute-effective-method compute-effective-method-function would be more useful... also protocol for automatically cacheing the effective method; clearing the cache; pre-filling the cache...

Expressivity: clarity; modularity; dynamicity. Efficiency: (defmethod compute-discriminating-function ((gf generic-function)) (let* ((methods (generic-function-methods gf)) (dfun (compile-dispatch-function methods))) (lambda (&rest args) (apply dfun args))))

Expressivity: clarity; modularity; dynamicity. Efficiency: (defmethod compute-discriminating-function ((gf generic-function)) (let* ((methods (generic-function-methods gf)) (dfun (compile-dispatch-function methods))) (lambda (&rest args) (apply dfun args))))

Conclusions Custom specializers are an additional tool in the CLOS toolchest; Skill/VCLOS experience shows that they can be useful; The basic machinery has been implemented in a Common ; There are details in the protocol to be sorted out to make it easy to use.

Usable protocol for compute-applicable-methods specializer-accepts-class-p specializer< Better cacheing protocol specializer-of compute-applicable-methods-using-specializers invalidation on redefinition of dependents Compelling applications efficient ML-style pattern matcher with run-time dynamicity dispatch based on emacs-like active modes [your favourite here]

Acknowledgments Please download try: available in current SBCLs! For the Skill version, contact your nearest Cadence Design Systems representative. : Cadence Design Systems Goldsmiths College David Lichteblau Paul Khuong Pascal Costanza