Simplifying Logical Formulas

Similar documents
An Explicit-Continuation Metacircular Evaluator

Building a system for symbolic differentiation

Vectors in Scheme. Data Structures in Scheme

Building a system for symbolic differentiation

Midterm Examination (Sample Solutions), Cmput 325

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

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

ALISP interpreter in Awk

6.001 recitation 3/21/07

User-defined Functions. Conditional Expressions in Scheme

Symbolic Computation

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

CSc 520 Principles of Programming Languages

Scheme Quick Reference

CS61A Midterm 2 Review (v1.1)

Scheme Quick Reference

CS 314 Principles of Programming Languages. Lecture 16

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

CS 314 Principles of Programming Languages

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

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

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

Introduction to Scheme

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

6.001, Spring Semester, 1998, Final Exam Solutions Your Name: 2 Part c: The procedure eval-until: (define (eval-until exp env) (let ((return (eval-seq

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Lists in Lisp and Scheme

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

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

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

Discussion 4. Data Abstraction and Sequences

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?

How to Design Programs Languages

YOUR NAME PLEASE: *** SOLUTIONS ***

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

A Brief Introduction to Scheme (II)

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

Overview. CS301 Session 3. Problem set 1. Thinking recursively

Functional Programming - 2. Higher Order Functions

Imperative, OO and Functional Languages A C program is

6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

Lisp. Versions of LISP

CSc 520. Principles of Programming Languages 7: Scheme List Processing

Fall 2017 December 4, 2017

Functional Programming

CSE 341 Lecture 16. More Scheme: lists; helpers; let/let*; higher-order functions; lambdas

(scheme-1) has lambda but NOT define

CSc 520 Principles of Programming Languages. Examining Lists. Constructing Lists... 7: Scheme List Processing

Problem 1: Binary Trees: Flatten

Functional Programming. Pure Functional Languages

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

Assignment 1. Due Tuesday October 11 at 11pm. No late assignments will be accepted. What to do

CS 314 Principles of Programming Languages

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

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

CS450 - Structure of Higher Level Languages

CS 314 Principles of Programming Languages

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))

Lecture Notes on Lisp A Brief Introduction

Functional Programming. Pure Functional Programming

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

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

Fall 2017 December 4, Scheme. Instructions

6.001: Structure and Interpretation of Computer Programs

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

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

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

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

Functional Programming. Pure Functional Languages

University of Massachusetts Lowell

Lexical vs. Dynamic Scope

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

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

6.001, Fall Semester, 1998 Lecture Notes, October 27 { Object Oriented Programming 2 An environment diagram illustrating +define foo +cons 1 2,, +set-

Data abstraction, revisited

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

Lecture #24: Programming Languages and Programs

(defvar *state* nil "The current state: a list of conditions.")

CS61A Notes Week 9: Muta4on (solu4ons) Revenge of the Box- and- pointers

Func%onal Programming in Scheme and Lisp

Project 1: Part 1. Project 1 will be to calculate orthogonal polynomials. It will have several parts. x = b± b 2 4ac., x 2 = b b 2 4ac.

Review of Functional Programming

Project 2: Scheme Interpreter

Project 1: Part 1. x = b± b 2 4ac., x 2 = b b 2 4ac.

Func%onal Programming in Scheme and Lisp

1.3. Conditional expressions To express case distinctions like

6.945 Adventures in Advanced Symbolic Programming

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Lisp Basic Example Test Questions

Racket Values. cons Glues Two Values into a Pair. The Pros of cons: Pairs and Lists in Racket. Box-and-pointer diagrams for cons trees

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,

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

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

Building up a language SICP Variations on a Scheme. Meval. The Core Evaluator. Eval. Apply. 2. syntax procedures. 1.

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

Problems 3-1, 3.3, 3.4 and 3.5 in Chapter 3 of Winston and Horn's LISP (see Below)

Important Example: Gene Sequence Matching. Corrigiendum. Central Dogma of Modern Biology. Genetics. How Nucleotides code for Amino Acids

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

Sample Final Exam Questions

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

Transcription:

Simplifying Logical Formulas Assume we have a SCHEME logical expression involving the operators and, or, not, and the truth constants #t and (). Example: (and a (not b) (or b c) (not (and c (not d))) #t (and e) (or () c e)) Such expressions can be simplified in a variety of ways. Negations: Use DeMorgan s Laws and the double negation rule to drive negations inward to apply only to atoms unsimplified Negation Normal Form NNF (not (and e 1 e 2... e n )) = (or (not e 1 ) (not e 2 )... (not e n ))) (not (or e 1 e 2... e n )) = (and (not e 1 ) (not e 2 )... (not e n ))) (not (not e)) = e Assume that we have a function called demorgan that does these simplifications. In the example above, (demorgan (and a (not b) (or b c) (not (and c (not d))) #t (and e) (or () c e))) = (and a (not b) (or b c) (or (not c) d) #t (and e) (or () c e))

Further Simplifications [nnf1 ] (or () < args >) = (or < args >) (or #t < args >) = #t [nnf1 ] (and #t < args >) = (and < args >) (and () < args >) = () [nnf0 ] (or) = () (and) = #t [ ] (or X (or Y Z)) = (or X Y Z) [ ] (and X (and Y Z)) = (and X Y Z) [nnf2 ] (or X... < args >... X) = (or < args >... X) [nnf2 ] (and X... < args >... X) = (and < args >... X) [ ] (or X) = X (and X) = X Each simplification applies only to a conjunction or disjunction. We do not worry about the arguments. We do not worry about any containing expression of which the conjunction or disjunction may be an argument.

Now let s program nnf0, nnf1, nnf2,, and. (define nnf0 (lambda (e) ; reduce (and) and (or) to #t and #f (cond ((equal? e (and)) #t) ((equal? e (or)) #f) (#t e) ) )) (define nnf2 (lambda (e) ; eliminate duplicate atomic arguments (cond ((not (pair? e)) e) ((eq? (car e) not) e) (#t (cons (car e) (makeset (cdr e)))) ) )) (define makeset (lambda (l) (cond ((null? l) ()) ((null? (cdr l)) l) (#t (union (list (car l)) (makeset (cdr l)))) ) ))

(define nnf1 (lambda (e) ; eliminate #t and #f (cond ((not (pair? e)) e) ((eq? (car e) and) (cond ((member #f (cdr e)) #f) ((member #t (cdr e)) (cons and (set-difference (cdr e) (list #t))) ) (#t e) ) ) ((eq? (car e) or) (cond ((member #t (cdr e)) #t) ((member #f (cdr e)) (cons or (set-difference (cdr e) (list #f))) ) (#t e) ) ) (#t e) ) )) (define set-difference (lambda (a b) (cond ((null? a) ()) ((member (car a) b) (set-difference (cdr a) b)) (#t (cons (car a) (set-difference (cdr a) b) )) ) ))

(define (lambda (e) ; (and x) = (or x) = x (cond ((not (pair? e)) e) ((and (or (eq? (car e) and) (eq? (car e) or)) (null? (cddr e)) ) (cadr e) ) (#t e) ) )) (define (lambda (e) ; (or a b (or c d)) = (or a b c d) (cond ((not (pair? e)) e) ; (and a b (and c d)) = (and a b c d) ((eq? (car e) not) e) (#t (cons (car e) (purge (car e) (cdr e)))) )) ) (define purge (lambda (op args) ; does the real work of (cond ((null? args) ()) ((not (pair? (car args))) (cons (car args) (purge op (cdr args)))) ((eq? op (caar args)) (append (cdar args) (purge op (cdr args)))) (#t (cons (car args) (purge op (cdr args)))) ) ))

In what order should nnf0, nnf1, nnf2,, and, be applied? nnf0 nnf1 nnf2 nnf0 nnf1: Eliminating constant arguments of #t and () can transform the entire expression into (and) or (or). Notice that nnf0 does NOT create expressions applicable to nnf1. nnf0 reduces (and) to #t, which is not a disjunction or a conjunction. (It might be the argument of such a containing expression.) nnf1 Eliminating #t and () can create and s and or s with single arguments.

nnf0 nnf1 nnf2 nnf2 Eliminating duplicate arguments can create and s and or s with single arguments. nnf2 Combining or s of or s (and s of and s) can create duplicate arguments. Combining or s of or s (and s of and s) can create single argument and s/or s.

nnf0 nnf1 nnf2 If nnfi nnfj, then nnfi must be applied prior to nnfj. The above graph represents a partial order. This means that there is a linear order of the functions such that: Whenever there is a path from nnfi to nnfj in the graph, then nnfi precedes nnfj in the linear order, (and NOT vice versa). One such linear order is: nnf1, nnf0,, nnf2, This tells us how to simplify a near-nnf formula using these functions.

(define nnf (lambda (e) (nnfaux (demorgan e)))) Remember, we assume the function demorgan to push negations inward and remove double negations. So nnfaux will now do the remaining simplification work. Note that we cannot merely call nnf0 on the expression. The dependency graph only describes what happens with an expression and its immediate arguments. So we have to make sure that the arguments themselves are fully simplified first. Then we simplify the expression at hand. (define nnfaux (lambda (e) (cond ((not (pair? e)) e) (#t (let ((simpargs (map nnfaux e))) ( (nnf2 ( (nnf0 (nnf1 simpargs))))) )) ) ))