Symbolic Computation

Similar documents
CS 314 Principles of Programming Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Functional Programming. Pure Functional Languages

Simplifying Logical Formulas

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

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

Documentation for LISP in BASIC

CSE 341 Section Handout #6 Cheat Sheet

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

Functional Programming. Pure Functional Languages

Scheme: Expressions & Procedures

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

FP Foundations, Scheme

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

Functional Programming. Pure Functional Programming

Functional Programming. Big Picture. Design of Programming Languages

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

CS61A Midterm 2 Review (v1.1)

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

User-defined Functions. Conditional Expressions in Scheme

Functional Programming

LECTURE 16. Functional Programming

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

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

CS 314 Principles of Programming Languages

Scheme Quick Reference

A brief tour of history

Modern Programming Languages. Lecture LISP Programming Language An Introduction

SOFTWARE ARCHITECTURE 6. LISP

Programming Languages

Scheme Quick Reference

CSc 520 Principles of Programming Languages

A Brief Introduction to Scheme (II)

CS 314 Principles of Programming Languages. Lecture 16

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

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

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

Lisp. Versions of LISP

CSc 520 Principles of Programming Languages

Functional Languages. Hwansoo Han

Functional Programming - 2. Higher Order Functions

CS 360 Programming Languages Interpreters

Propositional Calculus: Boolean Algebra and Simplification. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

Introduction to Scheme

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

Discussion 12 The MCE (solutions)

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

Propositional Calculus. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

6.001: Structure and Interpretation of Computer Programs

Programming Languages

Functional Programming Lecture 1: Introduction

Box-and-arrow Diagrams

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

4/19/2018. Chapter 11 :: Functional Languages

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

YOUR NAME PLEASE: *** SOLUTIONS ***

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

Extensible Pattern Matching

Chapter 15 Functional Programming Languages

1.3. Conditional expressions To express case distinctions like

Circuit analysis summary

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

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

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

8.3 Common Loop Patterns

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

Common LISP Tutorial 1 (Basic)

Lists of Lists. CS 5010 Program Design Paradigms Bootcamp Lesson 5.3

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

Case Study: Undefined Variables

CS 314 Principles of Programming Languages

Principles of Programming Languages 2017W, Functional Programming

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

A Brief Introduction to Scheme (I)

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

Chapter 15. Functional Programming Languages

Racket: Modules, Contracts, Languages

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

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

Boolean Algebra A B A AND B = A*B A B A OR B = A+B

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

Week 2: The Clojure Language. Background Basic structure A few of the most useful facilities. A modernized Lisp. An insider's opinion

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CS 275 Name Final Exam Solutions December 16, 2016

Chapter 1. Fundamentals of Higher Order Programming

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Bawar Abid Abdalla. Assistant Lecturer Software Engineering Department Koya University

Imperative, OO and Functional Languages A C program is

Finite Model Generation for Isabelle/HOL Using a SAT Solver

Lecture #24: Programming Languages and Programs

Functional Programming

What is tail position?

The syntax and semantics of Beginning Student

The syntax and semantics of Beginning Student

How to Design Programs

Imperative languages

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I

CS115 - Module 9 - filter, map, and friends

CSCC24 Functional Programming Scheme Part 2

Transcription:

Symbolic Computation Principles of Programming Languages https://lambda.mines.edu

1 What questions did you have on the reading? Can your group members answer, or you can ask me. 2 Define symbolic computation in your own words. 3 What structures in Racket would you find useful for symbolic computation? 4 Share what other applications you came up with for symbolic computation. Formulate some more with your group.

Wikipedia considers symbolic computation to be simply computer algebra. While computer algebra is a form of symbolic computation, there are plenty of other applications. Programming languages Compilers Artificial intelligence...

Lisp dialects have a homoiconic syntax: the code is data, and data is code. Lists being the structure of the language syntax, code can be manipulated just like lists. The concept of "quoting" is fairly unique to just Lisp. It leads to a natural way to manipulate and work on code in the language. Key point: we can manipulate code before it is evaluated! John McCarthy (1958) Recursive Functions of Symbolic Expressions and their Computation by Machine Today we will explore a practical application of symbolic computation in artificial intelligence.

To represent boolean expressions in Racket, we need to formalize an s-expression syntax for them: Conjunction a b c... (and a b c...) Disjunction a b c... (or a b c...) Negation a (not a) Practice: convert to s-expression 1 a (b c d) d 2 a (a b) (a b)

Note Depending on your background, you may already know this. Bear with me while I explain it to everyone else. A boolean expression is in conjunctive normal form (CNF) if and only if all of the below are true: It only contains conjunctions, disjunctions, and negations. Negations only contain a variable, not a conjunction or disjunction. Disjunctions only contain variables and negations. Example: (a b c) ( a b) Learning Group Activity Come up with an expression in CNF (not the example), and one not in CNF.

(define/match (in-cnf? expr [level 'root]) [((? symbol?) _) #t] [(`(not,(? symbol?)) _) #t] [((list-rest 'or args) (or 'root 'and)) (andmap (λ (x) (in-cnf? x 'or)) args)] [((list-rest 'and args) 'root) (andmap (λ (x) (in-cnf? x 'and)) args)] [( ) #f])

We can convert any boolean expression composed of just conjunctions, disjunctions, and negations to CNF using the following mathematical properties: Elimination of double-negation: a a DeMorgan s Law (Conjunction): (a b) ( a b) DeMorgan s Law (Disjunction): (a b) ( a b) Distributive Property: a (b c) (a b) (a c)

Convert each expression to CNF: 1 (a b) 2 ((a b) (c d)) 3 ((a b) (c d))

Here s the base structure we want our code to follow: (define (boolean->cnf expr) (if (in-cnf? expr) expr (boolean->cnf (match expr...)))) ;; cases for the conversions we know

[`(not (not,e)) e]

[`(or,e) e] [`(and,e) e]

DeMorgan s Law for Conjunction [`(not (and,@(list-rest args))) `(or,@(map (curry list 'not) args))] DeMorgan s Law for Disjunction [`(not (or,@(list-rest args))) `(and,@(map (curry list 'not) args))]

and in and simplification [`(and,@(list-no-order (list-rest 'and inside) outside...)) `(and,@inside,@outside)] or in or simplification [`(or,@(list-no-order (list-rest 'or inside) outside...)) `(or,@inside,@outside)]

[`(or,@(list-no-order (list-rest 'and and-args) args...)) `(or,@(cdr args) (and,@(map (λ (x) (list 'or (car args) x)) and-args)))]

[(list-rest sym args) (cons sym (map boolean->cnf args))]

> (boolean->cnf '(or (and a b) (and (not c) d) (and (not e) f))) '(and (or (not c) a (not e)) (or (not c) b (not e)) (or d a (not e)) (or d b (not e)) (or (not c) a f) (or (not c) b f) (or d a f) (or d b f))

The satisfiability problem 1 in computer science asks: Given a boolean expression, is there any set of assignments to the variables which results in the equation evaluating to true? For example: (and a (not a)): not satisfiable (and a a): satisfiable (you could imagine much larger inputs) 1 If you ve taken algorithms, you probably know that this problem is NP-complete

Note procedure DPLL(e): if e is true: return true if e is false: return false v select-variable(e) e 1 simplify(assume-true(v, e)) if DPLL(e 1 ): return true e 2 simplify(assume-false(v, e)) return DPLL(e 2 ) DPLL will work with any variable selection from select-variable, but certain selections may lead to a more efficent solution on average than others.

We never reached true, so this equation is not satisfiable 1. Assume a is true and simplify b c (b c) a ( a b) c (b c) 6. a = false false 2. b = true false 3. b = false c c 4. c = true false 5. c = false false

Draw the DPLL tree for the following expression, and determine whether the equation is satisfiable or not: (a b) ( a b) ( a b)

(define (solve-cnf expr) (define (solve-rec expr bindings) (case expr [(#t) bindings] [(#f) #f] [else (let ([sym (choose-symbol expr)]) (define (solve-assume value) (solve-rec (assume sym value expr) (cons (cons sym value) bindings))) (let ([sym-true (solve-assume #t)]) (if sym-true sym-true (solve-assume #f))))])) (solve-rec expr '()))

Not a good heuristic, but it works! (define (choose-symbol expr) (if (symbol? expr) expr (choose-symbol (cadr expr))))

(define (assume var value expr) (cond [(eq? var expr) value] [(equal? `(not,var) expr) (not value)] [(symbol? expr) expr] [else (match expr [`(not,_) expr] [(list-rest sym args)...])])) ;; handle conjunction/disjunction

(let ([look-for (case sym [(and) #f] [(or) #t])]) (define (f item acc) (if (eq? acc look-for) acc (let ([result (assume var value item)]) (cond [(eq? result look-for) result] [(eq? result (not look-for)) acc] [else (cons result acc)])))) (let ([result (foldl f '() args)]) (cond [(null? result) (not look-for)] [(eq? result look-for) result] [else (cons sym result)])))

(define (solve expr) (solve-cnf (boolean->cnf expr))) > (solve '(and a b)) '((b. #t) (a. #t)) > (solve '(or (and a b) (and c d) (and e f))) '((d. #t) (f. #t) (c. #t)) > (solve '(and a (not a))) #f > (solve '(and (or (not a) b) (or a (not b)))) '((b. #t) (a. #t))