documentl2

113
COMP 213 Advanced Object-oriented Programming Lecture 13 Propositional Logic

Upload: lksoo

Post on 28-Jun-2015

47 views

Category:

Education


0 download

DESCRIPTION

nth

TRANSCRIPT

Page 1: DocumentL2

COMP 213

Advanced Object-oriented Programming

Lecture 13

PropositionalLogic

Page 2: DocumentL2

Task

Develop a program that:

allows a user to enter a string representing a term inpropositional logic;prints out the term with minimal and maximal brackets;prints out a truth table for the term.

(actually, we won’t have time for the third one)

Page 3: DocumentL2

Task

Develop a program that:

allows a user to enter a string representing a term inpropositional logic;prints out the term with minimal and maximal brackets;prints out a truth table for the term.

(actually, we won’t have time for the third one)

Page 4: DocumentL2

Propositional Logic

Propositional logic is the logic of truth and reasoning. It isconcerned with how some statements follow logically fromother statements to form logical arguments.It was first systematically studied by Aristotle, and is still ofinterest in philosophical logic (mainly as one of the simplestnon-trivial logics).It is also relevant to Computer Science as the basis of Booleanlogic.

Page 5: DocumentL2

George Boole, 1815–1864

In 1854, Boole published AnInvestigation into the Laws ofThought, on Which are founded theMathematical Theories of Logic andProbabilities.

This work related logical operators and binary arithmetic, and isthe basis for what is now called Boolean logic(which is actually just another name for Propositional Logic).

Page 6: DocumentL2

Propositional Logic

Propositional Logic is a language consisting of terms that arebuilt from variables, the constants true and false, and Booleanoperators (‘not’, ‘and’, ’or’, etc.).

In BNF:

〈Prop〉 ::= 〈Var〉 | true | false | not 〈Prop〉 |〈Prop〉 and 〈Prop〉 | 〈Prop〉 or 〈Prop〉 |〈Prop〉 implies 〈Prop〉

〈Var〉 ::= ’A | ’B | ’C | · · ·

There is a reason for the single quotes before the variables . . . :we’ll use one of Maude’s built-in types for these.

Page 7: DocumentL2

Propositional Logic

Propositional Logic is a language consisting of terms that arebuilt from variables, the constants true and false, and Booleanoperators (‘not’, ‘and’, ’or’, etc.).

In BNF:

〈Prop〉 ::= 〈Var〉 | true | false | not 〈Prop〉 |〈Prop〉 and 〈Prop〉 | 〈Prop〉 or 〈Prop〉 |〈Prop〉 implies 〈Prop〉

〈Var〉 ::= ’A | ’B | ’C | · · ·

There is a reason for the single quotes before the variables . . . :we’ll use one of Maude’s built-in types for these.

Page 8: DocumentL2

Propositional Logic

Propositional Logic is a language consisting of terms that arebuilt from variables, the constants true and false, and Booleanoperators (‘not’, ‘and’, ’or’, etc.).

In BNF:

〈Prop〉 ::= 〈Var〉 | true | false | not 〈Prop〉 |〈Prop〉 and 〈Prop〉 | 〈Prop〉 or 〈Prop〉 |〈Prop〉 implies 〈Prop〉

〈Var〉 ::= ’A | ’B | ’C | · · ·

There is a reason for the single quotes before the variables . . . :we’ll use one of Maude’s built-in types for these.

Page 9: DocumentL2

Propositional ADT

Propositions also form an Abstract Data Type.

The propositions themselves are the abstract data values.

The operations are the constants (variables, true, false) and theoperators (not, and, or, implies).

We’ll specify this ADT in Maude, then implement it in Java.

Page 10: DocumentL2

Propositional ADT

Propositions also form an Abstract Data Type.

The propositions themselves are the abstract data values.

The operations are the constants (variables, true, false) and theoperators (not, and, or, implies).

We’ll specify this ADT in Maude, then implement it in Java.

Page 11: DocumentL2

Propositional ADT

Propositions also form an Abstract Data Type.

The propositions themselves are the abstract data values.

The operations are the constants (variables, true, false) and theoperators (not, and, or, implies).

We’ll specify this ADT in Maude, then implement it in Java.

Page 12: DocumentL2

Propositional ADT

Propositions also form an Abstract Data Type.

The propositions themselves are the abstract data values.

The operations are the constants (variables, true, false) and theoperators (not, and, or, implies).

We’ll specify this ADT in Maude, then implement it in Java.

Page 13: DocumentL2

Maude, Maude, Maude

file: PropLog.maude

fmod PROP LOGIC is

*** import Maude’s quoted identifiers,*** renaming the sort Qid to PVar (for "Propositional Variable")protecting QID *(sort Qid to PVar) .

Maude’s built-in module QID declares a sort Qid of ‘quotedidentifiers’; these are very like strings, except rather than beingenclosed in double quotes ("), they are preceded by a singleclosing quote (’). E.g., ’a, ’aa, ’abc, etc.This simply means that the sort Qid is now called PVar

Page 14: DocumentL2

Maude, Maude, Maude

file: PropLog.maude

fmod PROP LOGIC is

*** import Maude’s quoted identifiers,*** renaming the sort Qid to PVar (for "Propositional Variable")protecting QID *(sort Qid to PVar) .

Maude’s built-in module QID declares a sort Qid of ‘quotedidentifiers’; these are very like strings, except rather than beingenclosed in double quotes ("), they are preceded by a singleclosing quote (’). E.g., ’a, ’aa, ’abc, etc.This simply means that the sort Qid is now called PVar

Page 15: DocumentL2

Maude, Maude, Maude

file: PropLog.maude

fmod PROP LOGIC is

*** import Maude’s quoted identifiers,*** renaming the sort Qid to PVar (for "Propositional Variable")protecting QID *(sort Qid to PVar) .

Maude’s built-in module QID declares a sort Qid of ‘quotedidentifiers’; these are very like strings, except rather than beingenclosed in double quotes ("), they are preceded by a singleclosing quote (’). E.g., ’a, ’aa, ’abc, etc.This simply means that the sort Qid is now called PVar

Page 16: DocumentL2

Maude, Maude, Maude

file: PropLog.maude

fmod PROP LOGIC is

*** import Maude’s quoted identifiers,*** renaming the sort Qid to PVar (for "Propositional Variable")protecting QID *(sort Qid to PVar) .

Maude’s built-in module QID declares a sort Qid of ‘quotedidentifiers’; these are very like strings, except rather than beingenclosed in double quotes ("), they are preceded by a singleclosing quote (’). E.g., ’a, ’aa, ’abc, etc.This simply means that the sort Qid is now called PVar

Page 17: DocumentL2

Maude, Maude, Maude

file: PropLog.maude

fmod PROP LOGIC is

*** import Maude’s quoted identifiers,*** renaming the sort Qid to PVar (for "Propositional Variable")protecting QID *(sort Qid to PVar) .

Maude’s built-in module QID declares a sort Qid of ‘quotedidentifiers’; these are very like strings, except rather than beingenclosed in double quotes ("), they are preceded by a singleclosing quote (’). E.g., ’a, ’aa, ’abc, etc.This simply means that the sort Qid is now called PVar

Page 18: DocumentL2

Maude, Maude, Maude

file: PropLog.maude

fmod PROP LOGIC is

*** import Maude’s quoted identifiers,*** renaming the sort Qid to PVar (for "Propositional Variable")protecting QID *(sort Qid to PVar) .

Maude’s built-in module QID declares a sort Qid of ‘quotedidentifiers’; these are very like strings, except rather than beingenclosed in double quotes ("), they are preceded by a singleclosing quote (’). E.g., ’a, ’aa, ’abc, etc.This simply means that the sort Qid is now called PVar

Page 19: DocumentL2

Maude, Maude, Maude

file: PropLog.maude

fmod PROP LOGIC is

*** import Maude’s quoted identifiers,*** renaming the sort Qid to PVar (for "Propositional Variable")protecting QID *(sort Qid to PVar) .

Maude’s built-in module QID declares a sort Qid of ‘quotedidentifiers’; these are very like strings, except rather than beingenclosed in double quotes ("), they are preceded by a singleclosing quote (’). E.g., ’a, ’aa, ’abc, etc.This simply means that the sort Qid is now called PVar

Page 20: DocumentL2

Maude, Maude, Maude

file: PropLog.maude

fmod PROP LOGIC is

*** import Maude’s quoted identifiers,*** renaming the sort Qid to PVar (for "Propositional Variable")protecting QID *(sort Qid to PVar) .

Maude’s built-in module QID declares a sort Qid of ‘quotedidentifiers’; these are very like strings, except rather than beingenclosed in double quotes ("), they are preceded by a singleclosing quote (’). E.g., ’a, ’aa, ’abc, etc.This simply means that the sort Qid is now called PVar

Page 21: DocumentL2

More Maude

file PropLog.maude

*** propositionssort Prop .

*** every variable is a propositionsubsort PVar < Prop .

NB this is Maude, not Java; subsorts are not the same thing asinheritance — we’re not suggesting

class Prop extends PVar { ... }

but it does mean that PVars can be used whenever a Prop isexpected.

Page 22: DocumentL2

More Maude

file PropLog.maude

*** propositionssort Prop .

*** every variable is a propositionsubsort PVar < Prop .

NB this is Maude, not Java; subsorts are not the same thing asinheritance — we’re not suggesting

class Prop extends PVar { ... }

but it does mean that PVars can be used whenever a Prop isexpected.

Page 23: DocumentL2

More Maude

file PropLog.maude

*** propositionssort Prop .

*** every variable is a propositionsubsort PVar < Prop .

NB this is Maude, not Java; subsorts are not the same thing asinheritance — we’re not suggesting

class Prop extends PVar { ... }

but it does mean that PVars can be used whenever a Prop isexpected.

Page 24: DocumentL2

More Maude

file PropLog.maude

*** propositionssort Prop .

*** every variable is a propositionsubsort PVar < Prop .

NB this is Maude, not Java; subsorts are not the same thing asinheritance — we’re not suggesting

class Prop extends PVar { ... }

but it does mean that PVars can be used whenever a Prop isexpected.

Page 25: DocumentL2

More Maude

file PropLog.maude

*** propositionssort Prop .

*** every variable is a propositionsubsort PVar < Prop .

NB this is Maude, not Java; subsorts are not the same thing asinheritance — we’re not suggesting

class Prop extends PVar { ... }

but it does mean that PVars can be used whenever a Prop isexpected.

Page 26: DocumentL2

More Maude

file: PropLog.maude

ops true false : -> Prop .op and : Prop Prop -> Prop .op or : Prop Prop -> Prop .op implies : Prop Prop -> Prop .op not : Prop -> Prop .

Note: one underscore for each argument

The first three operations are infix; ‘not’ is prefix

Example propositions:

not ’a and ’b implies ’c or true

Page 27: DocumentL2

More Maude

file: PropLog.maude

ops true false : -> Prop .op and : Prop Prop -> Prop .op or : Prop Prop -> Prop .op implies : Prop Prop -> Prop .op not : Prop -> Prop .

Note: one underscore for each argument

The first three operations are infix; ‘not’ is prefix

Example propositions:

not ’a and ’b implies ’c or true

Page 28: DocumentL2

More Maude

file: PropLog.maude

ops true false : -> Prop .op and : Prop Prop -> Prop .op or : Prop Prop -> Prop .op implies : Prop Prop -> Prop .op not : Prop -> Prop .

Note: one underscore for each argument

The first three operations are infix; ‘not’ is prefix

Example propositions:

not ’a and ’b implies ’c or true

Page 29: DocumentL2

More Maude

file: PropLog.maude

ops true false : -> Prop .op and : Prop Prop -> Prop .op or : Prop Prop -> Prop .op implies : Prop Prop -> Prop .op not : Prop -> Prop .

Note: one underscore for each argument

The first three operations are infix; ‘not’ is prefix

Example propositions:

not ’a and ’b implies ’c or true

Page 30: DocumentL2

More Maude

file: PropLog.maude

ops true false : -> Prop .op and : Prop Prop -> Prop .op or : Prop Prop -> Prop .op implies : Prop Prop -> Prop .op not : Prop -> Prop .

Note: one underscore for each argument

The first three operations are infix; ‘not’ is prefix

Example propositions:

not ’a and ’b implies ’c or true

Page 31: DocumentL2

More Maude

file: PropLog.maude

ops true false : -> Prop .op and : Prop Prop -> Prop .op or : Prop Prop -> Prop .op implies : Prop Prop -> Prop .op not : Prop -> Prop .

Note: one underscore for each argument

The first three operations are infix; ‘not’ is prefix

Example propositions:

not ’a and ’b implies ’c or true

Page 32: DocumentL2

More Maude

file: PropLog.maude

ops true false : -> Prop .op and : Prop Prop -> Prop .op or : Prop Prop -> Prop .op implies : Prop Prop -> Prop .op not : Prop -> Prop .

Note: one underscore for each argument

The first three operations are infix; ‘not’ is prefix

Example propositions:

not ’a and ’b implies ’c or true

Page 33: DocumentL2

More Maude

file: PropLog.maude

ops true false : -> Prop .op and : Prop Prop -> Prop .op or : Prop Prop -> Prop .op implies : Prop Prop -> Prop .op not : Prop -> Prop .

Note: one underscore for each argument

The first three operations are infix; ‘not’ is prefix

Example propositions:

not ’a and ’b implies ’c or true

Page 34: DocumentL2

More Maude

file: PropLog.maude

ops true false : -> Prop .op and : Prop Prop -> Prop .op or : Prop Prop -> Prop .op implies : Prop Prop -> Prop .op not : Prop -> Prop .

Note: one underscore for each argument

The first three operations are infix; ‘not’ is prefix

Example propositions:

not ’a and ’b implies ’c or true

Page 35: DocumentL2

More Maude

file: PropLog.maude

ops true false : -> Prop .op and : Prop Prop -> Prop .op or : Prop Prop -> Prop .op implies : Prop Prop -> Prop .op not : Prop -> Prop .

Note: one underscore for each argument

The first three operations are infix; ‘not’ is prefix

Example propositions:

not ’a and ’b implies ’c or true

Page 36: DocumentL2

More Examples of Terms

’atruenot ’anot false’a and true’a and not ’b’c or ’a and not ’bnot ’c implies not ’b and not not ’a

As we can see from the last two examples, terms can beambiguous:(’c or ’a) and not ’b / ’c or (’a and not ’b)

Page 37: DocumentL2

More Examples of Terms

’atruenot ’anot false’a and true’a and not ’b’c or ’a and not ’bnot ’c implies not ’b and not not ’a

As we can see from the last two examples, terms can beambiguous:(’c or ’a) and not ’b / ’c or (’a and not ’b)

Page 38: DocumentL2

(Brackets)

We can use brackets to disambiguate terms; e.g.,(’c or ’a) and not ’b

We can also use conventions so that we sometimes don’t needbrackets to disambiguate terms:we give a precedence to each operator; operations with a lowprecedence are more tightly binding.

file: PropLog.maude

op and : Prop Prop -> Prop [prec 40] .op or : Prop Prop -> Prop [prec 44] .op implies : Prop Prop -> Prop [prec 48] .op not : Prop -> Prop [prec 30] .

Page 39: DocumentL2

(Brackets)

We can use brackets to disambiguate terms; e.g.,(’c or ’a) and not ’b

We can also use conventions so that we sometimes don’t needbrackets to disambiguate terms:we give a precedence to each operator; operations with a lowprecedence are more tightly binding.

file: PropLog.maude

op and : Prop Prop -> Prop [prec 40] .op or : Prop Prop -> Prop [prec 44] .op implies : Prop Prop -> Prop [prec 48] .op not : Prop -> Prop [prec 30] .

Page 40: DocumentL2

(Brackets)

We can use brackets to disambiguate terms; e.g.,(’c or ’a) and not ’b

We can also use conventions so that we sometimes don’t needbrackets to disambiguate terms:we give a precedence to each operator; operations with a lowprecedence are more tightly binding.

file: PropLog.maude

op and : Prop Prop -> Prop [prec 40] .op or : Prop Prop -> Prop [prec 44] .op implies : Prop Prop -> Prop [prec 48] .op not : Prop -> Prop [prec 30] .

Page 41: DocumentL2

Examples

Term Means’a and ’b or ’c (’a and ’b) or ’c

not ’a and ’b implies ’c ((not ’a) and ’b) implies ’cnot ’a and (’b implies ’c) (not ’a) and (’b implies ’c)’a and ’b or ’c implies ’d ((’a and ’b) or ’c) implies ’d

This disambiguates terms with different operators

but what about terms with the same operators, e.g.’a and ’b and ’c ?

Page 42: DocumentL2

Examples

Term Means’a and ’b or ’c (’a and ’b) or ’c

not ’a and ’b implies ’c ((not ’a) and ’b) implies ’cnot ’a and (’b implies ’c) (not ’a) and (’b implies ’c)’a and ’b or ’c implies ’d ((’a and ’b) or ’c) implies ’d

This disambiguates terms with different operators

but what about terms with the same operators, e.g.’a and ’b and ’c ?

Page 43: DocumentL2

Associativity

We will adopt the convention that binary operators areright-associative. I.e.,

Term Means’a or ’b or ’c or ’d or ’e ’a or (’b or (’c or (’d or ’e)))’a and ’b and ’c and ’d ’a and (’b and (’c and ’d))’a implies ’b implies ’c ’a implies (’b implies ’c)

etc.

Page 44: DocumentL2

Associativity

In fact, we will use a semantic property of and and or (calledassociativity), which says that any way of bracketing

’a and ’b and ’c and ’dgives the same result, so we can omit brackets entirely(and similarly for or).

file: PropLog.maude

op and : Prop Prop -> Prop [assoc prec 40] .op or : Prop Prop -> Prop [assoc prec 44] .op implies : Prop Prop -> Prop [prec 48] .op not : Prop -> Prop [prec 30] .

Page 45: DocumentL2

Associativity

In fact, we will use a semantic property of and and or (calledassociativity), which says that any way of bracketing

’a and ’b and ’c and ’dgives the same result, so we can omit brackets entirely(and similarly for or).

file: PropLog.maude

op and : Prop Prop -> Prop [assoc prec 40] .op or : Prop Prop -> Prop [assoc prec 44] .op implies : Prop Prop -> Prop [prec 48] .op not : Prop -> Prop [prec 30] .

Page 46: DocumentL2

Printing with Brackets

We still need to specify operations to print with maximal andminimal bracketing.

file: PropLog.maude

op printAllBrackets : Prop -> String .

We specify what this operation does by considering what itsarguments may be: a PVar, true, false, or a term built from theother four operations (and, or, implies, not).

Page 47: DocumentL2

Printing with Brackets

We still need to specify operations to print with maximal andminimal bracketing.

file: PropLog.maude

op printAllBrackets : Prop -> String .

We specify what this operation does by considering what itsarguments may be: a PVar, true, false, or a term built from theother four operations (and, or, implies, not).

Page 48: DocumentL2

Printing with Brackets

file: PropLog.maude

eq printAllBrackets(true) = "(true)" .eq printAllBrackets(false) = "(false)" .

var V : PVar .eq printAllBrackets(V) = "(" + string(V) + ")" .

module QIDop string : Qid -> String .

module QID *(sort Qid to PVar)op string : PVar -> String .

e.g., string(’example) = "example"

Page 49: DocumentL2

Printing with Brackets

file: PropLog.maude

eq printAllBrackets(true) = "(true)" .eq printAllBrackets(false) = "(false)" .

var V : PVar .eq printAllBrackets(V) = "(" + string(V) + ")" .

module QIDop string : Qid -> String .

module QID *(sort Qid to PVar)op string : PVar -> String .

e.g., string(’example) = "example"

Page 50: DocumentL2

Printing with Brackets

file: PropLog.maude

eq printAllBrackets(true) = "(true)" .eq printAllBrackets(false) = "(false)" .

var V : PVar .eq printAllBrackets(V) = "(" + string(V) + ")" .

module QIDop string : Qid -> String .

module QID *(sort Qid to PVar)op string : PVar -> String .

e.g., string(’example) = "example"

Page 51: DocumentL2

Printing with Brackets

file: PropLog.maude

eq printAllBrackets(true) = "(true)" .eq printAllBrackets(false) = "(false)" .

var V : PVar .eq printAllBrackets(V) = "(" + string(V) + ")" .

module QIDop string : Qid -> String .

module QID *(sort Qid to PVar)op string : PVar -> String .

e.g., string(’example) = "example"

Page 52: DocumentL2

Printing with Brackets

file: PropLog.maude

eq printAllBrackets(true) = "(true)" .eq printAllBrackets(false) = "(false)" .

var V : PVar .eq printAllBrackets(V) = "(" + string(V) + ")" .

module QIDop string : Qid -> String .

module QID *(sort Qid to PVar)op string : PVar -> String .

e.g., string(’example) = "example"

Page 53: DocumentL2

Printing with Brackets

file: PropLog.maude

eq printAllBrackets(true) = "(true)" .eq printAllBrackets(false) = "(false)" .

var V : PVar .eq printAllBrackets(V) = "(" + string(V) + ")" .

module QIDop string : Qid -> String .

module QID *(sort Qid to PVar)op string : PVar -> String .

e.g., string(’example) = "example"

Page 54: DocumentL2

Printing with Brackets

file: PropLog.maude

eq printAllBrackets(true) = "(true)" .eq printAllBrackets(false) = "(false)" .

var V : PVar .eq printAllBrackets(V) = "(" + string(V) + ")" .

module QIDop string : Qid -> String .

module QID *(sort Qid to PVar)op string : PVar -> String .

e.g., string(’example) = "example"

Page 55: DocumentL2

Printing with Brackets

file: PropLog.maude

eq printAllBrackets(true) = "(true)" .eq printAllBrackets(false) = "(false)" .

var V : PVar .eq printAllBrackets(V) = "(" + string(V) + ")" .

module QIDop string : Qid -> String .

module QID *(sort Qid to PVar)op string : PVar -> String .

e.g., string(’example) = "example"

Page 56: DocumentL2

Printing with Brackets

file: PropLog.maude

eq printAllBrackets(true) = "(true)" .eq printAllBrackets(false) = "(false)" .

var V : PVar .eq printAllBrackets(V) = "(" + string(V) + ")" .

module QIDop string : Qid -> String .

module QID *(sort Qid to PVar)op string : PVar -> String .

e.g., string(’example) = "example"

Page 57: DocumentL2

Printing with Brackets

file: PropLog.maudevars P P1 P2 : Prop .

eq printAllBrackets(P1 and P2) ="(" + printAllBrackets(P1) + " and "+ printAllBrackets(P2) + ")" .*** similarly for ‘or’ and ‘implies’. . .

eq printAllBrackets(not P) = "(not " + printAllBrackets(P) + ")" .

Page 58: DocumentL2

Printing with Brackets

file: PropLog.maudevars P P1 P2 : Prop .

eq printAllBrackets(P1 and P2) ="(" + printAllBrackets(P1) + " and "+ printAllBrackets(P2) + ")" .*** similarly for ‘or’ and ‘implies’. . .

eq printAllBrackets(not P) = "(not " + printAllBrackets(P) + ")" .

Page 59: DocumentL2

Printing with Brackets

file: PropLog.maudevars P P1 P2 : Prop .

eq printAllBrackets(P1 and P2) ="(" + printAllBrackets(P1) + " and "+ printAllBrackets(P2) + ")" .*** similarly for ‘or’ and ‘implies’. . .

eq printAllBrackets(not P) = "(not " + printAllBrackets(P) + ")" .

Page 60: DocumentL2

Printing with Brackets

file: PropLog.maudevars P P1 P2 : Prop .

eq printAllBrackets(P1 and P2) ="(" + printAllBrackets(P1) + " and "+ printAllBrackets(P2) + ")" .*** similarly for ‘or’ and ‘implies’. . .

eq printAllBrackets(not P) = "(not " + printAllBrackets(P) + ")" .

Page 61: DocumentL2

Printing with Brackets

file: PropLog.maudevars P P1 P2 : Prop .

eq printAllBrackets(P1 and P2) ="(" + printAllBrackets(P1) + " and "+ printAllBrackets(P2) + ")" .*** similarly for ‘or’ and ‘implies’. . .

eq printAllBrackets(not P) = "(not " + printAllBrackets(P) + ")" .

Page 62: DocumentL2

Printing with Brackets

file: PropLog.maudevars P P1 P2 : Prop .

eq printAllBrackets(P1 and P2) ="(" + printAllBrackets(P1) + " and "+ printAllBrackets(P2) + ")" .*** similarly for ‘or’ and ‘implies’. . .

eq printAllBrackets(not P) = "(not " + printAllBrackets(P) + ")" .

Page 63: DocumentL2

Printing with Brackets

file: PropLog.maudevars P P1 P2 : Prop .

eq printAllBrackets(P1 and P2) ="(" + printAllBrackets(P1) + " and "+ printAllBrackets(P2) + ")" .*** similarly for ‘or’ and ‘implies’. . .

eq printAllBrackets(not P) = "(not " + printAllBrackets(P) + ")" .

Page 64: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

op toString : Prop -> String .

eq toString(true) = "true" .eq toString(false) = "false" .eq toString(V) = string(V) .eq toString(P1 and P2) = ? .

If we just write

eq toString(P1 and P2) = toString(P1) + " and " + toString(P2) .

(and similarly for or, implies, not)

then we’ll never get any brackets anywhere.

Page 65: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

op toString : Prop -> String .

eq toString(true) = "true" .eq toString(false) = "false" .eq toString(V) = string(V) .eq toString(P1 and P2) = ? .

If we just write

eq toString(P1 and P2) = toString(P1) + " and " + toString(P2) .

(and similarly for or, implies, not)

then we’ll never get any brackets anywhere.

Page 66: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

op toString : Prop -> String .

eq toString(true) = "true" .eq toString(false) = "false" .eq toString(V) = string(V) .eq toString(P1 and P2) = ? .

If we just write

eq toString(P1 and P2) = toString(P1) + " and " + toString(P2) .

(and similarly for or, implies, not)

then we’ll never get any brackets anywhere.

Page 67: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

op toString : Prop -> String .

eq toString(true) = "true" .eq toString(false) = "false" .eq toString(V) = string(V) .eq toString(P1 and P2) = ? .

If we just write

eq toString(P1 and P2) = toString(P1) + " and " + toString(P2) .

(and similarly for or, implies, not)

then we’ll never get any brackets anywhere.

Page 68: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

op toString : Prop -> String .

eq toString(true) = "true" .eq toString(false) = "false" .eq toString(V) = string(V) .eq toString(P1 and P2) = ? .

If we just write

eq toString(P1 and P2) = toString(P1) + " and " + toString(P2) .

(and similarly for or, implies, not)

then we’ll never get any brackets anywhere.

Page 69: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

op toString : Prop -> String .

eq toString(true) = "true" .eq toString(false) = "false" .eq toString(V) = string(V) .eq toString(P1 and P2) = ? .

If we just write

eq toString(P1 and P2) = toString(P1) + " and " + toString(P2) .

(and similarly for or, implies, not)

then we’ll never get any brackets anywhere.

Page 70: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

op toString : Prop -> String .

eq toString(true) = "true" .eq toString(false) = "false" .eq toString(V) = string(V) .eq toString(P1 and P2) = ? .

If we just write

eq toString(P1 and P2) = toString(P1) + " and " + toString(P2) .

(and similarly for or, implies, not)

then we’ll never get any brackets anywhere.

Page 71: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

eq toString(P1 and P2) = ? .

If P1 is of the form P3 and P4, then we don’t need bracketsaround the first argument.

E.g.,

toString(’a and ’b and ’c) = "’a and ’b and ’c" .

But if P1 is of the form P3 or P4, then we do need bracketsaround the first argument.

E.g.,

toString(’a or ’b and ’c) = "(’a or ’b) and ’c" .

Page 72: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

eq toString(P1 and P2) = ? .

If P1 is of the form P3 and P4, then we don’t need bracketsaround the first argument.

E.g.,

toString(’a and ’b and ’c) = "’a and ’b and ’c" .

But if P1 is of the form P3 or P4, then we do need bracketsaround the first argument.

E.g.,

toString(’a or ’b and ’c) = "(’a or ’b) and ’c" .

Page 73: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

eq toString(P1 and P2) = ? .

If P1 is of the form P3 and P4, then we don’t need bracketsaround the first argument.

E.g.,

toString(’a and ’b and ’c) = "’a and ’b and ’c" .

But if P1 is of the form P3 or P4, then we do need bracketsaround the first argument.

E.g.,

toString(’a or ’b and ’c) = "(’a or ’b) and ’c" .

Page 74: DocumentL2

Printing with Minimal Brackets

In the second case, brackets were needed because or haslower precedence than and.

We’ll add an extra parameter that represents ‘the precedenceof the operator above a term’.This gives us exactly the information we need in order to decidewhether an operand requires brackets around it.

file: PropLog.maude

*** put brackets around the proposition if the precedence of*** its main operator is greater than the given integerop toStringPrec : Prop Int -> String .

Page 75: DocumentL2

Printing with Minimal Brackets

In the second case, brackets were needed because or haslower precedence than and.

We’ll add an extra parameter that represents ‘the precedenceof the operator above a term’.This gives us exactly the information we need in order to decidewhether an operand requires brackets around it.

file: PropLog.maude

*** put brackets around the proposition if the precedence of*** its main operator is greater than the given integerop toStringPrec : Prop Int -> String .

Page 76: DocumentL2

Printing with Minimal Brackets

In the second case, brackets were needed because or haslower precedence than and.

We’ll add an extra parameter that represents ‘the precedenceof the operator above a term’.This gives us exactly the information we need in order to decidewhether an operand requires brackets around it.

file: PropLog.maude

*** put brackets around the proposition if the precedence of*** its main operator is greater than the given integerop toStringPrec : Prop Int -> String .

Page 77: DocumentL2

Printing with Minimal Brackets

In the second case, brackets were needed because or haslower precedence than and.

We’ll add an extra parameter that represents ‘the precedenceof the operator above a term’.This gives us exactly the information we need in order to decidewhether an operand requires brackets around it.

file: PropLog.maude

*** put brackets around the proposition if the precedence of*** its main operator is greater than the given integerop toStringPrec : Prop Int -> String .

Page 78: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude*** no operator has precedence greater than 48,*** so no outermost bracketseq toString(P) = toStringPrec(P, 48) .

var I : Int .

eq toStringPrec(true, I) = "true" .

eq toStringPrec(false, I) = "false" .

eq toStringPrec(V, I) = string(V) .

Page 79: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude*** no operator has precedence greater than 48,*** so no outermost bracketseq toString(P) = toStringPrec(P, 48) .

var I : Int .

eq toStringPrec(true, I) = "true" .

eq toStringPrec(false, I) = "false" .

eq toStringPrec(V, I) = string(V) .

Page 80: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude*** no operator has precedence greater than 48,*** so no outermost bracketseq toString(P) = toStringPrec(P, 48) .

var I : Int .

eq toStringPrec(true, I) = "true" .

eq toStringPrec(false, I) = "false" .

eq toStringPrec(V, I) = string(V) .

Page 81: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude*** no operator has precedence greater than 48,*** so no outermost bracketseq toString(P) = toStringPrec(P, 48) .

var I : Int .

eq toStringPrec(true, I) = "true" .

eq toStringPrec(false, I) = "false" .

eq toStringPrec(V, I) = string(V) .

Page 82: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

cq toStringPrec(P1 and P2, I) =toStringPrec(P1, 40) + " and " + toStringPrec(P2, 40)if I >= 40 . *** no brackets needed

cq toStringPrec(P1 and P2, I) ="(" + toStringPrec(P1, 40) + " and " + toStringPrec(P2, 40)+ ")"if I < 40 . *** brackets are needed

so P1 and P2 will have brackets if neededsimilarly for or and not

Page 83: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

cq toStringPrec(P1 and P2, I) =toStringPrec(P1, 40) + " and " + toStringPrec(P2, 40)if I >= 40 . *** no brackets needed

cq toStringPrec(P1 and P2, I) ="(" + toStringPrec(P1, 40) + " and " + toStringPrec(P2, 40)+ ")"if I < 40 . *** brackets are needed

so P1 and P2 will have brackets if neededsimilarly for or and not

Page 84: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

cq toStringPrec(P1 and P2, I) =toStringPrec(P1, 40) + " and " + toStringPrec(P2, 40)if I >= 40 . *** no brackets needed

cq toStringPrec(P1 and P2, I) ="(" + toStringPrec(P1, 40) + " and " + toStringPrec(P2, 40)+ ")"if I < 40 . *** brackets are needed

so P1 and P2 will have brackets if neededsimilarly for or and not

Page 85: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

cq toStringPrec(P1 and P2, I) =toStringPrec(P1, 40) + " and " + toStringPrec(P2, 40)if I >= 40 . *** no brackets needed

cq toStringPrec(P1 and P2, I) ="(" + toStringPrec(P1, 40) + " and " + toStringPrec(P2, 40)+ ")"if I < 40 . *** brackets are needed

so P1 and P2 will have brackets if neededsimilarly for or and not

Page 86: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

cq toStringPrec(P1 and P2, I) =toStringPrec(P1, 40) + " and " + toStringPrec(P2, 40)if I >= 40 . *** no brackets needed

cq toStringPrec(P1 and P2, I) ="(" + toStringPrec(P1, 40) + " and " + toStringPrec(P2, 40)+ ")"if I < 40 . *** brackets are needed

so P1 and P2 will have brackets if neededsimilarly for or and not

Page 87: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

cq toStringPrec(P1 and P2, I) =toStringPrec(P1, 40) + " and " + toStringPrec(P2, 40)if I >= 40 . *** no brackets needed

cq toStringPrec(P1 and P2, I) ="(" + toStringPrec(P1, 40) + " and " + toStringPrec(P2, 40)+ ")"if I < 40 . *** brackets are needed

so P1 and P2 will have brackets if neededsimilarly for or and not

Page 88: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

cq toStringPrec(P1 and P2, I) =toStringPrec(P1, 40) + " and " + toStringPrec(P2, 40)if I >= 40 . *** no brackets needed

cq toStringPrec(P1 and P2, I) ="(" + toStringPrec(P1, 40) + " and " + toStringPrec(P2, 40)+ ")"if I < 40 . *** brackets are needed

so P1 and P2 will have brackets if neededsimilarly for or and not

Page 89: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

cq toStringPrec(P1 and P2, I) =toStringPrec(P1, 40) + " and " + toStringPrec(P2, 40)if I >= 40 . *** no brackets needed

cq toStringPrec(P1 and P2, I) ="(" + toStringPrec(P1, 40) + " and " + toStringPrec(P2, 40)+ ")"if I < 40 . *** brackets are needed

so P1 and P2 will have brackets if neededsimilarly for or and not

Page 90: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

cq toStringPrec(P1 and P2, I) =toStringPrec(P1, 40) + " and " + toStringPrec(P2, 40)if I >= 40 . *** no brackets needed

cq toStringPrec(P1 and P2, I) ="(" + toStringPrec(P1, 40) + " and " + toStringPrec(P2, 40)+ ")"if I < 40 . *** brackets are needed

so P1 and P2 will have brackets if neededsimilarly for or and not

Page 91: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

cq toStringPrec(P1 implies P2, I) =toStringPrec(P1, 47) + " implies " + toStringPrec(P2, 48)if I >= 48 . *** no brackets needed

cq toStringPrec(P1 implies P2, I) ="(" + toStringPrec(P1, 47) + " implies " + toStringPrec(P2, 48)+ ")"if I < 48 . *** brackets are needed

This gives us right associativity:P1 will have brackets round it if its main operator is ‘implies’;P2 will not

Page 92: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

cq toStringPrec(P1 implies P2, I) =toStringPrec(P1, 47) + " implies " + toStringPrec(P2, 48)if I >= 48 . *** no brackets needed

cq toStringPrec(P1 implies P2, I) ="(" + toStringPrec(P1, 47) + " implies " + toStringPrec(P2, 48)+ ")"if I < 48 . *** brackets are needed

This gives us right associativity:P1 will have brackets round it if its main operator is ‘implies’;P2 will not

Page 93: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

cq toStringPrec(P1 implies P2, I) =toStringPrec(P1, 47) + " implies " + toStringPrec(P2, 48)if I >= 48 . *** no brackets needed

cq toStringPrec(P1 implies P2, I) ="(" + toStringPrec(P1, 47) + " implies " + toStringPrec(P2, 48)+ ")"if I < 48 . *** brackets are needed

This gives us right associativity:P1 will have brackets round it if its main operator is ‘implies’;P2 will not

Page 94: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

cq toStringPrec(P1 implies P2, I) =toStringPrec(P1, 47) + " implies " + toStringPrec(P2, 48)if I >= 48 . *** no brackets needed

cq toStringPrec(P1 implies P2, I) ="(" + toStringPrec(P1, 47) + " implies " + toStringPrec(P2, 48)+ ")"if I < 48 . *** brackets are needed

This gives us right associativity:P1 will have brackets round it if its main operator is ‘implies’;P2 will not

Page 95: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

cq toStringPrec(P1 implies P2, I) =toStringPrec(P1, 47) + " implies " + toStringPrec(P2, 48)if I >= 48 . *** no brackets needed

cq toStringPrec(P1 implies P2, I) ="(" + toStringPrec(P1, 47) + " implies " + toStringPrec(P2, 48)+ ")"if I < 48 . *** brackets are needed

This gives us right associativity:P1 will have brackets round it if its main operator is ‘implies’;P2 will not

Page 96: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

cq toStringPrec(P1 implies P2, I) =toStringPrec(P1, 47) + " implies " + toStringPrec(P2, 48)if I >= 48 . *** no brackets needed

cq toStringPrec(P1 implies P2, I) ="(" + toStringPrec(P1, 47) + " implies " + toStringPrec(P2, 48)+ ")"if I < 48 . *** brackets are needed

This gives us right associativity:P1 will have brackets round it if its main operator is ‘implies’;P2 will not

Page 97: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

cq toStringPrec(P1 implies P2, I) =toStringPrec(P1, 47) + " implies " + toStringPrec(P2, 48)if I >= 48 . *** no brackets needed

cq toStringPrec(P1 implies P2, I) ="(" + toStringPrec(P1, 47) + " implies " + toStringPrec(P2, 48)+ ")"if I < 48 . *** brackets are needed

This gives us right associativity:P1 will have brackets round it if its main operator is ‘implies’;P2 will not

Page 98: DocumentL2

Printing with Minimal Brackets

file: PropLog.maude

cq toStringPrec(P1 implies P2, I) =toStringPrec(P1, 47) + " implies " + toStringPrec(P2, 48)if I >= 48 . *** no brackets needed

cq toStringPrec(P1 implies P2, I) ="(" + toStringPrec(P1, 47) + " implies " + toStringPrec(P2, 48)+ ")"if I < 48 . *** brackets are needed

This gives us right associativity:P1 will have brackets round it if its main operator is ‘implies’;P2 will not

Page 99: DocumentL2

Maude Interpreterreduce toString((’a or ’b) and ’c) .=

toStringPrec((’a or ’b) and ’c, 48)= *** 48 >= 40; no brackets needed

toStringPrec(’a or ’b, 40) + " and " + toStringPrec(’c, 40)= *** 40 < 44; brackets are needed

"(" + toStringPrec(’a, 44) + " or " + toStringPrec(’b, 44) + ")"+ " and " + toStringPrec(’c, 40)

="(" + string(’a) + " or " + string(’b) + ")"+ " and " + string(’c)

="(a or b) and c"

Page 100: DocumentL2

Maude Interpreterreduce toString((’a or ’b) and ’c) .=

toStringPrec((’a or ’b) and ’c, 48)= *** 48 >= 40; no brackets needed

toStringPrec(’a or ’b, 40) + " and " + toStringPrec(’c, 40)= *** 40 < 44; brackets are needed

"(" + toStringPrec(’a, 44) + " or " + toStringPrec(’b, 44) + ")"+ " and " + toStringPrec(’c, 40)

="(" + string(’a) + " or " + string(’b) + ")"+ " and " + string(’c)

="(a or b) and c"

Page 101: DocumentL2

Maude Interpreterreduce toString((’a or ’b) and ’c) .=

toStringPrec((’a or ’b) and ’c, 48)= *** 48 >= 40; no brackets needed

toStringPrec(’a or ’b, 40) + " and " + toStringPrec(’c, 40)= *** 40 < 44; brackets are needed

"(" + toStringPrec(’a, 44) + " or " + toStringPrec(’b, 44) + ")"+ " and " + toStringPrec(’c, 40)

="(" + string(’a) + " or " + string(’b) + ")"+ " and " + string(’c)

="(a or b) and c"

Page 102: DocumentL2

Maude Interpreterreduce toString((’a or ’b) and ’c) .=

toStringPrec((’a or ’b) and ’c, 48)= *** 48 >= 40; no brackets needed

toStringPrec(’a or ’b, 40) + " and " + toStringPrec(’c, 40)= *** 40 < 44; brackets are needed

"(" + toStringPrec(’a, 44) + " or " + toStringPrec(’b, 44) + ")"+ " and " + toStringPrec(’c, 40)

="(" + string(’a) + " or " + string(’b) + ")"+ " and " + string(’c)

="(a or b) and c"

Page 103: DocumentL2

Maude Interpreterreduce toString((’a or ’b) and ’c) .=

toStringPrec((’a or ’b) and ’c, 48)= *** 48 >= 40; no brackets needed

toStringPrec(’a or ’b, 40) + " and " + toStringPrec(’c, 40)= *** 40 < 44; brackets are needed

"(" + toStringPrec(’a, 44) + " or " + toStringPrec(’b, 44) + ")"+ " and " + toStringPrec(’c, 40)

="(" + string(’a) + " or " + string(’b) + ")"+ " and " + string(’c)

="(a or b) and c"

Page 104: DocumentL2

Maude Interpreterreduce toString((’a or ’b) and ’c) .=

toStringPrec((’a or ’b) and ’c, 48)= *** 48 >= 40; no brackets needed

toStringPrec(’a or ’b, 40) + " and " + toStringPrec(’c, 40)= *** 40 < 44; brackets are needed

"(" + toStringPrec(’a, 44) + " or " + toStringPrec(’b, 44) + ")"+ " and " + toStringPrec(’c, 40)

="(" + string(’a) + " or " + string(’b) + ")"+ " and " + string(’c)

="(a or b) and c"

Page 105: DocumentL2

Maude Interpreterreduce toString((’a or ’b) and ’c) .=

toStringPrec((’a or ’b) and ’c, 48)= *** 48 >= 40; no brackets needed

toStringPrec(’a or ’b, 40) + " and " + toStringPrec(’c, 40)= *** 40 < 44; brackets are needed

"(" + toStringPrec(’a, 44) + " or " + toStringPrec(’b, 44) + ")"+ " and " + toStringPrec(’c, 40)

="(" + string(’a) + " or " + string(’b) + ")"+ " and " + string(’c)

="(a or b) and c"

Page 106: DocumentL2

Maude Interpreterreduce toString((’a or ’b) and ’c) .=

toStringPrec((’a or ’b) and ’c, 48)= *** 48 >= 40; no brackets needed

toStringPrec(’a or ’b, 40) + " and " + toStringPrec(’c, 40)= *** 40 < 44; brackets are needed

"(" + toStringPrec(’a, 44) + " or " + toStringPrec(’b, 44) + ")"+ " and " + toStringPrec(’c, 40)

="(" + string(’a) + " or " + string(’b) + ")"+ " and " + string(’c)

="(a or b) and c"

Page 107: DocumentL2

Maude Interpreterreduce toString((’a or ’b) and ’c) .=

toStringPrec((’a or ’b) and ’c, 48)= *** 48 >= 40; no brackets needed

toStringPrec(’a or ’b, 40) + " and " + toStringPrec(’c, 40)= *** 40 < 44; brackets are needed

"(" + toStringPrec(’a, 44) + " or " + toStringPrec(’b, 44) + ")"+ " and " + toStringPrec(’c, 40)

="(" + string(’a) + " or " + string(’b) + ")"+ " and " + string(’c)

="(a or b) and c"

Page 108: DocumentL2

Maude Interpreterreduce toString((’a or ’b) and ’c) .=

toStringPrec((’a or ’b) and ’c, 48)= *** 48 >= 40; no brackets needed

toStringPrec(’a or ’b, 40) + " and " + toStringPrec(’c, 40)= *** 40 < 44; brackets are needed

"(" + toStringPrec(’a, 44) + " or " + toStringPrec(’b, 44) + ")"+ " and " + toStringPrec(’c, 40)

="(" + string(’a) + " or " + string(’b) + ")"+ " and " + string(’c)

="(a or b) and c"

Page 109: DocumentL2

Maude Interpreterreduce toString((’a or ’b) and ’c) .=

toStringPrec((’a or ’b) and ’c, 48)= *** 48 >= 40; no brackets needed

toStringPrec(’a or ’b, 40) + " and " + toStringPrec(’c, 40)= *** 40 < 44; brackets are needed

"(" + toStringPrec(’a, 44) + " or " + toStringPrec(’b, 44) + ")"+ " and " + toStringPrec(’c, 40)

="(" + string(’a) + " or " + string(’b) + ")"+ " and " + string(’c)

="(a or b) and c"

Page 110: DocumentL2

Back to Java

public class Prop {

public Prop and(Prop p1, Prop p2) { ... }

// similarly for ‘or’, etc.

public String printAllBrackets() { ...}

public String toString() { ...}

}

We need a data representation.

Page 111: DocumentL2

Back to Java

public class Prop {

public Prop and(Prop p1, Prop p2) { ... }

// similarly for ‘or’, etc.

public String printAllBrackets() { ...}

public String toString() { ...}

}

We need a data representation.

Page 112: DocumentL2

Back to Java

public class Prop {

public Prop and(Prop p1, Prop p2) { ... }

// similarly for ‘or’, etc.

public String printAllBrackets() { ...}

public String toString() { ...}

}

We need a data representation.

Page 113: DocumentL2

That’s All, Folks!

Summary

sort PropMaude equationshelper functions

Next:

data representation