Principles of Programming Languages, Spring 2016 Assignment 5 Logic Programming

Size: px
Start display at page:

Download "Principles of Programming Languages, Spring 2016 Assignment 5 Logic Programming"

Transcription

1 Principles of Programming Languages, Spring 2016 Assignment 5 Logic Programming Submission instructions: a. Submit an archive file named id1_id2.zip where id1 and id2 are the IDs of the students responsible for the submission (or id1.zip for one student in the group). b. A contract must be included for every implemented procedure (Signature, Purpose, possibly Examples). E.g.: % Signature: member(x,l) % Purpose: holds when item X occurs within list L. % Examples: % member(2, [1, 2, 3]) true % member(x, [1,2,3]) X = 1 c. Use exact procedure and file names, as your code will be tested automatically. d. Answer theoretical questions in ex5.pdf. You can scan hand-drawn trees and graphs and add them to the submitted work. e. Answer coding questions in their specified files. f. Your implementation will be held correct if it returns answers in any order given a query, as long as all the correct answers are returned, and only the correct answers are returned, and infinite computations are avoided. Question 1: Comprehension Questions Answer in file ex5.pdf. a. Specify two differences and a similarity between composite terms and atomic formulas in LP. b. According to the answer-query algorithm given in class, is every proof tree in Relational Logic Programming finite? Explain. c. Suggest how the answer-query algorithm can be modified, so that it always finishes the computation. That is, infinite branches are cropped at some point in which it is obvious that the branch is infinite. You can assume that there exists a given value N(P, Q), which equals the number of all atomic formulas possible, with respect to a given program P and a given query Q, in RLP. List the changes that should be done to the answer-query algorithm given in the lecture notes (p.348, repeated here for convenience). Tree operations: make_node(label): Creates a node labeled label. add_branch(tree, edge_label, branch): Adds branch as a right branch to the root node of tree, with edge labeled by edge_label. label(node) selects the label of node. -1-

2 answer-query algorithm: Signature: answer-query(q,p,gsel,rsel) Input: A query: Q =?- G1,..., Gn., where Gi, i = 1..n is an atomic formula A program P Output: A set of substitutions for variables of Q (not necessarily for all variables). Method: 1. answers = {} 2. PT := proof-tree(make_node(q)). 3. Return answers/q the restriction of the substitutions in answers to the variables of Q. proof-tree: Signature: proof-tree(node). (node is a tree node with label query edges are labelled with substitutions) Input: A tree node node Output: A proof tree rooted in node. Method: if label(node) is?- true,..., true. then 1. Mark node as a Success node (leaf). 2. answers = answers {s1 s2... sn}, where s1,..., sn are the substitution labels of the path from the tree-root to node. Mark node with this substitution. else 1. Goal selection: G = Gsel(label(node)) 2. Rename variables in every rule and fact of P 3. Rule selection: rules = Rsel(G, P) = ( R, σ ) R P, G σ=head(r) σ 4. apply-rules(node, G, rules) d. Define and give examples for a program with (1) success finite proof tree, (2) success infinite proof tree, (3) failure proof tree, and (4) infinite failure proof tree. Question 2. Unify Answer in file ex5.pdf. a. What is the result of the following operations? Provide the computation steps according to the algorithm presented in the Course book p.345. Explain in case of failure. 1. unify[n(d(d),d,d,k,n(n),k),n(d(d),d,d,k,n(n),d)] 2. unify[k(n,d,k,k,n,k,a(d)),k(n,a(n),k,a(d),a(k),k,a(d))] 3. unify[d(k,d),k(k,d)] 4. unify[p(.(.(a,d),k)),p(.(a,.(d,k)))] 5. unify[p([[a D] K]),p([a [D K]])] b. In chapter 5 we used unification of type expressions and of typing statements, in order to infer a type for a language expression. For example, we computed (in Ex.3, question 5), the mgu of the expressions: {f:[t1 -> [T2 -> T1]], x:t1} - (f x):t1 {f:t3, x:t4} - (f x):[t1 -> T2] Compare the two kinds of unification processes. Suggest two differences and two similarities. -2-

3 Question 3: Relational Logic Programming (with SQL operations) Answer in file ex5.pl. Answer the question using SQL operations, as defined in Section in the book. The procedure not_member, which is provided in file ex5-aux.pl can be used. Relational databases are useful for managing large amounts of data. Each table in the database represents a relation. Elementary query operations, such as select, project, join and Cartesian-product enable data access. A relational database for Book-Library management is given in the file ex5-aux.pl. The database consists of four tables, represented as fact-based procedures: 1) The table book(id, Name, Author, Category, Local)/5 contains information about books available in the library. Each book has an id (that identifies it uniquely), a name, a single (for simplicity) author id (from the author table), a category (fiction, children, and so on), and boolean that defines whether this book is written locally or abroad. 2) The table editor(id, Name)/2 is the editors list. 3) The table author(id, Name)/2 is the authors list. 4) The table editorial(book, Editor)/2 contains an entry with two ids for each editorial role. a. Write a procedure local_children_book_authoring(book, Author)/2 that defines the relation between a book name and its author name, for local children books. Which SQL operations are performed by this procedure? Mark in the contract. b. Write a procedure busy_editor(name)/1 that identifies editors that edit at least two books. Which SQL operations are performed by this procedure? Mark in the contract. c. Write a procedure people(book, List)/2 that defines the relation between a book name and a list of names of all people participated in the book preparation - author and editors (in any order). This question involves lists (non-rlp elements). Question 4: Lists management. Answer in file ex5.pl. a. sub procedure Write a procedure sub(sublist, List)/2 that defines the following relation between two lists: % Signature: sub(sublist, List)/2 % Purpose: All elements in Sublist appear in List in the same order. % Precondition: List is fully instantiated (queries do not include variables in their first argument). % Example: %?- sub(x, [1, 2, 3]). % X = [1, 2, 3]; % X = [1, 2]; % X = [1, 3]; % X = [2, 3]; -3-

4 % X = [1]; % X = [2]; % X = [3]; % X = []; % false The following Scheme predicate implements one of the modes of this relation: ; Signature: sub?(sub, long) ; Type: [List(T) * List(T) -> Boolean] ; Purpose: Determine whether we can obtain sub by only removing items from long. ; Pre-conditions: - ; Examples: ; (sub? empty empty) -> true ; (sub? empty '(1)) -> true ; (sub? '(1) empty) -> false ; (sub? '(1) '(1 2 3)) -> true ; (sub? '(1 3) '(1 2 3)) -> true ; (sub? '(3 1) '(1 2 3)) -> false ; (sub? '(4) '(1 2 3)) -> false (define sub? (lambda (sub long) (cond ((empty? sub) #t) ((empty? long) #f) (else (let ((suffix (member (car sub) long))) (if suffix (sub? (cdr sub) (cdr suffix)) #f)))))) The following Scheme procedure computes another mode of the same relation: ; Signature: all-subs(long) ; Type: [List(T) -> List(List(T))] ; Purpose: compute all lists that can be obtained from long by removing items from it. ; Pre-conditions: - ; Tests: ; (all-subs '(1 2 3)) -> ; '(() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)) (define all-subs (lambda (long) (if (empty? long) (list empty)) (else (let ((all-rests (all-subs (cdr long)))) (append all-rests (map (lambda (sub) (cons first sub)) all-rests)))))))) b. splitlist procedure Recall the Scheme interpreter for LP. The file auxiliary.rkt has a function split-list: ; Signature: split-list(l, elt) ; Type: [List(T) * T -> Pair(List(T), List(T)) -4-

5 ; Purpose: split a list of items into a pair (prefix. suffix) s.t. (prefix elt suffix) = l ; Pre-conditions: (member elt l) ; Tests: (split '( ) 3) ==> ((1 2) 4 5) (define split-list (lambda (l elt) (letrec ((iter (lambda (prefix suffix) (if (equal? (car suffix) elt) (cons prefix (cdr suffix)) (iter (append prefix (list (car suffix))) (cdr suffix)))))) (iter empty l)))) Implement a predicate splitlist(l, Elt, LeftList, RightList)/4 in Prolog. Question 5: Grammars Answer in file ex5.pl. a. Compound Nouns English Form The following context-free grammar describes a very partial yet infinite subset of English. S is the start nonterminal (variable). Other non-terminals represent syntax components such as nouns, verbs, prepositions, adjectives and determiners. S --> Np Vp Np --> Det Adjs N Vp --> V Np Pp Adjs --> ε Adj Adjs Pp --> ε P Np Adj --> beautiful funny tall big Det --> a the N --> cat dog mouse house table V --> saw ate P --> in from on with The file ex5.pl includes a program that implements the above grammar. Read the contract and run the examples. Extend the grammar above to support the form of compound nouns in English. Compound nouns are formed by a sequence of nouns for example: Science book, tooth paste, bus stop, school bus, school bus stop Modify the derivation rules accordingly you will need to add a new nonterminal. Implement the change in the Prolog program. b. subcfg procedure Write a procedure subcfg(subtext, Text)/2 that defines the relation between a sentence Text and a sequence of words SubText, such that SubText can contain only words from a sentence Text (but -5-

6 not all of them), exclusively in that order. Both SubText and Text must fit the new grammar, and Text is fully instantiated. For example: subcfg([a, mouse, ate, a, table], [a, beautiful, mouse, dog, ate, a, cat, table, in, the, house]) true subcfg(x, [a, beautiful, mouse, dog, ate, a, cat, table, in, the, house]) X = [a, dog, ate, a, house]; % more outputs Question 6: Answer-query algorithm, Answer in ex5.pdf file. Church numbers (numerals) provide symbolic representation for the natural numbers. They are defined inductively: 1. zero is a Church number, 2. For a Church number N, s(n) is a Church number. The following program defines addition: ; Signature: natural_number(n)/1 Purpose: N is a natural number. natural_number(zero). %1 natural_number(s(x)) :- natural_number(x). %2 Signature: plus(x, Y, Z)/3 Purpose: Z is the sum of X and Y. plus(x, zero, X) :- natural_number(x). %1 plus(x, s(y), s(z)) :- plus(x, Y, Z). %2 a. Draw the proof tree for the query below and the given program. For success leaves, compute the substitution composition and report the answer.?- plus(s(x), s(x), s(s(s(zero)))). b. What are the answers of the answer-query algorithm for this query? c. Is this a success or a failure proof tree? Explain. d. Is this tree finite or infinite? Explain. Question 7: Scheme LP interpreter This question refers to the LP-interpreter that is presented in the course site. The architecture of the interpreter is also given in the course site, and a general description was given in the practice session. a. The substitution-adt.rkt module Answer in ex5.pdf file. Two main operations of substitutions are substitution-combination, and substitution-application to expressions in the language. The substitution-combination operation is implemented by the sub-combine procedure, -6-

7 while substitution-application is implemented by four procedures: sub-apply, sub-apply-atomicformula, sub-apply-query, sub-apply-rule. Explain why substitution-application is split into four procedures, and describe the dependencies between these four procedures you can draw a graph, in which a directed arrow describes dependency (as seen in practices about scheme evaluators). b. Gsel procedure Answer in folder interpreter_gsel. Modify the Gsel procedure to select the goal which is not true and has the minimal number of variables, instead of the first non-true goal on the left. Examples: > (Gsel (make-query (list '(member (var Y) (cons (var X) empty)) '(member (var X) (cons 2 empty)) '(member 1 (cons 2 empty))))) (member 1 (cons 2 empty)) c. Rule tracing Answer in folder interpreter_trace. 1. Nodes of the proof-tree that are constructed by answer-query carry a query and a substitution that combines all the substitutions on the edges of the proof tree. This way, the substitution in a success leaf is already an answer to the query (after restriction to the query variables). In order to trace the rules that lead to the answer, we can extend a PT-node to carry also the list of rules that lead to this node (from the tree root). Extend PT-node to include also a list of rules, where a rule is represented by its predicate and number, as in: '( (apply 3) Rule 0). In the extended version, a PY-node is a triplet: <query, substitution, Rule-list> The new kind of node will be called PT-node-rules, and has the operations: Constructor: make-pt-node-rules/3 Getters: PT-node-rules->query/1, PT-node-rules->sub/1, PT-node-rules->rules/1 2. Write a procedure LP-node-expander-rules/2, which is a variant of LP-node-expander, that expands a PT-node-rules in a way that the list of rules in child nodes is the list of rules of the parent node, extended with the rule, whose application yields the child node. Signature: LP-node-expander(PT-node, program) Type: [PT-Node-rules * Program -> List(PT-Node-rules)] The following drawing explains the operation of LP-node-expander-rules/2: < Q, s, (V1,, Vn) > s1, R1 sm, Rm < Q, s s1, (V1,, Vn,R1) > < Q, s sm, (V1,, Vn, Rm) > -7-

8 3. Write a procedure answer-query-trace which extends answer-query to return pairs of a substitution and the list of rules that create it. Signature: answer-query(query, program) Type: [Query * Program -> List(Record (Sub,List(Rule)) )] where Record(T1,, Tn) stands for a list of n elements of types T1,, Tn, respectively. Example: > (answer-query-trace (make-query '(append (cons 1 empty) (cons 2 empty) Z))) program) '({Z = (cons 1 (cons 2 empty))} ((append/3 Rule 1). ) -8-

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages Lesson Collaboration 22 An Introduction and Management to Logic Programming Dana Fisman Logic Programming ( Pure Prolog ) www.cs.bgu.ac.il/~ppl172 1 Review of Last Lecture

More information

User-defined Functions. Conditional Expressions in Scheme

User-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 information

1.3. Conditional expressions To express case distinctions like

1.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 information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages Slides by Dana Fisman based on book by Mira Balaban and lecuture notes by Michael Elhadad Lesson 15 Type Inference Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172

More information

Building a system for symbolic differentiation

Building 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 information

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

Announcements. 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 information

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Scheme 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 information

2 Ambiguity in Analyses of Idiomatic Phrases

2 Ambiguity in Analyses of Idiomatic Phrases Representing and Accessing [Textual] Digital Information (COMS/INFO 630), Spring 2006 Lecture 22: TAG Adjunction Trees and Feature Based TAGs 4/20/06 Lecturer: Lillian Lee Scribes: Nicolas Hamatake (nh39),

More information

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs CS 61A Fall 2016 Structure and Interpretation of Computer Programs Final INSTRUCTIONS You have 3 hours to complete the exam. The exam is closed book, closed notes, closed computer, closed calculator, except

More information

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages CS 314 Principles of Programming Languages Lecture 17: Functional Programming Zheng (Eddy Zhang Rutgers University April 4, 2018 Class Information Homework 6 will be posted later today. All test cases

More information

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs CS 6A Fall 206 Structure and Interpretation of Computer Programs Final Solutions INSTRUCTIONS You have hours to complete the exam. The exam is closed book, closed notes, closed computer, closed calculator,

More information

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters. CS 61A Spring 2019 Guerrilla Section 5: April 20, 2019 1 Interpreters 1.1 Determine the number of calls to scheme eval and the number of calls to scheme apply for the following expressions. > (+ 1 2) 3

More information

Scheme Basics > (butfirst '(help!)) ()

Scheme Basics > (butfirst '(help!)) () Scheme Basics > (butfirst '(help!)) () [The butfirst of a *sentence* containing one word is all but that word, i.e., the empty sentence. (BUTFIRST 'HELP!) without the inner parentheses would be butfirst

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages Lesson 14 Type Checking Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172 1 Type Checking We return to the issue of type safety we discussed informally,

More information

CS61A Midterm 2 Review (v1.1)

CS61A Midterm 2 Review (v1.1) Spring 2006 1 CS61A Midterm 2 Review (v1.1) Basic Info Your login: Your section number: Your TA s name: Midterm 2 is going to be held on Tuesday 7-9p, at 1 Pimentel. What will Scheme print? What will the

More information

Building a system for symbolic differentiation

Building 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 information

Functional Programming. Pure Functional Languages

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 information

Logic Programming and Resolution Lecture notes for INF3170/4171

Logic Programming and Resolution Lecture notes for INF3170/4171 Logic Programming and Resolution Lecture notes for INF3170/4171 Leif Harald Karlsen Autumn 2015 1 Introduction This note will explain the connection between logic and computer programming using Horn Clauses

More information

LECTURE 16. Functional Programming

LECTURE 16. Functional Programming LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

More information

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name CS 61A Midterm #2 ver1.03 -- March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name Look at the edge of your seat. Write your ROOM, seat row and number. Your row number

More information

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1 Defining Program Syntax Chapter Two Modern Programming Languages, 2nd ed. 1 Syntax And Semantics Programming language syntax: how programs look, their form and structure Syntax is defined using a kind

More information

Introduction to Scheme

Introduction 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 information

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts

More information

Functional Programming - 2. Higher Order Functions

Functional Programming - 2. Higher Order Functions Functional Programming - 2 Higher Order Functions Map on a list Apply Reductions: foldr, foldl Lexical scoping with let s Functional-11, CS5314, Sp16 BGRyder 1 Higher Order Functions Functions as 1st class

More information

Module 6. Knowledge Representation and Logic (First Order Logic) Version 2 CSE IIT, Kharagpur

Module 6. Knowledge Representation and Logic (First Order Logic) Version 2 CSE IIT, Kharagpur Module 6 Knowledge Representation and Logic (First Order Logic) 6.1 Instructional Objective Students should understand the advantages of first order logic as a knowledge representation language Students

More information

Typed Racket: Racket with Static Types

Typed 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 information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target

More information

The Typed Racket Guide

The 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 information

Functional Programming. Pure Functional Programming

Functional 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 information

Organization of Programming Languages CS3200/5200N. Lecture 11

Organization 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 information

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

Summer 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 information

Functional Programming. Pure Functional Languages

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 information

Resolution (14A) Young W. Lim 6/14/14

Resolution (14A) Young W. Lim 6/14/14 Copyright (c) 2013-2014. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free

More information

Intro. Scheme Basics. scm> 5 5. scm>

Intro. 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 information

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens Syntactic Analysis CS45H: Programming Languages Lecture : Lexical Analysis Thomas Dillig Main Question: How to give structure to strings Analogy: Understanding an English sentence First, we separate a

More information

A Brief Introduction to Scheme (II)

A 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 information

Comp 311: Sample Midterm Examination

Comp 311: Sample Midterm Examination Comp 311: Sample Midterm Examination October 29, 2007 Name: Id #: Instructions 1. The examination is closed book. If you forget the name for a Scheme operation, make up a name for it and write a brief

More information

Functional programming with Common Lisp

Functional 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 information

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

SCHEME 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 information

CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name

CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name CS 61A Midterm #2 ver1.03 -- March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name Look at the edge of your seat. Write your ROOM, seat row and number. Your row number

More information

Sample midterm 1 #1. Problem 1 (What will Scheme print?).

Sample midterm 1 #1. Problem 1 (What will Scheme print?). Sample midterm 1 #1 Problem 1 (What will Scheme print?). What will Scheme print in response to the following expressions? If an expression produces an error message, you may just say error ; you don t

More information

1 of 5 5/11/2006 12:10 AM CS 61A Spring 2006 Midterm 2 solutions 1. Box and pointer. Note: Please draw actual boxes, as in the book and the lectures, not XX and X/ as in these ASCII-art solutions. Also,

More information

Chapter 15. Functional Programming Languages

Chapter 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 information

Foundations of AI. 9. Predicate Logic. Syntax and Semantics, Normal Forms, Herbrand Expansion, Resolution

Foundations of AI. 9. Predicate Logic. Syntax and Semantics, Normal Forms, Herbrand Expansion, Resolution Foundations of AI 9. Predicate Logic Syntax and Semantics, Normal Forms, Herbrand Expansion, Resolution Wolfram Burgard, Andreas Karwath, Bernhard Nebel, and Martin Riedmiller 09/1 Contents Motivation

More information

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs CS 61A Summer 2014 Structure and Interpretation of Computer Programs Final Solutions INSTRUCTIONS ˆ You have 3 hours to complete the exam. ˆ The exam is closed book, closed notes, and closed electronics,

More information

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ Specifying Syntax Language Specification Components of a Grammar 1. Terminal symbols or terminals, Σ Syntax Form of phrases Physical arrangement of symbols 2. Nonterminal symbols or syntactic categories,

More information

CSE341 Autumn 2017, Final Examination December 12, 2017

CSE341 Autumn 2017, Final Examination December 12, 2017 CSE341 Autumn 2017, Final Examination December 12, 2017 Please do not turn the page until 2:30. Rules: The exam is closed-book, closed-note, etc. except for both sides of one 8.5x11in piece of paper. Please

More information

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

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs Lexical addressing The difference between a interpreter and a compiler is really two points on a spectrum of possible

More information

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER 2012 2013 MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS Time allowed TWO hours Candidates may complete the front

More information

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CPS 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 information

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion simple and/or composition of goals hides complex control patterns not easily represented by traditional

More information

Programming Languages

Programming Languages Programming Languages Tevfik Koşar Lecture - XIII March 2 nd, 2006 1 Roadmap Functional Languages Lambda Calculus Intro to Scheme Basics Functions Bindings Equality Testing Searching 2 1 Functional Languages

More information

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27 CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter 2.1-2.7 p. 1/27 CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer

More information

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

SCHEME 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 information

Write a procedure powerset which takes as its only argument a set S and returns the powerset of S.

Write a procedure powerset which takes as its only argument a set S and returns the powerset of S. Answers to CS61 A Final of May 23, 1997 We repeat the questions; some have typos corrected with respect to the actual exam handed out. Question 1 (5 points): Let us represent a set S of unique expressions,

More information

1. 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. 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 information

CS 314 Principles of Programming Languages. Lecture 16

CS 314 Principles of Programming Languages. Lecture 16 CS 314 Principles of Programming Languages Lecture 16 Zheng Zhang Department of Computer Science Rutgers University Friday 28 th October, 2016 Zheng Zhang 1 CS@Rutgers University Class Information Reminder:

More information

CSE 417 Dynamic Programming (pt 6) Parsing Algorithms

CSE 417 Dynamic Programming (pt 6) Parsing Algorithms CSE 417 Dynamic Programming (pt 6) Parsing Algorithms Reminders > HW9 due on Friday start early program will be slow, so debugging will be slow... should run in 2-4 minutes > Please fill out course evaluations

More information

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

Symbolic 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 information

Functional Programming. Big Picture. Design of Programming Languages

Functional 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 information

CS3L Summer 2011 Final Exam, Part 2 You may leave when finished; or, you must stop promptly at 12:20

CS3L Summer 2011 Final Exam, Part 2 You may leave when finished; or, you must stop promptly at 12:20 CS3L Summer 2011 Final Exam, Part 2 You may leave when finished; or, you must stop promptly at 12:20 Name: Login: cs3- First names of the people to your left and right, if any: Left: Right: 1. If you have

More information

Principles of Programming Languages

Principles 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 information

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 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

More information

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages CS 314 Principles of Programming Languages Lecture 18: Functional Programming Zheng (Eddy) Zhang Rutgers University April 9, 2018 Review: Defining Scheme Functions (define ( lambda (

More information

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g)) CS61A Notes 02b Fake Plastic Trees Box and Pointer Diagrams QUESTIONS: Evaluate the following, and draw a box-and-pointer diagram for each. (Hint: It may be easier to draw the box-and-pointer diagram first.)

More information

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Fall 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 information

Syntax and Grammars 1 / 21

Syntax and Grammars 1 / 21 Syntax and Grammars 1 / 21 Outline What is a language? Abstract syntax and grammars Abstract syntax vs. concrete syntax Encoding grammars as Haskell data types What is a language? 2 / 21 What is a language?

More information

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

Fall 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 information

CSE413 Midterm. Question Max Points Total 100

CSE413 Midterm. Question Max Points Total 100 CSE413 Midterm 05 November 2007 Name Student ID Answer all questions; show your work. You may use: 1. The Scheme language definition. 2. One 8.5 * 11 piece of paper with handwritten notes Other items,

More information

Overview. CS389L: Automated Logical Reasoning. Lecture 6: First Order Logic Syntax and Semantics. Constants in First-Order Logic.

Overview. CS389L: Automated Logical Reasoning. Lecture 6: First Order Logic Syntax and Semantics. Constants in First-Order Logic. Overview CS389L: Automated Logical Reasoning Lecture 6: First Order Logic Syntax and Semantics Işıl Dillig So far: Automated reasoning in propositional logic. Propositional logic is simple and easy to

More information

An Explicit-Continuation Metacircular Evaluator

An Explicit-Continuation Metacircular Evaluator Computer Science (1)21b (Spring Term, 2018) Structure and Interpretation of Computer Programs An Explicit-Continuation Metacircular Evaluator The vanilla metacircular evaluator gives a lot of information

More information

STRUCTURES AND STRATEGIES FOR STATE SPACE SEARCH

STRUCTURES AND STRATEGIES FOR STATE SPACE SEARCH Slide 3.1 3 STRUCTURES AND STRATEGIES FOR STATE SPACE SEARCH 3.0 Introduction 3.1 Graph Theory 3.2 Strategies for State Space Search 3.3 Using the State Space to Represent Reasoning with the Predicate

More information

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt Data abstractions Abstractions and their variations Basic data abstractions Why data abstractions are useful Procedural abstraction Process of procedural abstraction Define formal parameters, capture process

More information

Chapter 15 Functional Programming Languages

Chapter 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 information

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018 CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018 Deliverables: Your code (submit to PLC server). A13 participation survey (on Moodle, by the day after the A13 due date). This is

More information

Trees. Binary arithmetic expressions. Visualizing binary arithmetic expressions. ((2 6) + (5 2))/(5 3) can be defined in terms of two smaller

Trees. Binary arithmetic expressions. Visualizing binary arithmetic expressions. ((2 6) + (5 2))/(5 3) can be defined in terms of two smaller Trees Readings: HtDP, sections 14, 15, 16. We will cover the ideas in the text using different examples and different terminology. The readings are still important as an additional source of examples.

More information

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams. CS 61A, Fall, 2002, Midterm #2, L. Rowe 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams. a) d) 3 1 2 3 1 2 e) b) 3 c) 1 2 3 1 2 1 2 For each of the following Scheme

More information

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

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator. .00 SICP Interpretation part Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment Environment as explicit parameter Defining new procedures Why do

More information

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs CS 61A Fall 2018 Structure and Interpretation of Computer Programs Final INSTRUCTIONS You have 3 hours to complete the exam. The exam is closed book, closed notes, closed computer, closed calculator, except

More information

CS450 - Structure of Higher Level Languages

CS450 - Structure of Higher Level Languages Spring 2018 Streams February 24, 2018 Introduction Streams are abstract sequences. They are potentially infinite we will see that their most interesting and powerful uses come in handling infinite sequences.

More information

CSE 3302 Programming Languages Lecture 8: Functional Programming

CSE 3302 Programming Languages Lecture 8: Functional Programming CSE 3302 Programming Languages Lecture 8: Functional Programming (based on the slides by Tim Sheard) Leonidas Fegaras University of Texas at Arlington CSE 3302 L8 Spring 2011 1 Functional Programming Languages

More information

Z Notation. June 21, 2018

Z Notation. June 21, 2018 Z Notation June 21, 2018 1 Definitions There are many different ways to introduce an object in a Z specification: declarations, abbreviations, axiomatic definitions, and free types. Keep in mind that the

More information

Module 10: General trees

Module 10: General trees Module 10: General trees Readings: HtDP, Sections 15 and 16 CS 115 Winter 2019 10: General trees 1 General trees Binary trees can be used for a large variety of application areas. One limitation is the

More information

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy 06-02552 Princ. of Progr. Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 6: Polymorphic Type Systems 1. Polymorphic

More information

עקרונות שפות תכנות, סמסטר ב' 2016 תרגול 7: הסקת טיפוסים סטטית. Axiomatic Type Inference

עקרונות שפות תכנות, סמסטר ב' 2016 תרגול 7: הסקת טיפוסים סטטית. Axiomatic Type Inference עקרונות שפות תכנות, סמסטר ב' 2016 תרגול 7: הסקת טיפוסים סטטית 1. Axiomatic type inference 2. Type inference using type constraints נושאי התרגול: Axiomatic Type Inference Definition (seen in class): A Type-substitution

More information

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 19 Tuesday, April 3, 2018 1 Introduction to axiomatic semantics The idea in axiomatic semantics is to give specifications

More information

CS 314 Principles of Programming Languages

CS 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 information

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018 Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018 Lecture 11 Ana Bove April 26th 2018 Recap: Regular Languages Decision properties of RL: Is it empty? Does it contain this word? Contains

More information

COMP 382: Reasoning about algorithms

COMP 382: Reasoning about algorithms Spring 2015 Unit 2: Models of computation What is an algorithm? So far... An inductively defined function Limitation Doesn t capture mutation of data Imperative models of computation Computation = sequence

More information

Introduction to Logic Programming. Ambrose

Introduction to Logic Programming. Ambrose Introduction to Logic Programming Ambrose Bonnaire-Sergeant @ambrosebs abonnairesergeant@gmail.com Introduction to Logic Programming Fundamental Logic Programming concepts Related to FP General implementation

More information

Lexical and Syntax Analysis

Lexical and Syntax Analysis Lexical and Syntax Analysis (of Programming Languages) Top-Down Parsing Lexical and Syntax Analysis (of Programming Languages) Top-Down Parsing Easy for humans to write and understand String of characters

More information

6.945 Adventures in Advanced Symbolic Programming

6.945 Adventures in Advanced Symbolic Programming MIT OpenCourseWare http://ocw.mit.edu 6.945 Adventures in Advanced Symbolic Programming Spring 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. ps.txt

More information

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs CS 6A Fall 05 Structure and Interpretation of Computer Programs Final INSTRUCTIONS You have hours to complete the exam. The exam is closed book, closed notes, closed computer, closed calculator, except

More information

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion simple and/or composition of goals hides complex control patterns not easily represented by traditional

More information

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation? Normal Order (Lazy) Evaluation Alternative models for computation: Normal (Lazy) Order Evaluation Memoization Streams Applicative Order: evaluate all arguments, then apply operator Normal Order: pass unevaluated

More information

6.001 Notes: Section 31.1

6.001 Notes: Section 31.1 6.001 Notes: Section 31.1 Slide 31.1.1 In previous lectures we have seen a number of important themes, which relate to designing code for complex systems. One was the idea of proof by induction, meaning

More information

עקרונות שפות תכנות 2018 תרגול 8 Type Inference System

עקרונות שפות תכנות 2018 תרגול 8 Type Inference System עקרונות שפות תכנות 2018 תרגול 8 Type Inference System Type Inference System The Type Inference System is a TypeScript Implementation of the algorithm for Type Checking and Inference using Type Equations.

More information

Lecture 4: January 12, 2015

Lecture 4: January 12, 2015 32002: AI (First Order Predicate Logic, Interpretation and Inferences) Spring 2015 Lecturer: K.R. Chowdhary Lecture 4: January 12, 2015 : Professor of CS (VF) Disclaimer: These notes have not been subjected

More information

Qualifying Exam in Programming Languages and Compilers

Qualifying Exam in Programming Languages and Compilers Qualifying Exam in Programming Languages and Compilers University of Wisconsin Fall 1991 Instructions This exam contains nine questions, divided into two parts. All students taking the exam should answer

More information

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley PAIRS AND LISTS 6 GEORGE WANG gswang.cs61a@gmail.com Department of Electrical Engineering and Computer Sciences University of California, Berkeley June 29, 2010 1 Pairs 1.1 Overview To represent data types

More information