15 Unification and Embedded Languages in Lisp
|
|
- Britton Harrell
- 5 years ago
- Views:
Transcription
1 15 Unification and Embedded Languages in Lisp Chapter Objectives Chapter Contents Pattern matching in Lisp: Database examples Full unification as required for Predicate Calculus problem solving Needed for applying inference rules General structure mapping Recursive for embedded structures Building interpreters and embedded languages Example: read-eval-print loop Example: infix interpreter 15.1 Pattern Matching: Introduction 15.2 Interpreters and Embedded Languages 15.1 Pattern Matching: Introduction Pattern Matching in Lisp In Chapter 15 we first design an algorithm for matching patterns in general list structures. This is the basis for the unify function which supports full pattern matching and the return of sets of unifying substitutions for matching patterns in predicate calculus expressions. We will see this as the basis for interpreters for logic programming and rule-based systems in Lisp, presented in Chapters 16 and 17. Pattern matching is an important AI methodology that has already been discussed in the Prolog chapters and in the presentation of production systems. In this section we implement a recursive pattern matcher and use it to build a pattern-directed retrieval function for a simple database. The heart of this retrieval system is a function called match, which takes as arguments two s-expressions and returns t if the expressions match. Matching requires that both expressions have the same structure as well as having identical atoms in corresponding positions. In addition, match allows the inclusion of variables, denoted by?, in an s-expression. Variables are allowed to match with any s-expression, either a list or an atom, but do not save bindings, as with full unification (next). Examples of the desired behavior for match appear below. If the examples seem reminiscent of the Prolog examples in Part II, this is because match is actually a simplified version of the unification algorithm that forms the heart of the Prolog environment, as well as of many other pattern-directed AI systems. We will later expand match into the full unification algorithm by allowing named variables and returning a list of bindings required for a match. 195
2 196 Part II: Programming in Lisp > (match (likes bill wine) (likes bill wine)) t > (match (likes bill wine) (likes bill milk)) nil > (match (likes bill?) (likes bill wine)) t > (match (likes? wine) (likes bill?)) t > (match (likes bill?) (likes bill (prolog lisp smalltalk)) t > (match (likes?) (likes bill wine)) nil match is used to define a function called get-matches, which takes as arguments two s-expressions. The first argument is a pattern to be matched against elements of the second s-expression, which must be a list. getmatches returns a list of the elements of the list that match the first argument. In the example below, get-matches is used to retrieve records from an employee database as described earlier in Part III. Because the database is a large and relatively complex s-expression, we have bound it to the global variable *database* and use that variable as an argument to get-matches. This was done to improve readability of the examples. > (setq *database* (((lovelace ada) ) *database* ((turing alan) ) ((shelley mary) ) ((vonneumann john) ) ((simon herbert) ) ((mccarthy john) ) ((russell bertrand) )) > (get-matches ((turing alan) ) *database*) ((turing alan) ) > (get-matches (? ?) *database*) ;people who make (((lovelace ada) ) ((simon herbert) )) > (get-matches ((? john)??) *database*) ;all people named john (((vonneumann john) ) ((mccarthy john) )) We implement get-matches using cdr recursion: each step attempts to match the target pattern with the first element of the database (the car of the list). If the is a match, the function will cons it onto the list of
3 Chapter 15 Unification and Embedded Languages 197 matches returned by the recursive call to form the answer for the pattern. get-matches is defined: (defun get-matches (pattern database) (cond ((null database) ( )) ((match pattern (car database)) (cons (car database) (get-matches pattern (cdr database)))) (t (get-matches pattern (cdr database))))) The heart of the system is the match function, a predicate that determines whether or not two s-expressions containing variables actually match. match is based on the idea that two lists match if and only if their respective cars and cdrs match, suggesting a car-cdr recursive scheme for the algorithm. The recursion terminates when either of the arguments is atomic (this includes the empty list, nil, which is both an atom and a list). If both patterns are the same atom or if one of the patterns is a variable atom,?, which can match with anything, then termination is with a successful match; otherwise, the match will fail. Notice that if either of the patterns is a variable, the other pattern need not be atomic; variables may match with variables or with s-expressions of arbitrary complexity. Because the handling of the terminating conditions is complex, the implementation of match uses a function called match-atom that takes two arguments, one or both of which is an atom, and checks to see whether the patterns match. By hiding this complexity in match-atom the car-cdr recursive structure of match is more apparent: (defun match (pattern1 pattern2) (cond (or (atom pattern1) (atom pattern2)) (match-atom pattern1 pattern2)) (t (and (match (car pattern1) (car pattern2)) (match (cdr pattern1) (cdr pattern2)))))) The implementation of match-atom makes use of the fact that when it is called, at least one of the arguments is an atom. Because of this assumption, a simple test for equality of patterns is all that is needed to test that both patterns are the same atom (including both being a variable); it will fail either if the two patterns are different atoms or if one of them is nonatomic. If the first test fails, the only way match can succeed is if one of the patterns is a variable. This check constitutes the remainder of the function definition. Finally, we define a function variable-p to test whether or not a pattern is a variable. Treating variables as an abstract data type now will simplify later extensions to the function, for example, the extension of the function to named variables as in Prolog.
4 198 Part II: Programming in Lisp (defun match-atom (pattern1 pattern2) (or (equal pattern1 pattern2) (variable-p pattern1) (variable-p pattern2))) (defun variable-p (x) (equal x?)) A Recursive Unification Function We have just completed the implementation of a recursive patternmatching algorithm that allowed the inclusion of unnamed variables in patterns. Our next step will be to extend this simple pattern matcher into the full unification algorithm. See Luger (2009, Section 2.3) for a pseudocode version of this algorithm. The function, unify, allows named variables in both of the patterns to be matched, and returns a substitution list of the variable bindings required for the match. This unification function is the basis of the inference systems for logic and expert system interpreters developed later in Chapters 16 and 17. As follows the definition of unification, patterns are either constants, variables, or list structures. We will distinguish variables from one another by their names. Named variables will be represented as lists of the form (var <name>), where <name> is usually an atomic symbol. (var x), (var y), and (var newstate) are all examples of legal variables. The function unify takes as arguments two patterns to be matched and a set of variable substitutions (bindings) to be employed in the match. Generally, this set will be empty (nil) when the function is first called. On a successful match, unify returns a (possibly empty) set of substitutions required for a successful match. If no match was possible, unify returns the symbol failed; nil is used to indicate an empty substitution set, i.e., a match in which no substitutions were required. An example of the behavior of unify, with comments, is: > (unify (p a (var x)) (p a b) ( )) (((var x). b)) ;Returns substitution of b for (var x). > (unify (p (var y) b) (p a (var x)) ( )) (((var x). b) ((var y). a)) ;Variables appear in both patterns. > (unify (p (var x)) (p (q a (var y))) ( )) (((var x) q a (var y))) ;Variable is bound to a complex pattern. > (unify (p a) (p a) ( )) nil ;nil indicates no substitution required. > (unify (p a) (q a) ( )) failed ;Returns atom failed to indicate failure.
5 Chapter 15 Unification and Embedded Languages 199 We will explain the. notation, as in ((var x). b), after we present the function unify. unify, like the pattern matcher of earlier in this section, uses a car-cdr recursive scheme and is defined by: (defun unify (pattern1 pattern2 substitution-list) (cond ((equal substitution-list failed) failed) ((varp pattern1) (match-var pattern1 ((varp pattern2) pattern2 substitution-list)) (match-var pattern2 pattern1 substitution-list)) ((is-constant-p pattern1) (cond ((equal pattern1 pattern2) substitution-list) (t failed))) ((is-constant-p pattern2) failed) (t (unify (cdr pattern1) (cdr pattern2) (unify (car pattern1) (car pattern2) substitution-list))))) On entering unify, the algorithm first checks whether the substitution-list is equal to failed. This could occur if a prior attempt to unify the cars of two patterns had failed. If this condition is met, the entire unification operation fails, and the function returns failed. Next, if either pattern is a variable, the function match-var is called to perform further checking and possibly add a new binding to substitution-list. If neither pattern is a variable, unify tests whether either is a constant, returning the unchanged substitution list if they are the same constant, otherwise it returns failed. The last item in the cond statement implements the tree-recursive decomposition of the problem. Because all other options have failed, the function concludes that the patterns are lists the must be unified recursively. It does this using a standard tree-recursive scheme: first, the cars of the patterns are unified using the bindings in substitutionlist. The result is passed as the third argument to the call of unify on the cdrs of both patterns. This allows the variable substitutions made in matching the cars to be applied to other occurrences of those variables in the cdrs of both patterns. match-var, for the case of matching a variable and a pattern, is defined:
6 200 Part II: Programming in Lisp (defun match-var (var pattern substitution-list) (cond ((equal var pattern) substitution-list) (t (let ((binding (get-binding var substitution-list))) (cond (binding (unify (get-binding-value binding) pattern substitution-list)) ((occursp var pattern) failed) (t (add-substitution var pattern substitution-list))))))) match-var first checks whether the variable and the pattern are the same; unifying a variable with itself requires no added substitutions, so substitution-list is returned unchanged. If var and pattern are not the same, match-var checks whether the variable is already bound. If a binding exists, unify is called recursively to match the value of the binding with pattern. Note that this binding value may be a constant, a variable, or a pattern of arbitrary complexity; requiring a call to the full unification algorithm to complete the match. If no binding currently exists for var, the function calls occursp to test whether var appears in pattern. As explained in (Luger 2009), the occurs check is needed to prevent attempts to unify a variable with a pattern containing that variable, leading to a circular structure. For example, if (var x) was bound to (p (var x)), any attempt to apply those substitutions to a pattern would result in an infinite structure. If var appears in pattern, match-var returns failed; otherwise, it adds the new substitution pair to substitution-list using addsubstitution. unify and match-var are the heart of the unification algorithm. occursp (which performs a tree walk on a pattern to find any occurrences of the variable in that pattern), varp, and is-constantp (which test whether their argument is a variable or a constant, respectively) appear below. Functions for handling substitution sets are discussed below. (defun occursp (var pattern) (cond ((equal var pattern) t) ((or (varp pattern) nil) (is-constant-p pattern)) (t (or (occursp var (car pattern)) (occursp var (cdr pattern)))))) (defun is-constant-p (item) (atom item))
7 Chapter 15 Unification and Embedded Languages 201 (defun varp (item) (and (listp item) (equal (length item) 2) (equal (car item) var))) Sets of substitutions are represented using a built-in Lisp data type called the association list or a-list. This is the basis for the functions addsubstitutions, get-binding, and binding-value. An association list is a list of data records, or key/data pairs. The car of each record is a key for its retrieval; the cdr of each record is called the datum. The datum may be a list of values or a single atom. Retrieval is implemented by the function assoc, which takes as arguments a key and an association list and returns the first member of the association list that has the key as its car. An optional third argument to assoc specifies the test to be used in comparing keys. The default test is the Common Lisp function eql, a form of equality test requiring that two arguments be the same object (i.e., either the same memory location or the same numeric value). In implementing substitution sets, we specify a less strict test, equal, which requires only that the arguments match syntactically (i.e., are designated by identical names). An example of assoc s behavior appears next: > (assoc 3 ((1 a) (2 b) (3 c) (4 d))) (3 c) > (assoc d ((a b c) (b c d e) (d e f) (c d e)) :test # equal) (d e f) > (assoc c ((a. 1) (b. 2) (c. 3) (d. 4)) :test # equal) (c. 3) Note that assoc returns the entire record matched on the key; the datum may be retrieved from this list by the cdr function. Also, notice that in the last call the members of the a-list are not lists but a structure called dotted pairs, e.g., (a. 1). The dotted pair, or cons pair, is actually the fundamental constructor in Lisp. It is the result of consing one s-expression onto another; the list notation that we have used throughout the chapter is just a notational variant of dotted pairs. For example, the value returned by (cons 1 nil) is actually (1. nil); this is equivalent to (1). Similarly, the list (1 2 3) may be written in dotted pair notation as (1. (2. ( 3. nil))). Although the actual effect of a cons is to create a dotted pair, the list notation is cleaner and is generally preferred. If two atoms are consed together, the result is always written using dotted pair notation. The cdr of a dotted pair is the second element in the pair, rather than a list containing the second atom. For example: > (cons a b) (a. b)
8 202 Part II: Programming in Lisp > (car (a. b)) a > (cdr (a. b)) b Dotted pairs occur naturally in association lists when one atom is used as a key for retrieving another atom, as well as in other applications that require the formation and manipulation of pairs of atomic symbols. Because unifications often substitute a single atom for a variable, dotted pairs appear often in the association list returned by the unification function. Along with assoc, Common Lisp defines the function acons, which takes as arguments a key, a datum, and an association list and returns a new association list whose first element is the result of consing the key onto the datum. For example: > (acons a 1 nil) ((a. 1)) Note that when acons is given two atoms, it adds their cons to the association list: > (acons pets (emma jack clyde) ((name. bill) (hobbies music skiing movies) (job. programmer))) ((pets emma jack clyde) (name. bill) (hobbies music skiing movies)(job. programmer)) The members of an association list may themselves be either dotted pairs or lists. Association lists provide a convenient way to implement a variety of tables and other simple data retrieval schemes. In implementing the unification algorithm, we use association lists to represent sets of substitutions: the keys are the variables, and the data are the values of their bindings. The datum may be a simple variable or constant or a more complicated structure. Using association lists, the substitution set functions are defined: (defun get-binding (var substitution-list) (assoc var substitution-list :test # equal)) (defun get-binding-value (binding) (cdr binding)) (defun add-substitution (var pattern substitution-list) (acons var pattern substitution-list)) This completes the implementation of the unification algorithm. We will use the unification algorithm again in Section 15.1 to implement a simple Prolog in Lisp interpreter, and again in Section 16.2 to build an expert system shell.
9 Chapter 15 Unification and Embedded Languages Interpreters and Embedded Languages The top level of the Lisp interpreter is known as the read-eval-print loop. This describes the interpreter s behavior in reading, evaluating, and printing the value of s-expressions entered by the user. The eval function, defined in Section 11.2, is the heart of the Lisp interpreter; using eval, it is possible to write Lisp s top-level read-eval-print loop in Lisp itself. In the next example, we develop a simplified version of this loop. This version is simplified chiefly in that it does not have the error-handling abilities of the built-in loop, although Lisp does provide the functionality needed to implement such capabilities. To write the read-eval-print loop, we use two more Lisp functions, read and print. read is a function that takes no parameters; when it is evaluated, it returns the next s-expression entered at the keyboard. print is a function that takes a single argument, evaluates it, and then prints that result to standard output. Another function that will prove useful is terpri, a function of no arguments that sends a newline character to standard output. terpri also returns a value of nil on completion. Using these functions, the read-eval-print loop is based on a nested s-expression: (print (eval (read))) When this is evaluated, the innermost s-expression, (read), is evaluated first. The value returned by the read, the next s-expression entered by the user, is passed to eval, where it is evaluated. The result of this evaluation is passed to print, where it is sent to the display screen. To complete the loop we add a print expression to output the prompt, a terpri to output a newline after the result has been printed, and a recursive call to repeat the cycle. Thus, the final read-eval-print loop is defined: (defun my-read-eval-print ( ) (print :) ;output a prompt ( : ) (print (eval (read))) (terpri) ;output a newline (my-read-eval-print)) This may be used on top of the built-in interpreter: > (my-read-eval-print) :(+ 1 2) ;note the alternative prompt 3 ;do it all again : ;etc As this example illustrates, by making functions such as quote and eval available to the user, Lisp gives the programmer a high degree of control over the handling of functions. Because Lisp programs and data are both represented as s-expressions, we may write programs that perform any desired manipulations of Lisp expressions prior to evaluating them. This underlies much of Lisp s power as an imperative representation language because it allows arbitrary Lisp code to be stored, modified, and evaluated when needed. It also makes it simple to write specialized interpreters that
10 204 Part II: Programming in Lisp may extend or modify the behavior of the built-in Lisp interpreter in some desired fashion. This capability is at the heart of many Lisp-based expert systems, which read user queries and respond to them according to the expertise contained in their knowledge base. As an example of the way in which such a specialized interpreter may be implemented in Lisp, we modify my-read-eval-print so that it evaluates arithmetic expressions in an infix rather than a prefix notation, as we see in the following example (note the modified prompt, infix->): infix-> (1 + 2) 3 infix-> (7 2) 5 infix-> ((5 + 2) * (3 1)) 15 ;Loop handles nesting. To simplify the example, the infix interpreter handles only arithmetic expressions. A further simplification restricts the interpreter to binary operations and requires that all expressions be fully parenthesized, eliminating the need for more sophisticated parsing techniques or worries about operator precedence. However, it does allow expressions to be nested to arbitrary depth and handles Lisp s binary arithmetic operators. We modify the previously developed read-eval-print loop by adding a function that translates infix expressions into prefix expressions prior to passing them on to eval. A first attempt at writing this function might look like: (defun simple-in-to-pre (exp) (list (nth 1 exp) (nth 0 exp) ;first operand (nth 2 exp) ;second operand ;Middle element becomes first element. simple-in-to-pre is effective in translating simple expressions; however, it is not able to correctly translate nested expressions, that is, expressions in which the operands are themselves infix expressions. To handle this situation properly, the operands must also be translated into prefix notation. Recursion is halted by testing the argument to determine whether it is a number, returning it unchanged if it is. The completed version of the infix-to-prefix translator is: (defun in-to-pre (exp) (cond ((numberp exp) exp) (t (list (nth 1 exp) (in-to-pre (nth 0 exp)) (in-to-pre (nth 2 exp)))))) Using this translator, the read-eval-print loop may be modified to interpret infix expressions, as defined next:
11 Chapter 15 Unification and Embedded Languages 205 (defun in-eval ( ) (print infix->) (print (eval (in-to-pre (read)))) (terpri) (in-eval)) This allows the interpretation of binary expressions in infix form: > (in-eval) infix->(2 + 2) 4 infix->((3 * 4) 5) 7 In the above example, we have implemented a new language in Lisp, the language of infix arithmetic. Because of the facilities Lisp provides for symbolic computing (lists and functions for their manipulation) along with the ability to control evaluation, this was much easier to do than in many other programming languages. This example illustrates an important AI programming methodology, that of meta-linguistic abstraction. Very often in AI programming, a problem is not completely understood, or the program required to solve a problem is extremely complex. Metalinguistic abstraction uses the underlying programming language, in this case, Lisp, to implement a specialized, high-level language that may be more effective for solving a particular class of problems. The term metalinguistic abstraction refers to our use of the base language to implement this other programming language, rather than to directly solve the problem. As we saw in Chapter 5, Prolog also gives the programmer the power to create meta-level interpreters. The power of meta-interpreters to support programming in complex domains was discussed in Part I. Exercises 1. Newton s method for solving roots takes an estimate of the value of the root and tests it for accuracy. If the guess does not meet the required tolerance, it computes a new estimate and repeats. Pseudo-code for using Newton s method to get the square root of a number is: function root-by-newtons-method (x, tolerance) guess := 1; repeat guess := 1/2(guess + x/guess) until absolute-value(x guess guess) < tolerance Write a recursive Lisp function to compute square roots by Newton s method. 2. Write a random number generator in Lisp. This function must maintain a global variable, seed, and return a different random number each time the function is called. For a description of a reasonable random number algorithm, consult any basic algorithms text.
12 206 Part II: Programming in Lisp 3. Test the unify form of Section 15.1 with five different examples of your own creation. 4. Test the occursp form of Section 15.1 on five different examples of your own creation 5. Write a binary post-fix interpreter that takes arbitrarily complex structures in post-fix form and evaluates them. Two examples of post-fix are (3 4 +) and (6 (5 4 +) *).
Functional Programming. Pure Functional Languages
Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure
More informationA Small Interpreted Language
A Small Interpreted Language What would you need to build a small computing language based on mathematical principles? The language should be simple, Turing equivalent (i.e.: it can compute anything that
More informationFunctional programming with Common Lisp
Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 81 Expressions and functions
More informationFunctional Programming. Pure Functional Languages
Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure
More informationScheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.
Scheme: Data CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks ggchappell@alaska.edu
More informationFunctional Programming. Pure Functional Programming
Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).
More informationModern Programming Languages. Lecture LISP Programming Language An Introduction
Modern Programming Languages Lecture 18-21 LISP Programming Language An Introduction 72 Functional Programming Paradigm and LISP Functional programming is a style of programming that emphasizes the evaluation
More information19 Machine Learning in Lisp
19 Machine Learning in Lisp Chapter Objectives Chapter Contents ID3 algorithm and inducing decision trees from lists of examples. A basic Lisp implementation of ID3 Demonstration on a simple credit assessment
More informationLISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.
LISP Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits. From one perspective, sequences of bits can be interpreted as a code for ordinary decimal digits,
More information11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT
Functional programming FP Foundations, Scheme (2 In Text: Chapter 15 LISP: John McCarthy 1958 MIT List Processing => Symbolic Manipulation First functional programming language Every version after the
More informationLambda Calculus. Gunnar Gotshalks LC-1
Lambda Calculus LC-1 l- Calculus History Developed by Alonzo Church during 1930 s-40 s One fundamental goal was to describe what can be computed. Full definition of l-calculus is equivalent in power to
More informationFunctional Programming. Big Picture. Design of Programming Languages
Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics
More informationCIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires
1 A couple of Functions 1 Let's take another example of a simple lisp function { one that does insertion sort. Let us assume that this sort function takes as input a list of numbers and sorts them in ascending
More informationDocumentation for LISP in BASIC
Documentation for LISP in BASIC The software and the documentation are both Copyright 2008 Arthur Nunes-Harwitt LISP in BASIC is a LISP interpreter for a Scheme-like dialect of LISP, which happens to have
More informationLook at the outermost list first, evaluate each of its arguments, and use the results as arguments to the outermost operator.
LISP NOTES #1 LISP Acronymed from List Processing, or from Lots of Irritating Silly Parentheses ;) It was developed by John MacCarthy and his group in late 1950s. Starting LISP screen shortcut or by command
More informationINF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals
INF4820: Algorithms for Artificial Intelligence and Natural Language Processing Common Lisp Fundamentals Stephan Oepen & Murhaf Fares Language Technology Group (LTG) August 30, 2017 Last Week: What is
More informationLambda Calculus LC-1
Lambda Calculus LC-1 λ- Calculus History-1 Developed by Alonzo Church during 1930 s-40 s One fundamental goal was to describe what can be computed. Full definition of λ-calculus is equivalent in power
More informationBuilding a system for symbolic differentiation
Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)
More informationFifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application
Fifth Generation CS 4100 LISP From Principles of Programming Languages: Design, Evaluation, and Implementation (Third Edition, by Bruce J. MacLennan, Chapters 9, 10, 11, and based on slides by Istvan Jonyer
More informationBuilding a system for symbolic differentiation
Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)
More informationIntroduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.
Introduction to LISP York University Department of Computer Science and Engineering York University- CSE 3401- V. Movahedi 11_LISP 1 Introduction to LISP Evaluation and arguments S- expressions Lists Numbers
More informationFunctional Programming
Functional Programming CS331 Chapter 14 Functional Programming Original functional language is LISP LISt Processing The list is the fundamental data structure Developed by John McCarthy in the 60 s Used
More informationIntroduction to Functional Programming and basic Lisp
Introduction to Functional Programming and basic Lisp Based on Slides by Yves Lespérance & Peter Roosen-Runge 1 Functional vs Declarative Programming declarative programming uses logical statements to
More informationA Brief Introduction to Scheme (II)
A Brief Introduction to Scheme (II) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Lists Scheme II p.1/29 Lists Aggregate data
More information6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson
6.184 Lecture 4 Interpretation Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 1 Interpretation Parts of an interpreter Arithmetic calculator
More informationSymbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89
Symbolic Programming Symbols: +, -, 1, 2 etc. Symbolic expressions: (+ 1 2), (+ (* 3 4) 2) Symbolic programs are programs that manipulate symbolic expressions. Symbolic manipulation: you do it all the
More informationCS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)
CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal) What is the Metacircular Evaluator? It is the best part
More informationScheme in Scheme: The Metacircular Evaluator Eval and Apply
Scheme in Scheme: The Metacircular Evaluator Eval and Apply CS21b: Structure and Interpretation of Computer Programs Brandeis University Spring Term, 2015 The metacircular evaluator is A rendition of Scheme,
More informationLecture Notes on Lisp A Brief Introduction
Why Lisp? Lecture Notes on Lisp A Brief Introduction Because it s the most widely used AI programming language Because Prof Peng likes using it Because it s good for writing production software (Graham
More information1.3. Conditional expressions To express case distinctions like
Introduction Much of the theory developed in the underlying course Logic II can be implemented in a proof assistant. In the present setting this is interesting, since we can then machine extract from a
More informationFunctional Programming Languages (FPL)
Functional Programming Languages (FPL) 1. Definitions... 2 2. Applications... 2 3. Examples... 3 4. FPL Characteristics:... 3 5. Lambda calculus (LC)... 4 6. Functions in FPLs... 7 7. Modern functional
More informationFunctional Languages. Hwansoo Han
Functional Languages Hwansoo Han Historical Origins Imperative and functional models Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s Different formalizations of the notion of an algorithm
More informationLisp Basic Example Test Questions
2009 November 30 Lisp Basic Example Test Questions 1. Assume the following forms have been typed into the interpreter and evaluated in the given sequence. ( defun a ( y ) ( reverse y ) ) ( setq a (1 2
More informationProject 2: Scheme Interpreter
Project 2: Scheme Interpreter CSC 4101, Fall 2017 Due: 12 November 2017 For this project, you will implement a simple Scheme interpreter in C++ or Java. Your interpreter should be able to handle the same
More information6.001 Notes: Section 8.1
6.001 Notes: Section 8.1 Slide 8.1.1 In this lecture we are going to introduce a new data type, specifically to deal with symbols. This may sound a bit odd, but if you step back, you may realize that everything
More informationLambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.
λ Calculus Basis Lambda Calculus and Lambda notation in Lisp II Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky Mathematical theory for anonymous functions» functions that have
More informationSOFTWARE ARCHITECTURE 6. LISP
1 SOFTWARE ARCHITECTURE 6. LISP Tatsuya Hagino hagino@sfc.keio.ac.jp slides URL https://vu5.sfc.keio.ac.jp/sa/ 2 Compiler vs Interpreter Compiler Translate programs into machine languages Compilers are
More informationCS 314 Principles of Programming Languages
CS 314 Principles of Programming Languages Lecture 16: Functional Programming Zheng (Eddy Zhang Rutgers University April 2, 2018 Review: Computation Paradigms Functional: Composition of operations on data.
More informationAnnouncement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.
Overview Announcement Announcement Lisp Basics CMUCL to be available on sun.cs. You may use GNU Common List (GCL http://www.gnu.org/software/gcl/ which is available on most Linux platforms. There is also
More informationOrganization of Programming Languages CS3200/5200N. Lecture 11
Organization of Programming Languages CS3200/5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Functional vs. Imperative The design of the imperative languages
More informationFUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,
FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE, 2011 Jan Janoušek MI-FLP Evropský sociální fond Praha & EU: Investujeme do vaší budoucnosti L I S P - Introduction L I S P
More informationScheme: Expressions & Procedures
Scheme: Expressions & Procedures CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, March 31, 2017 Glenn G. Chappell Department of Computer Science University
More informationTyped Racket: Racket with Static Types
Typed Racket: Racket with Static Types Version 5.0.2 Sam Tobin-Hochstadt November 6, 2010 Typed Racket is a family of languages, each of which enforce that programs written in the language obey a type
More informationIntroduction to lambda calculus Part 3
Introduction to lambda calculus Part 3 Antti-Juhani Kaijanaho 2017-01-27... 1 Untyped lambda calculus... 2 Typed lambda calculi In an untyped lambda calculus extended with integers, it is required that
More informationLisp. Versions of LISP
Lisp Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks is based on Common Lisp Scheme is one of the major
More informationScheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax
Scheme Tutorial Introduction Scheme is an imperative language with a functional core. The functional core is based on the lambda calculus. In this chapter only the functional core and some simple I/O is
More informationPattern Matching WIlensky Chapter 21
Pattern Matching WIlensky Chapter 21 PM-1 Pattern Matching A ubiquitous function in intelligence is pattern matching» IQ tests, for example, contain pattern matching problems because they are recognized
More informationImperative, OO and Functional Languages A C program is
Imperative, OO and Functional Languages A C program is a web of assignment statements, interconnected by control constructs which describe the time sequence in which they are to be executed. In Java programming,
More informationSCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017
SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
More informationALISP interpreter in Awk
ALISP interpreter in Awk Roger Rohrbach 1592 Union St., #94 San Francisco, CA 94123 January 3, 1989 ABSTRACT This note describes a simple interpreter for the LISP programming language, written in awk.
More informationCPS 506 Comparative Programming Languages. Programming Language Paradigm
CPS 506 Comparative Programming Languages Functional Programming Language Paradigm Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming
More informationSummer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define
CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
More informationLambda Calculus see notes on Lambda Calculus
Lambda Calculus see notes on Lambda Calculus Shakil M. Khan adapted from Gunnar Gotshalks recap so far: Lisp data structures basic Lisp programming bound/free variables, scope of variables Lisp symbols,
More informationAnnouncements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.
The current topic: Scheme! Introduction! Object-oriented programming: Python Functional programming: Scheme! Introduction! Numeric operators, REPL, quotes, functions, conditionals! Function examples, helper
More informationPrinciples of Programming Languages 2017W, Functional Programming
Principles of Programming Languages 2017W, Functional Programming Assignment 3: Lisp Machine (16 points) Lisp is a language based on the lambda calculus with strict execution semantics and dynamic typing.
More informationSCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015
SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
More informationC311 Lab #3 Representation Independence: Representation Independent Interpreters
C311 Lab #3 Representation Independence: Representation Independent Interpreters Will Byrd webyrd@indiana.edu February 5, 2005 (based on Professor Friedman s lecture on January 29, 2004) 1 Introduction
More informationPrinciples of Programming Languages
Principles of Programming Languages www.cs.bgu.ac.il/~ppl172 Lesson 6 - Defining a Programming Language Bottom Up Collaboration and Management - Elements of Programming Dana Fisman 1 What we accomplished
More information6.001 Notes: Section 15.1
6.001 Notes: Section 15.1 Slide 15.1.1 Our goal over the next few lectures is to build an interpreter, which in a very basic sense is the ultimate in programming, since doing so will allow us to define
More informationHeap storage. Dynamic allocation and stacks are generally incompatible.
Heap storage Dynamic allocation and stacks are generally incompatible. 1 Stack and heap location Pointer X points to stack storage in procedure Q's activation record that no longer is live (exists) when
More information6.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
6.037 Lecture 4 Interpretation Interpretation Parts of an interpreter Meta-circular Evaluator (Scheme-in-scheme!) A slight variation: dynamic scoping Original material by Eric Grimson Tweaked by Zev Benjamin,
More informationRecursive Functions of Symbolic Expressions and Their Application, Part I
Recursive Functions of Symbolic Expressions and Their Application, Part I JOHN MCCARTHY Review: Amit Kirschenbaum Seminar in Programming Languages Recursive Functions of Symbolic Expressions and Their
More informationHomework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012
Homework 1 Due 14 February Handout 3 CSCI 334: Spring, 2012 Reading 1. (Required) Mitchell, Chapter 3. 2. (As Needed) The Lisp Tutorial from the Links web page, as needed for the programming questions.
More informationFunctions, Conditionals & Predicates
Functions, Conditionals & Predicates York University Department of Computer Science and Engineering 1 Overview Functions as lambda terms Defining functions Variables (bound vs. free, local vs. global)
More information;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))
Homework 1 Due 13 September Handout 2 CSC 131: Fall, 2006 6 September Reading 1. Read Mitchell, Chapter 3. 2. The Scheme Tutorial and the Scheme Quick Reference from the Links web page, as needed for the
More information6.001 Notes: Section 1.1
6.001 Notes: Section 1.1 Slide 1.1.1 This first thing we need to do is discuss the focus of 6.001. What is this course all about? This seems quite obvious -- this is a course about computer science. But
More informationCSCI337 Organisation of Programming Languages LISP
Organisation of Programming Languages LISP Getting Started Starting Common Lisp $ clisp i i i i i i i ooooo o ooooooo ooooo ooooo I I I I I I I 8 8 8 8 8 o 8 8 I \ `+' / I 8 8 8 8 8 8 \ `-+-' / 8 8 8 ooooo
More informationChapter 15. Functional Programming Languages
Chapter 15 Functional Programming Languages Copyright 2009 Addison-Wesley. All rights reserved. 1-2 Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages
More informationIntroduction to Functional Programming
Introduction to Functional Programming Xiao Jia xjia@cs.sjtu.edu.cn Summer 2013 Scheme Appeared in 1975 Designed by Guy L. Steele Gerald Jay Sussman Influenced by Lisp, ALGOL Influenced Common Lisp, Haskell,
More informationA LISP Interpreter in ML
UNIVERSITY OF OSLO Department of Informatics A LISP Interpreter in ML Mandatory Assignment 1 INF3110 September 21, 2009 Contents 1 1 Introduction The purpose of this assignment is to write an interpreter,
More informationIntroduction to Scheme
How do you describe them Introduction to Scheme Gul Agha CS 421 Fall 2006 A language is described by specifying its syntax and semantics Syntax: The rules for writing programs. We will use Context Free
More informationCommon LISP-Introduction
Common LISP-Introduction 1. The primary data structure in LISP is called the s-expression (symbolic expression). There are two basic types of s-expressions: atoms and lists. 2. The LISP language is normally
More informationINTERPRETATION AND SCHEME LISTS 12
INTERPRETATION AND SCHEME LISTS 12 COMPUTER SCIENCE 61A July 26, 2012 1 Calculator Language We are beginning to dive into the realm of interpreting computer programs. To do so, we will examine a few new
More informationFall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives
CS 6A Scheme Fall 208 Discussion 8: October 24, 208 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write
More informationUser-defined Functions. Conditional Expressions in Scheme
User-defined Functions The list (lambda (args (body s to a function with (args as its argument list and (body as the function body. No quotes are needed for (args or (body. (lambda (x (+ x 1 s to the increment
More informationSCHEME AND CALCULATOR 5b
SCHEME AND CALCULATOR 5b COMPUTER SCIENCE 6A July 25, 203 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,
More informationStreams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson
Streams, Delayed Evaluation and a Normal Order Interpreter CS 550 Programming Languages Jeremy Johnson 1 Theme This lecture discusses the stream model of computation and an efficient method of implementation
More informationTyped Scheme: Scheme with Static Types
Typed Scheme: Scheme with Static Types Version 4.1.1 Sam Tobin-Hochstadt October 5, 2008 Typed Scheme is a Scheme-like language, with a type system that supports common Scheme programming idioms. Explicit
More informationChapter 1. Fundamentals of Higher Order Programming
Chapter 1 Fundamentals of Higher Order Programming 1 The Elements of Programming Any powerful language features: so does Scheme primitive data procedures combinations abstraction We will see that Scheme
More informationImplementing Programming Languages
Out of print; full text available for free at http://www.gustavus.edu/+max/concrete-abstractions.html CHAPTER TEN Implementing Programming Languages 10.1 Introduction The Scheme system you ve been using
More information4/19/2018. Chapter 11 :: Functional Languages
Chapter 11 :: Functional Languages Programming Language Pragmatics Michael L. Scott Historical Origins The imperative and functional models grew out of work undertaken by Alan Turing, Alonzo Church, Stephen
More informationThe Typed Racket Guide
The Typed Racket Guide Version 5.3.6 Sam Tobin-Hochstadt and Vincent St-Amour August 9, 2013 Typed Racket is a family of languages, each of which enforce
More informationFunctional programming in LISP
Programming Languages Week 4 Functional programming in LISP College of Information Science and Engineering Ritsumeikan University review of part 3 enumeration of dictionaries you receive a sequence of
More informationSymbolic Reasoning. Dr. Neil T. Dantam. Spring CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Symbolic Reasoning Spring / 86
Symbolic Reasoning Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Spring 2019 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 1 / 86 Introduction Definition: Symbolic Reasoning Inference
More informationOperational Semantics
15-819K: Logic Programming Lecture 4 Operational Semantics Frank Pfenning September 7, 2006 In this lecture we begin in the quest to formally capture the operational semantics in order to prove properties
More informationFunctional Languages. CSE 307 Principles of Programming Languages Stony Brook University
Functional Languages CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Historical Origins 2 The imperative and functional models grew out of work
More informationBelow are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.
6.001, Fall Semester, 2002 Quiz II Sample solutions 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs
More informationSCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))
SCHEME 0 COMPUTER SCIENCE 6A July 26, 206 0. Warm Up: Conditional Expressions. What does Scheme print? scm> (if (or #t (/ 0 (/ 0 scm> (if (> 4 3 (+ 2 3 4 (+ 3 4 (* 3 2 scm> ((if (< 4 3 + - 4 00 scm> (if
More informationArtificial Intelligence Lecture 1
Artificial Intelligence Lecture 1 istrative Matters Webpage: www.aass.oru.se/~ali/ai2008 Teacher: Amy Loutfi Hours: Fridays 10 12 Lab Assistant: Marcello Cirillo 2 istrative Matters Course book: Alison
More informationSCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012
SCHEME COMPUTER SCIENCE 6A October 29th, 202 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs, we will eventually
More informationData Types The ML Type System
7 Data Types 7.2.4 The ML Type System The following is an ML version of the tail-recursive Fibonacci function introduced Fibonacci function in ML in Section 6.6.1: EXAMPLE 7.96 1. fun fib (n) = 2. let
More information6.001 Notes: Section 6.1
6.001 Notes: Section 6.1 Slide 6.1.1 When we first starting talking about Scheme expressions, you may recall we said that (almost) every Scheme expression had three components, a syntax (legal ways of
More information1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.
1. true / false By a compiler we mean a program that translates to code that will run natively on some machine. 2. true / false ML can be compiled. 3. true / false FORTRAN can reasonably be considered
More informationSimple Lisp. Alonzo Church. John McCarthy. Turing. David Hilbert, Jules Richard, G. G. Berry, Georg Cantor, Bertrand Russell, Kurt Gödel, Alan
Alonzo Church John McCarthy Simple Lisp David Hilbert, Jules Richard, G. G. Berry, Georg Cantor, Bertrand Russell, Kurt Gödel, Alan Turing Dr. Philip Cannata 1 Simple Lisp See the class website for a pdf
More informationFall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives
CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write
More informationINTERPRETATION AND SCHEME LISTS 12
INTERPRETATION AND SCHEME LISTS 12 COMPUTER SCIENCE 61A July 26, 2012 1 Calculator Language We are beginning to dive into the realm of interpreting computer programs. To do so, we will examine a few new
More informationChapter 15 Functional Programming Languages
Chapter 15 Functional Programming Languages Fundamentals of Functional Programming Languages Introduction to Scheme A programming paradigm treats computation as the evaluation of mathematical functions.
More informationa little more on macros sort of like functions, but..
a little more on macros 1 sort of like functions, but.. one of the most interesting but tricky aspects of Lisp. unlike functions, macros don't evaluate their arguments; they compute on unevaluated expressions
More informationIntro. Scheme Basics. scm> 5 5. scm>
Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if
More informationPart I Logic programming paradigm
Part I Logic programming paradigm 1 Logic programming and pure Prolog 1.1 Introduction 3 1.2 Syntax 4 1.3 The meaning of a program 7 1.4 Computing with equations 9 1.5 Prolog: the first steps 15 1.6 Two
More information