CS 476 Programming Language Design William Mansky Logic

  • Slides: 35
Download presentation
CS 476 – Programming Language Design William Mansky

CS 476 – Programming Language Design William Mansky

Logic Programming • Declarative programming: say what you want, not how to do it

Logic Programming • Declarative programming: say what you want, not how to do it • A logic program consists of a series of logical assertions, and a query: man(socrates). mortal(X) : - man(X). ? - mortal(socrates). true. 1

Logic Programming • Declarative programming: say what you want, not how to do it

Logic Programming • Declarative programming: say what you want, not how to do it • A logic program consists of a series of logical assertions, and a query: man(socrates). mortal(X) : - man(X). ? - mortal(X). X = socrates. 2

Logic Programming age(person 1, 21). age(person 2, 23). age(person 3, 25). age(person 4, 27).

Logic Programming age(person 1, 21). age(person 2, 23). age(person 3, 25). age(person 4, 27). older(X, Y) : - age(X, Xage), age(Y, Yage), Xage > Yage. ? - older (X, person 1), older(Y, X). X = person 2, Y = person 3; X = person 2, Y = person 4; X = person 3, Y=person 4. 3

Logic Programming: Syntax T : : = true | <ident> | <#> | <Ident>

Logic Programming: Syntax T : : = true | <ident> | <#> | <Ident> | <ident>(T, …, T) R : : = T : - T, …, T. Q : : = ? - T, …, T. P : : = R … R Q Syntactic sugar: t. => t : - true. 4

Logic Programming: Execution • Maintain a list of goals that still need to be

Logic Programming: Execution • Maintain a list of goals that still need to be proved • Pick a goal to prove next • Find a rule whose conclusion matches the goal, and apply it: ― Instantiate it to match the goal, by unification ― Replace the goal with the instantiated premises of the rule • If no rules apply, backtrack to the last decision point and make a different choice • If all goals are solved, output the solution 5

Logic Programming: Execution Rules: age(person 1, 21), …, older(X, Y) : - … Goals:

Logic Programming: Execution Rules: age(person 1, 21), …, older(X, Y) : - … Goals: older(X, person 1), older(Y, X) older(X, Y) : - age(X, Xage), age(Y, Yage), Xage > Yage. 6

Logic Programming: Execution • 7

Logic Programming: Execution • 7

Logic Programming: Execution • 8

Logic Programming: Execution • 8

Logic Programming: Execution • 9

Logic Programming: Execution • 9

Logic Programming: Execution • 10

Logic Programming: Execution • 10

Logic Programming: Execution Rules: age(person 1, 21), …, older(X, Y) : - … Goals:

Logic Programming: Execution Rules: age(person 1, 21), …, older(X, Y) : - … Goals: 21 > 21, older(Y, person 1) Unproveable! 11

Logic Programming: Execution • 12

Logic Programming: Execution • 12

Logic Programming: Execution • 13

Logic Programming: Execution • 13

Logic Programming: Execution • 14

Logic Programming: Execution • 14

Logic Programming: Execution • Maintain a list of goals that still need to be

Logic Programming: Execution • Maintain a list of goals that still need to be proved • Pick a goal to prove next • Find a rule whose conclusion matches the goal, and apply it: ― Instantiate it to match the goal, by unification ― Replace the goal with the instantiated premises of the rule • If no rules apply, backtrack to the last decision point and make a different choice • If all goals are solved, output the solution 15

Logic Programming: Semantics • 16

Logic Programming: Semantics • 16

Logic Programming: Semantics 17

Logic Programming: Semantics 17

Logic Programming: Semantics • 18

Logic Programming: Semantics • 18

Logic Programming: Execution • Note: this language is Turing-complete! • So there are non-terminating

Logic Programming: Execution • Note: this language is Turing-complete! • So there are non-terminating logic programs • Syntax-directed rule systems avoid backtracking, but might still fail to terminate 19

Logic Programming: Cut • Consider: max(X, Y, X) : - X >= Y. (*

Logic Programming: Cut • Consider: max(X, Y, X) : - X >= Y. (* max rule 1 *) max(X, Y, Y) : - X < Y. (* max rule 2 *) Goals: max(78, 51, Z), gs Stack: [(gs 1, …); …] 20

Logic Programming: Cut • 21

Logic Programming: Cut • 21

Logic Programming: Cut • 22

Logic Programming: Cut • 22

Logic Programming: Cut • Consider: max(X, Y, X) : - X >= Y. (*

Logic Programming: Cut • Consider: max(X, Y, X) : - X >= Y. (* max rule 1 *) max(X, Y, Y) : - X < Y. (* max rule 2 *) Goals: max(78, 51, Z), gs Stack: [(gs 1, …); …] 23

Logic Programming: Cut • We can fix this with a cut: max(X, Y, X)

Logic Programming: Cut • We can fix this with a cut: max(X, Y, X) : - X >= Y, !. (* max rule 1 *) max(X, Y, Y) : - X < Y. (* max rule 2 *) Goals: max(78, 51, Z), gs Stack: [(gs 1, …); …] 24

Logic Programming: Cut • 25

Logic Programming: Cut • 25

Logic Programming: Cut • 26

Logic Programming: Cut • 26

Logic Programming: Cut • We can fix this with a cut: max(X, Y, X)

Logic Programming: Cut • We can fix this with a cut: max(X, Y, X) : - X >= Y, !. (* max rule 1 *) max(X, Y, Y) : - X < Y. (* max rule 2 *) Goals: gs 1 Stack: […] 27

Logic Programming: Cut • We can cut after a complicated computation: prop(X, Y) :

Logic Programming: Cut • We can cut after a complicated computation: prop(X, Y) : - p 1(X), p 2(X), p 3(Y), !. 28

Logic Programming: Syntax T : : = true | <ident> | <#> | <Ident>

Logic Programming: Syntax T : : = true | <ident> | <#> | <Ident> | <ident>(T, …, T) | ! R : : = T : - T, …, T. Q : : = ? - T, …, T. P : : = R … R Q Syntactic sugar: t. => t : - true. 29

Logic Programming: Semantics 30

Logic Programming: Semantics 30

Logic Programming: Semantics 31

Logic Programming: Semantics 31

Logic Programming: Semantics 32

Logic Programming: Semantics 32

Logic Programming: Semantics 33

Logic Programming: Semantics 33

Logic Programming • A form of declarative programming where we give a set of

Logic Programming • A form of declarative programming where we give a set of rules and then ask questions about what can be proved • Searches for a proof tree for the query, filling in variables as it goes, and backtracking when it hits a dead end • Uses unification to figure out how to apply a rule to a goal • Useful for databases and knowledge retrieval systems • Can be used for PL too, but not as efficient as syntax-directed algorithms 34