Prolog. the structure Ref: Learn Prolog Now! On line Prolog Documentation. Blackburn, Bos, Striegnitz.
|
|
- Gervase Garrison
- 5 years ago
- Views:
Transcription
1 Prolog the structure Ref: Learn Prolog Now! On line Prolog Documentation. Blackburn, Bos, Striegnitz.
2 Prolog Summary (1 page) Prolog programs consist of Facts Rules Queries a rule with no right hand side (RHS) LHS : RHS. fact/rule with value(s) / variable(s) Variables Begin with upper case letters May be instantiated on the LHS & RHS of rules Rules Composed of sub goals (other facts/rules) May be recursively defined Result returned in the last variable (or variables) Queries Facts/rules with uninstantiated variables / values, = and ; = or. = end Lists [ ] [ H T ] 2
3 Prolog important! Prolog programs consist of Facts Rules Queries Examples a rule with no right hand side (RHS) LHS : RHS. fact/rule with value(s) / variable(s) bigger(elephant, horse). // fact is_bigger(x, Y) : bigger(x, Y). // rule check facts is_bigger(x, Y) : bigger(x, Z), is_bigger(z, Y). // rule (1) is_bigger(elephant, horse). // query is_bigger(x, Y). // query // (2) (1) does there exists X bigger than Z for some Z bigger than Y? (2) what Xs are bigger than Ys? for all X and Y 3
4 Prolog Database + Query bigger(elephant, horse). bigger(horse, donkey). bigger(donkey, dog). bigger(donkey, monkey). bigger(x,y). X = elephant, X = horse, X = donkey, X = donkey, Y = horse; Y = donkey; Y = dog; Y = monkey. is_bigger(x, Y) : bigger(x, Y). is_bigger(x, Y) : bigger(x, Z), is_bigger(z, Y). is_bigger(x,y). X = elephant, X = horse, X = donkey, X = donkey, X = elephant; X = elephant, X = elephant, X = horse, X = horse, false. Y = horse; Y = donkey; Y = dog; Y = monkey; Y = donkey; Y = dog; Y = monkey; Y = dog; Y = monkey; 4
5 Prolog important! Prolog variables begin with an upper case letter Variables are only instantiated within a given fact or rule E.g. is_bigger(x, Y) : bigger(x, Z), is_bigger(z, Y). X, Y, Z only refer to values within this rule NOT between rules Variables may be instantiated in the LHS & RHS of rules A Prolog rule is a goal often composed of sub goals task(a,d) : sub_goal_a(a,b), sub_goal_b(b,c), sub_goal_c(c,d). The final variable (or variables) on the LHS is often the result For sub goals this provides the input to the next sub goal The RHS of rules are evaluated sequentially Sub goals may (often) be recursive 5
6 Review of Prolog 1 What are the main features of Prolog? bigger(elephant, horse). % facts bigger(donkey, monkey). is_bigger(x, Y) : bigger(x, Y). % rules is_bigger(x, Y) : bigger(x, Z), is_bigger(z, Y). facts + rules = knowledge base rule = head + body / fact = rule with head only facts and rules are tested sequentially if the first fails, the next one is tested variable names begin with an upper case letter or _ The last argument in a predicate is often the result 6
7 Review of Prolog 1 task(a,d) : sub_goal_a(a,b), sub_goal_b(b,c), sub_goal_c(c,d). name instantiations apply only within one rule variables are either INSTANTIATED or UNINSTANTIATED variables may be instantiated in the head OR body of a rule i.e. information passing can be left to right or right to left! lists are either empty [ ] or non empty [H T] testall : tell('library.out'), showall, told, halt. % I/O? findall(x, is_bigger(x, monkey), L). L = [donkey, elephant, horse]. (sorted) findall collects information in a list >prolog % start system? [ abc.pl ]. % load a file (quick version) 7
8 Thinking in Prolog This is not obvious! Recall in the family program, many of the lists had duplicates For multiple answers, the ; had to be used repeatedly What would be more useful would be a list! Here is ONE solution (there are no doubt many others) sbrother(x, Y, [X, Y]) : parent(z, X), parent(z, Y), malediff(x, Y). siblings(q) : findall(z, sbrother(_,_,z), L), sort(l,q). How may we reason about this solution? Where would you begin? 8
9 Thinking in Prolog sbrother(x, Y, [X, Y]) : parent(z, X), parent(z, Y), malediff(x, Y). siblings(q) : findall(z, sbrother(_,_,z), L), sort(l,q). Work up from the individual predicates parent(z, X) is true if Z is a parent of X malediff(x, Y) is true if X is male and X!= Y sbrother(x, Y, [X, Y]) is true if X and Y have the same parent, X is male and X is not Y the 3 rd argument gives a list of pairs [X,Y] findall(z, pred( Z ), L) collects the values of Z in a list L sort(l, Q) sorts a list L into Q and removes duplicates Q is a sorted list of pairs [X, Y] siblings(q) gives the list Q as a sorted list (no duplicates set) 9
10 Thinking in Prolog pbrother(x, Y) sbrother(x, Y, [X, Y]) : parent(z, X), parent(z, Y), malediff(x, Y). : parent(z, X), parent(z, Y), malediff(x, Y). Note the difference in these 2 definitions pbrother will simply list each X and Y (using ; ) sbrother will also construct a list containing [X, Y] This will give [dick, sandra], [paul, lisa], [paul, mary] sbrother + ; will still give us too much information hence we can repackage this using findall/3, as siblings(q) : findall(z, sbrother(_,_,z), L), sort(l,q). This will give us [ [dick, sandra], [paul, lisa], [paul, mary] ] How does this work? 10
11 Thinking in Prolog pbrother(x, Y) : parent(z, X), parent(z, Y), malediff(x, Y). sbrother(x, Y, [X, Y]) : parent(z, X), parent(z, Y), malediff(x, Y). BUT refactor! Can we say qbrother extends pbrother? OO think! pbrother(x, Y) : parent(z, X), parent(z, Y), malediff(x, Y). qbrother(x, Y, [X, Y]) : pbrother(x, Y). Which is more efficient / readable then define qsiblings qsiblings(q) : findall(z, qbrother(_,_,z), L), sort(l,q). 11
12 Thinking in Prolog In the library database program we had listauthor(x) : book(_,author(x),_), display(x). listlastname(x,y) : book(_,author(x),_), last(x,y), display(y). lastnames : findall(y, listlastname(_,y), _). listauthor(x) gives [patrick, henry, winston] for the first book listlastname(x, Y) gives winston for the first book (in Y) lastnames gives a list of the last names What did we do? Simply insert last(x, Y) in listlastname Then display the last names. 12
13 Thinking in Prolog What have we done so far? Defined facts, rules and queries Prolog works using relations Explored Prolog essentially as a DataBase system Abstract Programming Languages such as Prolog and Lisp are more abstract They often provide abstract data structures + operations e.g. list You require a knowledge of built in predicates e.g. last(x, Y) You need to learn more about programming paradigms Imperative, OO, relational (Prolog), functional (Lisp, Haskell) You can then start to combine these ideas in whichever language you are using e.g. a functional style in C (see DSA!) Multi paradigm programming and languages e.g. Leda, F# 13
14 Thinking in Prolog accumulators reverse(xs, Ys) : reverse(xs, [ ], Ys). reverse([x Xs], Acc, Ys) : reverse(xs, [X Acc], Ys). reverse([ ], Ys, Ys). NB: 2 reverse reverse/2 reverse/3 reverse([a,b,c], R). Call: (6) reverse([a,b,c], _G378). reverse/2 Call: (7) reverse([a,b,c], [ ], _G378). reverse/3 Call: (8) reverse([b,c], [a], _G378). reverse/3 Call: (9) reverse([c], [b,a], _G378). reverse/3 Call: (10) reverse([ ], [c,b,a], _G378). reverse/3 Exit: (10) reverse([ ], [c,b,a], [c,b,a]). reverse/3 Exit: (9) reverse([c], [b,a], [c,b,a]). reverse/3 Exit: (8) reverse([b,c], [a], [c,b,a]). reverse/3 Exit: (7) reverse([a,b,c], [], [c,b,a]). reverse/3 Exit: (6) reverse([a,b,c], [c,b,a]). reverse/2 R = [c,b,a] did you spot the stack? 14
15 Thinking in Prolog insert([], It, [It]). insert([h T], It, [It, H T]) insert([h T], It, [H NewT]) : It. : It, insert(t, It, NewT). Very often, the last argument to a predicate is the result. insert can be read as insert(list, Value, NewList) or in English: add a value to a list to produce a new list. Points to note: a Prolog list is often expressed as [H T] (head/tail) the empty list is written as [ ] names are instantiated within a rule on both the left & right The order of the rules is important if the first definition fails then the second is tried and so on 15
16 Thinking in Prolog 1. insert([], It, [It]). 2. insert([h T], It, [It, H T]) : It. 3. insert([h T], It, [H NewT]) : It, insert(t, It, NewT). 1 the empty case the result is a list with one element, It 2 the value is added at the head of the new list 3 the value is added to the tail of the list result NewT the old head is the head of the new list You have already done this in C (in DSA) the recursive version Apply this knowledge to understanding the Prolog code 16
17 insert([ ], It, [It]). (1) insert([h T], It, [It, H T]) : It. (2) insert([h T], It, [H NewT]) : It, insert(t, It, NewT). (3) [trace]? insert([1,2,3], 4, Z). Call: (6) insert([1,2,3], 4, _G379) Call: (7) 1@>4? Fail: (7) 1@>4? Redo: (6) insert([1,2,3], 4, _G379) Call: (7) 1@<4? Exit: (7) 1@<4? Call: (7) insert([2,3], 4, _G437) Call: (8) 2@>4? Fail: (8) 2@>4? Redo: (7) insert([2,3], 4, _G437) Call: (8) 2@<4? Exit: (8) 2@<4? Call: (8) insert([3], 4, _G440) Call: (9) 3@>4? Fail: (9) 3@>4? Redo: (8) insert([3], 4, _G440) Call: (9) 3@<4? Exit: (9) 3@<4? Call: (9) insert([ ], 4, _G443) Exit: (9) insert([ ], 4, [4]) Exit: (8) insert([3], 4, [3,4]) Exit: (7) insert([2,3], 4, [2,3,4]) Exit: (6) insert([1,2,3], 4, [1,2,3,4]) Z = [1, 2, 3, 4]. 17
18 Thinking in Prolog (+ Haskell + C) insert([], It, [It]). insert([h T], It, [It, H T]) insert([h T], It, [H NewT]) : It. : It, insert(t, It, NewT). badd v [ ] = v: [ ] badd v (x:xs) v < x = v : (x:xs) otherwise = x : badd v xs static listref b_add(int v, listref L) { return is_empty(l)? create_e(v) : v < get_value(head(l))? cons(create_e(v), L) : cons(head(l), b_add(v, tail(l))); } 18
19 Thinking in Prolog (+ Haskell + C) insert([], It, [It]). insert([h T], It, [It, H T]) insert([h T], It, [H NewT]) : It. : It, insert(t, It, NewT). badd v [ ] = v: [ ] badd v (x:xs) v < x = v : (x:xs) otherwise = x : badd v xs static listref b_add(int v, listref L) { return is_empty(l)? create_e(v) : v < get_value(head(l))? cons(create_e(v), L) : cons(head(l), b_add(v, tail(l))); } 19
20 Language overview: Prolog How is Prolog structured? Prolog Syntax. Terms: Atoms, Numbers, Variables, Complex Terms (Structures) Constants: Atoms, Numbers Simple Terms: Constants, Variables Character Set: A Z, a z, 0 9, symbols (+,, *, ) Atom: string of characters (including underscore) sequence of characters in single quotes a string of special characters Numbers: integers (real, floating point) 20
21 Language overview: Prolog Variables: Complex Term: Rules: Body: string of upper/lower case letters, digits starts with _ or an upper case letter functor + sequence of arguments (X,Y, ) the functor (name) must be an atom arity the number of arguments e.g. last(x, Y) is last/2 where 2 =arity abc/2 and abc/3 are distinct predicates may be nested and/or recursive Head : Body. conjunctions (,) & disjunctions (;) (and/or) Facts: Head. (rule with empty body) Clauses: rules & facts (describe relations) Execution: satisfying GOALS 21
22 Language overview: Prolog Repetition: recursion Relations: work in both directions (more later) Design Patterns: clichés or common solutions Tail Recursion: often used cliché; optimised in Prolog Higher Order predicate which takes predicates as Programming: arguments e.g. findall(x, pred(..x..), L) Hashing: often implemented Modules: not always provided Restrictions: Negation: CWA Closed World Assumption mortal(aristotle). false see CWA 22
23 Language overview: Prolog Recursive Definitions (note Tail Recursion) is_bigger(x, Y) : bigger(x, Y). is_bigger(x, Y) : bigger(x, Z), is_bigger(z, Y). Lists: [a,b,c,d,e], [] (empty list), [H T] head/tail pattern member(x, [X _]). member(x, [_ T) : member(x, T). _ is the anonymous variable ( don t care ) append([], L, L). append([h T], L2, [H L3]) : append(t, L2, L3). this predicate requires thinking about! Try it! note how the predicate works in 2 directions 23
24 Language overview: Prolog Append example how does this work? append([], L, L). append([h T], L2, [H L3]) : append(t, L2, L3). append(l1, L2, L3) is best way to think about this predicate e.g. append([a,b,c], [d,e,f], X). will give X = [a,b,c,d,e,f]. Note that the second line definition 1. Splits L1 into its head and tail components (decomposition) 2. Leaves L2 unchanged 3. Adds the head of L1 to the List L3 (recomposition) 4. The recursive descent passes tail(l1); 5. The recursive ascent adds the heads in reverse 24
25 Language overview: Prolog append([], L, L). append([h T], L2, [H L3]) : append(t, L2, L3). 1. append([a,b,c], [d,e,f], L3) L1 not [] 2. append([b,c], [d,e,f], L3) L1 not [] 3. append([c], [d,e,f], L3) L1 not [] 4. append([], [d,e,f], L3) L1 is [], L3 = [d,e,f] 5. Return recursive calls! 6. append(_, _, c [d,e,f]) L3 is [c,d,e,f] 7. append(_, _, b [c,d,e,f]) L3 is [b,c,d,e,f] 8. append(_, _, a [b,c,d,e,f]) L3 is [a,b,c,d,e,f] 9. Finished! 25
26 Language overview: Prolog Rule (1): Rule (2): append( [], L, L). append( [H T], L2, [H L3]) : append(t, L2, L3). trace: append([a,b], [c,d], L3). Call: (6) append( [a,b], [c,d], _G382) ; Rule (2) Call: (7) append( [b], [c,d], _G443) ; Rule (2) Call: (8) append( [], [c,d], _G446) ; Rule (1) Exit: (8) append( [], [c,d], [c,d]) ; Rule (1) Exit: (7) append( [b], [c,d], [b,c,d]) ; Rule (2) Exit: (6) append( [a,b], [c,d], [a,b,c,d]) ; Rule (2) L3 = [a,b,c,d]. 26
27 Language overview: Prolog Rule (1): Rule (2): append([], L, L). append([h T], L2, [H L3]) : append(t, L2, L3). Deconstruction phase Rule(2) goes from Left to Right Rule (2): Call (1) append([ a b ], [ c, d ], [ a L3] ) : append( [ b ], [ c, d ], L3). Rule (2): Call (2) append([ b [ ] ], [ c, d ], [ b L3] ) : append([ ], [ c, d ], L3). Rule (1): Call (3) append([ ], [c,d], [c,d] ) i.e. List2 [c,d] is "copied" to L3 Reconstruction phase Rule(2) goes Right to Left i.e. L3 is "passed back" to the Left Hand Side Rule (1): Exit (3) returns append( [ ], [ c,d ], [ c,d ] ) i.e. List2 [c,d] is "copied back" to L3 Rule (2): Exit (2) returns append( [ b ], [ c, d ], [ b [ c, d ] ) i.e. append([ b ], [ c, d ], [ b, c, d ] ) List [ b, c, d] is "copied back" to L3 Rule (2): Exit (1) returns append[ a b ], [ c,d ], [ a [ b, c, d ] ) i.e. append( [ a,b ], [ c,d ], [a, b, c, d]) L3 is List [a, b, c, d] answer L3 is [a, b, c, d] 27
28 Language overview: Prolog Some list predicate examples (you would expect these!) length([a,b,c], X). X = 3. member(a, [a,b,c]). true. member(x, [a,b,c]). false. last([a,b,c], X). X = c. reverse([a,b,c], X). X = [c,b,a]. select(a, [a,b,c], X). X = [b,c]. append([a,b,c], [d,e,f], X). X = [a,b,c,d,e,f]. append(x, Y, [a,b,c,d]). X = [], Y = [a,b,c,d] ; X = [a], Y = [b,c,d] ; X = [a,b], Y = [c,d] ; X = [a,b,c], Y = [d] ; X = [a,b,c,d], Y = [] ; false. 28
29 Language overview: Prolog Some arithmetic predicate examples X is X is 4. X is +(2, 2). X is 4. Similarly for 2 2; 2*2; 2/2; mod(7,2); 3+2*4; (3+2)*4; etc. The usual arithmetic precedence rules apply Comparison: X op Y: op: <, =<, =:=, =/=, =>, > Notation: Prolog uses 2+2 as well as +(2, 2) The arithmetic operators are in fact predicates e.g. +/2, +/1 29
30 Language overview: Prolog Some predicates for terms NB: Prolog is a type free language as such these languages require predicates to test properties of constructs e.g. terms is the argument atom/1 an atom? integer/1 an integer? float/1 a floating point number? number/1 a number? atomic/1 a constant? var/1 nonvar/1 an uninstantiated variable? an instantiated variable OR another term not an uninstantiated variable 30
31 Language overview: Prolog More predicates for terms functor/3 name and arity of a predicate? functor(append(l1, L2, X), F, A). F = append. A = 3. arg/3 value of the nth argument? arg(2, append([a,b], [c,d],x), A). A = [c,d]. =.. /2 functor + args list? append([a,b], [c,d], X) =.. Z. Z = [append, [a,b],[c,d], X]. 31
32 Language overview: Prolog Some predicates for strings ( = list of ASCII codes)? atom_codes(donald, X). X = [100, 111, 110, 97, 108, 100].? atom_codes( Donald, X). X = [68, 111, 110, 97, 108, 100].? atom_codes(abc, X), append(x, X, L), atom_codes(n, L). X = [97, 98, 99]. L = [97, 98, 99, 97, 98, 99]. N = abcabc Similarly for numbers? number_codes(123, X). X = [49, 50, 51]. 32
33 Language overview: Prolog Some simple I/O predicates From the library database example testall : tell('library.out'), showall, told, halt. tell change the standard output (screen) to a file showall process information told switch the standard output back to the screen display(x) : tab(3), write(x), nl. From the lab 2 code see(file), get0(c), readword(c,w,c1), restsent(w,c1,ws), seen. see(file) switch input to file input get0(c) read a character from the file seen switch standard input back to the keyboard 33
34 Language overview: Prolog Some simple I/O predicates display/ copy a file process(file) : open(file, read, In), get_char(in, Char1), process_stream(char1, In), close(in). process_stream(end_of_file, _) :!. // cut stops process process_stream(char, In) : print(char), get_char(in, Char2), process_stream(char2, In). p1 : process('iotest.in'). p2 : tell('iotest.out'), process('iotest.in'), told. 34
35 Language overview: Prolog Some simple I/O predicates formatting Ref: SICStus Prolog User's Manual format/2 and format/3 provide an interface to the C s <stdio.h> function printf Examples are given in the above reference for those interested See / seen & tell / told come from the DEC 10 Prolog file I/O Again see the above reference The SICStus Prolog User's Manual will also give you an idea of the level of sophistication Prolog has reached Link: 35
36 Language overview: Prolog Summary what to focus on Thinking in Prolog & writing programs Use the built in predicates Write short predicates which may be further combined Be aware that relations work in 2 directions Use patterns such as tail recursion & [H T] Try to program abstractly think first what you want to do! Learning Prolog Start with short programs (course/web examples) Use web sources to find examples e.g. Rosetta Code Experiment and make mistakes!!! 36
Prolog. an introduction
Prolog an introduction Introducing Prolog Language Declarative defines relationships facts + rules + queries a database! based on logic (predicate calculus) Procedural C & Lisp History Developed as part
More informationCombining Lists & Built-in Predicates
Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 6 11/10/04 11/10/04 AIPP Lecture 6: Built-in Predicates 1 Collecting Results Last time we
More informationProlog. (Programming in Logic) Prepared by: Tanay Kumar Saha
Prolog (Programming in Logic) Prepared by: Tanay Kumar Saha Motivation for Learning Prolog Follow prolog news: http://www.swi-prolog.org/news SWI-Prolog Used For Satellite Anomaly Detection Fast cars and
More information3 Lists. List Operations (I)
3 Lists. List Operations (I) The list is the simplest yet the most useful Prolog structure. A list is a sequence of any number of objects. Example 3.1: L = [1, 2, 3], R = [a, b, c], T = [john, marry, tim,
More informationLecture 9: A closer look at terms
Lecture 9: A closer look at terms Theory Introduce the == predicate Take a closer look at term structure Introduce strings in Prolog Introduce operators Exercises Exercises of LPN: 9.1, 9.2, 9.3, 9.4,
More informationChapter 2. Declarative Semantics
Advanced Logic Programming https://sewiki.iai.uni-bonn.de/teaching/lectures/alp/2017/ Chapter 2. Declarative Semantics - Last updated: May 29, 2017 - How do we know what a goal / program means? Translation
More informationProblem Solving and Search
Problem Solving and Search Ulle Endriss Institute for Logic, Language and Computation University of Amsterdam [ http://www.illc.uva.nl/~ulle/teaching/pss/ ] Ulle Endriss 1 Table of Contents Lecture 1:
More informationTopic B: Backtracking and Lists
Topic B: Backtracking and Lists 1 Recommended Exercises and Readings From Programming in Prolog (5 th Ed.) Readings: Chapter 3 2 Searching for the Answer In order for a Prolog program to report the correct
More informationLecture 11: Feb. 10, 2016
CS323: AI (Hands on with Prolog) Spring 2016 Lecturer: K.R. Chowdhary Lecture 11: Feb. 10, 2016 : Professor of CS (VF) Disclaimer: These notes have not been subjected to the usual scrutiny reserved for
More informationDerived from PROgramming in LOGic (1972) Prolog and LISP - two most popular AI languages. Prolog programs based on predicate logic using Horn clauses
Prolog Programming Derived from PROgramming in LOGic (1972) Good at expressing logical relationships between concepts Prolog and LISP - two most popular AI languages Execution of a Prolog program is a
More informationCOP4020 Programming Languages. Logical programming with Prolog Prof. Xin Yuan
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan Topics Logic programming with Prolog COP4020 Spring 2013 2 Definitions: Prolog Terms Terms are symbolic expressions that are
More informationINTRODUCTION TO PROLOG
INTRODUCTION TO PROLOG PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2018 Dalhousie University 1/44 STRUCTURE OF A PROLOG PROGRAM Where, declaratively, Haskell expresses a computation as a system
More informationLecture 9. Exercises. Theory. Solutions to exercises LPN 8.1 & 8.2. Patrick Blackburn, Johan Bos & Kristina Striegnitz
Lecture 9 Exercises Solutions to exercises LPN 8.1 & 8.2 Theory Solution to Exercise 8.1 Suppose we add the noun ``men'' (which is plural) and the verb ``shoot''. Then we would want a DCG which says that
More informationLecture Notes An Introduction to Prolog Programming
Institute for Logic, Language and Computation Lecture Notes An Introduction to Prolog Programming Ulle Endriss Universiteit van Amsterdam c by Ulle Endriss, University of Amsterdam (Email: ulle.endriss@uva.nl)
More informationIntroduction. CSc 372. Comparative Programming Languages. 22 : Prolog Lists. Department of Computer Science University of Arizona.
CSc 372 Comparative Programming Languages 22 : Prolog Lists Introduction Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2011 Christian Collberg Christian Collberg Prolog
More informationPROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions
PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions 0 Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x 2 x {1...5}} The set {1,4,9,16,25}
More informationThe current topic: Prolog. Announcements. Meaning of a Prolog rule. Prolog syntax. Reminder: The deadline for Lab 2 re-mark requests is Friday.
The current topic: Prolog! Introduction! Object-oriented programming: Python! Functional programming: Scheme! Python GUI programming (Tkinter)! Types and values Logic programming: Prolog! Introduction
More informationIN112 Mathematical Logic
Institut Supérieur de l Aéronautique et de l Espace IN112 Mathematical Logic Lab session on Prolog Christophe Garion DMIA ISAE Christophe Garion IN112 IN112 Mathematical Logic 1/ 31 License CC BY-NC-SA
More informationIN112 Mathematical Logic
Institut Supérieur de l Aéronautique et de l Espace IN112 Mathematical Logic Lab session on Prolog Christophe Garion DMIA ISAE Christophe Garion IN112 IN112 Mathematical Logic 1/ 31 License CC BY-NC-SA
More informationCPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia
CPL 2016, week 10 Clojure functional core Oleg Batrashev Institute of Computer Science, Tartu, Estonia April 11, 2016 Overview Today Clojure language core Next weeks Immutable data structures Clojure simple
More informationOrdered structures. Dr. C. Constantinides. Department of Computer Science and Software Engineering Concordia University Montreal, Canada
Ordered structures Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada January 10, 2017 1/19 Introduction Ordered structures include tuples,
More information10. Logic Programming With Prolog
Copyright (C) R.A. van Engelen, FSU Department of Computer Science, 2000 10. Logic Programming With Overview Logic Programming Logic Programming Logic programming is a form of declarative programming A
More informationThe object level in Prolog. Meta-level predicates and operators. Contents. The flow of computation. The meta level in Prolog
Lecture 8 Meta-level predicates and operators Contents Object level vs. meta level Controlling flow of computation Checking and dismantling expressions Comparison operators The object level in Prolog Prolog
More informationA general introduction to Functional Programming using Haskell
A general introduction to Functional Programming using Haskell Matteo Rossi Dipartimento di Elettronica e Informazione Politecnico di Milano rossi@elet.polimi.it 1 Functional programming in a nutshell
More informationPROLOG. First simple Prolog Program. Basic elements of Prolog. Clause. NSSICT/CH/Nov., 2012.
PROLOG Prolog is a programming language for symbolic, n-numeric computations. It is specially well suited for solving problems that involve objects and relation between objects. First simple Prolog Program
More informationDSA: Programming. Cheap Tricks for the (abstract) programmer!
DSA: Programming Cheap Tricks for the (abstract) programmer! Programming Towards a more abstract style Functions should be simple A small number of steps & 1 operation only 10-20 lines is a big function
More informationPROgramming in LOGic PROLOG Recursion, Lists & Predicates
PROgramming in LOGic PROLOG Recursion, Lists & Predicates CSC9Y4 1 Recursion Recursion in Prolog means placing in the body of a rule a call to the predicate which occurs in the head of the rule. Here is
More informationScheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax
Scheme Tutorial Introduction Scheme is an imperative language with a functional core. The functional core is based on the lambda calculus. In this chapter only the functional core and some simple I/O is
More informationManipulating atomics and clauses
Manipulating atomics and clauses This lecture revisits recursion, showing how to read input. Then it introduces type testing predicates, atomic term processing term creation and decomposition and clause
More informationCS 360: Programming Languages Lecture 10: Logic Programming with Prolog
CS 360: Programming Languages Lecture 10: Logic Programming with Prolog Geoffrey Mainland Drexel University Section 1 Administrivia Midterm Tuesday Midterm is Tuesday, February 14! Study guide is on the
More informationPROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions
PROGRAMMING IN HASKELL CS-205 - Chapter 6 - Recursive Functions 0 Introduction As we have seen, many functions can naturally be defined in terms of other functions. factorial :: Int Int factorial n product
More informationProlog Introduction. Gunnar Gotshalks PI-1
Prolog Introduction PI-1 Physical Symbol System Hypothesis A physical symbol system has the necessary and sufficient means for general intelligent action. Allen Newell and Herbert A. Simon PI-2 Physical
More informationChapter 16. Logic Programming Languages
Chapter 16 Logic Programming Languages Chapter 16 Topics Introduction A Brief Introduction to Predicate Calculus Predicate Calculus and Proving Theorems An Overview of Logic Programming The Origins of
More informationA Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1
A Second Look At ML Chapter Seven Modern Programming Languages, 2nd ed. 1 Outline Patterns Local variable definitions A sorting example Chapter Seven Modern Programming Languages, 2nd ed. 2 Two Patterns
More informationIntroduction 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 informationProlog. Intro to Logic Programming
Prolog Logic programming (declarative) Goals and subgoals Prolog Syntax Database example rule order, subgoal order, argument invertibility, backtracking model of execution, negation by failure, variables
More informationIntroduction to Prolog Paper Refs. Prolog tutor. Julian Verdurmen. Seminar Softw. tech. for teaching and learning. September 30, 2009
Seminar Softw. tech. for teaching and learning September 30, 2009 Outline 1 Introduction to Prolog Basics Simple example 2 Basics Simple example Outline 1 Introduction to Prolog Basics Simple example 2
More informationProlog-2 nd Lecture. Prolog Predicate - Box Model
Prolog-2 nd Lecture Tracing in Prolog Procedural interpretation of execution Box model of Prolog predicate rule How to follow a Prolog trace? Trees in Prolog use nested terms Unification Informally Formal
More informationLecture 6: More Lists
Lecture 6: More Lists Theory Define append/3, a predicate for concatenating two lists, and illustrate what can be done with it Discuss two ways of reversing a list A naïve way using append/3 A more efficient
More informationThis lecture covers: Prolog s execution strategy explained more precisely. Revision of the elementary Prolog data types
This lecture covers: Prolog s execution strategy explained more precisely The execution strategy has been presented as simply placing all matching clauses onto the stack. In fact, Prolog is slightly more
More informationLogic Programming: Lecture 1
Logic Programming: Lecture 1 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in PLC, 3 April 2017 Logic programming Programming with relations Variables Names starting with a capital letter
More informationLecture 16: Logic Programming in Prolog
Lecture 16: Logic Programming in Prolog COMP 524 Programming Language Concepts Stephen Olivier March 26, 2009 Based on slides by A. Block, notes by N. Fisher, F. Hernandez-Campos, and D. Stotts Goal of
More informationIntroduction to Logic Programming in Prolog 1 / 39
Introduction to Logic Programming in Prolog 1 / 39 Outline Programming paradigms Logic programming basics Introduction to Prolog Predicates, queries, and rules Understanding the query engine Goal search
More informationCSc 372 Comparative Programming Languages
CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Christian Collberg collberg+372@gmail.com Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg
More informationIntroduction to Programming, Aug-Dec 2006
Introduction to Programming, Aug-Dec 2006 Lecture 3, Friday 11 Aug 2006 Lists... We can implicitly decompose a list into its head and tail by providing a pattern with two variables to denote the two components
More informationCSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona
1/43 CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg Functions over Lists
More informationIKI30820 Logic Programming Programming Style and Techniques Slide 09
IKI30820 Logic Programming Programming Style and Techniques Slide 09 Ari Saptawijaya (slides) Adila A. Krisnadhi (L A T E Xadaptation) Fakultas Ilmu Komputer Universitas Indonesia 2009/2010 Semester Gasal
More informationTail recursion. Decision. Assignment. Iteration
Computer Programming Tail recursion. Decision. Assignment. Iteration Marius Minea marius@cs.upt.ro 7 October 2014 Two ways of writing recursion unsigned max(unsigned a, unsigned b) { return a > b? a :
More informationCS 381: Programming Language Fundamentals
CS 381: Programming Language Fundamentals Summer 2017 Introduction to Logic Programming in Prolog Aug 2, 2017 Outline Programming paradigms Logic programming basics Introduction to Prolog Predicates, queries
More informationExercises on the Fundamentals of Prolog
1 Introduction Exercises on the Fundamentals of Prolog These exercises are intended to help reinforce material taught in the lectures of CIS335 course in Prolog. They do not contribute any marks to the
More informationWan Hussain Wan Ishak
September 1 st Session 2014/2015 (A141) Wan Hussain Wan Ishak School of Computing UUM College of Arts and Sciences Universiti Utara Malaysia (P) 04-9285150 (E) hussain@uum.edu.my (U) http://wanhussain.com
More informationCSCE 314 Programming Languages
CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee 1 Higher-order Functions A function is called higher-order if it takes a function as an argument or returns a function as
More informationArithmetic Terms are Symbolic
Arithmetic Terms are Symbolic Evaluation of an arithmetic term into a numeric value must be forced. That is, 1+2 is an infix representation of the relation +(1,2). This term is not an integer! Therefore?-
More informationResearch Report AI A Numerical Equation Solver in Prolog Michael A. Covington Artificial Intelligence Programs The University of Georgia
Research Report AI 1989 02 A Numerical Equation Solver in Prolog Michael A. Covington Artificial Intelligence Programs The University of Georgia Athens, Georgia 30602 U.S.A. A Numerical Equation Solver
More information3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)
Structures Programming in C++ Sequential Branching Repeating Loops (Repetition) 2 1 Loops Repetition is referred to the ability of repeating a statement or a set of statements as many times this is necessary.
More informationModern Programming Languages. Lecture LISP Programming Language An Introduction
Modern Programming Languages Lecture 18-21 LISP Programming Language An Introduction 72 Functional Programming Paradigm and LISP Functional programming is a style of programming that emphasizes the evaluation
More informationRecursion, Structures, and Lists
Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 4 04/10/04 30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists 1 The central ideas of Prolog
More informationProlog (cont d) Remark. Using multiple clauses. Intelligent Systems and HCI D7023E
Intelligent Systems and HCI D703E Lecture : More Prolog Paweł Pietrzak Prolog (cont d) 1 Remark The recent version of SWI- Prolog displays true and false rather than and no Using multiple clauses Different
More informationCS 360: Programming Languages Lecture 10: Introduction to Haskell
CS 360: Programming Languages Lecture 10: Introduction to Haskell Geoffrey Mainland Drexel University Thursday, February 5, 2015 Adapted from Brent Yorgey s course Introduction to Haskell. Section 1 Administrivia
More informationHaske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell
Haskell An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dk The most popular purely functional, lazy programming language Functional programming language : a program
More informationShell CSCE 314 TAMU. Higher Order Functions
1 CSCE 314: Programming Languages Dr. Dylan Shell Higher Order Functions 2 Higher-order Functions A function is called higher-order if it takes a function as an argument or returns a function as a result.
More informationLogic Programming Languages
Logic Programming Languages Introduction Logic programming languages, sometimes called declarative programming languages Express programs in a form of symbolic logic Use a logical inferencing process to
More informationPROLOG PROgramming in LOGic
PROLOG PROgramming in LOGic 1 Knowledge-Based Information Systems (Relational) database systems are very efficient in the handling of data. Information is data together with a suitable interpretation.
More informationFirst-Order Logic (FOL)
First-Order Logic (FOL) FOL consists of the following parts: Objects/terms Quantified variables Predicates Logical connectives Implication Objects/Terms FOL is a formal system that allows us to reason
More informationChapter 6. Predefined Predicates
Advanced Logic Programming https://sewiki.iai.uni-bonn.de/teaching/lectures/alp/2017/ Chapter 6. Predefined Predicates Updated: 12 June, 2017 Reorganized entire chapter Input and output Exception handling
More informationAdvanced Prolog Programming
1 Introduction CIS335, Assignment 4 Advanced Prolog Programming Geraint Wiggins November 12, 2004 This practical is the second self-assessed exercise for MSc students on the Prolog module. It is intended
More informationAn introduction introduction to functional functional programming programming using usin Haskell
An introduction to functional programming using Haskell Anders Møller amoeller@cs.au.dkau Haskell The most popular p purely functional, lazy programming g language Functional programming language : a program
More informationFunctional 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 informationIntroduction. chapter Functions
chapter 1 Introduction In this chapter we set the stage for the rest of the book. We start by reviewing the notion of a function, then introduce the concept of functional programming, summarise the main
More informationProlog - 3 Prolog search trees + traces
Prolog - 3 Prolog search trees + traces 1 Review member(a, [A B]). member(a, [B C]) :- member (A,C). goal-oriented semantics: can get value assignment for goal member(a,[b C]) by showing truth of subgoal
More informationsimplicity 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 informationIntroduction to Prolog
Introduction to Prolog David Woods dwoods@scss.tcd.ie Week 3 - HT Declarative Logic The Prolog programming language is, at its theoretical core, a declarative language. This is unlike more commonly used
More informationChapter 16. Logic Programming Languages ISBN
Chapter 16 Logic Programming Languages ISBN 0-321-49362-1 Chapter 16 Topics Introduction A Brief Introduction to Predicate Calculus Predicate Calculus and Proving Theorems An Overview of Logic Programming
More informationProgramming Language Concepts: Lecture 22
Programming Language Concepts: Lecture 22 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 22, 15 April 2009 Logic programming
More informationLING/C SC/PSYC 438/538. Lecture 20 Sandiway Fong
LING/C SC/PSYC 438/538 Lecture 20 Sandiway Fong Today's Topics SWI-Prolog installed? We will start to write grammars today Quick Homework 8 SWI Prolog Cheatsheet At the prompt?- 1. halt. 2. listing. listing(name).
More informationLISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.
LISP Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits. From one perspective, sequences of bits can be interpreted as a code for ordinary decimal digits,
More informationCAP 5602 Summer, Lesson 4: Loops. The topics 1. the cut and some of its uses 2. the while loop 3. the do until loop
CAP 5602 Summer, 2011 Lesson 4: Loops The topics 1. the cut and some of its uses 2. the while loop 3. the do until loop 1. The cut The cut (!) is a way of controlling backtracking. If a rule contains a
More informationLogic Programming. CITS 3242 Programming Paradigms. Topic 16: Part IV: Advanced Topics
CITS 3242 Programming Paradigms Part IV: Advanced Topics Topic 16: Logic Programming Logic Programming is a paradigm for programming by declaring facts and rules. Programs are executed by querying whether
More informationShell CSCE 314 TAMU. Haskell Functions
1 CSCE 314: Programming Languages Dr. Dylan Shell Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions can
More informationProgramming Paradigms
PP 2017/18 Unit 6 Prolog Basics 1/42 Programming Paradigms Unit 6 Prolog Basics J. Gamper Free University of Bozen-Bolzano Faculty of Computer Science IDSE PP 2017/18 Unit 6 Prolog Basics 2/42 Outline
More informationChapter 16. Logic Programming Languages ISBN
Chapter 16 Logic Programming Languages ISBN 0-321-49362-1 Chapter 16 Topics Introduction A Brief Introduction to Predicate Calculus Predicate Calculus and Proving Theorems An Overview of Logic Programming
More informationOverview. Declarative Languages. operation of del. Deleting an element from a list. functions using del. inserting elements with del
Overview Declarative Languages D7012E: Arithmetic and Backtracking Fredrik Bengtsson Some standard functions Operators Arithmetic The eight queens problem Controlling backtracking cut Deleting an element
More informationLogic Programming. Efficiency Issues. Temur Kutsia
Logic Programming Efficiency Issues Temur Kutsia Research Institute for Symbolic Computation Johannes Kepler University of Linz, Austria kutsia@risc.uni-linz.ac.at Efficiency Issues in Prolog Narrow the
More informationThe Prolog to Mercury transition guide
The Prolog to Mercury transition guide Version 14.01.1 Thomas Conway Zoltan Somogyi Fergus Henderson Copyright c 1995 2014 The University of Melbourne. Permission is granted to make and distribute verbatim
More informationLecture Overview Prolog 1. Introduction Interaction Terms 2. Clauses and predicates Clauses Predicates Variables 3.
1 Lecture Overview Prolog 1. Introduction Interaction Terms 2. Clauses and predicates Clauses Predicates Variables 3. Satisfying goals 2 Prolog A standard free Prolog can be downloaded from http://www.swi-prolog.org
More informationLogic Programming (PLP 11) Prolog Imperative Control Flow: Backtracking, Cut, Fail, Not
Logic Programming (PLP 11) Prolog Imperative Control Flow: Backtracking, Cut, Fail, Not Carlos Varela Rennselaer Polytechnic Institute September 2, 2014 C. Varela 1 Backtracking Forward chaining goes from
More informationPredicate Calculus. Problems? Syntax. Atomic Sentences. Complex Sentences. Truth
Problems? What kinds of problems exist for propositional logic? Predicate Calculus A way to access the components of an individual assertion Predicate Calculus: used extensively in many AI programs, especially
More informationsimplicity 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 informationLists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca
Lists Adrian Groza Department of Computer Science Technical University of Cluj-Napoca Recall... Parameter evaluation Call-by-value Call-by-name Call-by-need Functions Infix operators Local declarations,
More informationSymbolic Computation Example Programming Exercises for Prolog
2001 July 16 3401 Prolog Programming Exercises Page 1 of 8 Symbolic Computation Example Programming Exercises for Prolog 1. Write a Prolog predicate countbt(tree, Count) to count the number of des in a
More informationHaskell Types, Classes, and Functions, Currying, and Polymorphism
1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Types, Classes, and Functions, Currying, and Polymorphism 2 Types A type is a collection of related values. For example, Bool contains the
More informationLogical reasoning systems
Logical reasoning systems Theorem provers and logic programming languages Production systems Frame systems and semantic networks Description logic systems CS 561, Session 19 1 Logical reasoning systems
More informationProgramming Paradigms
PP 2016/17 Unit 16 Erlang Modules, Functions and Control Structures 1/31 Programming Paradigms Unit 16 Erlang Modules, Functions and Control Structures J. Gamper Free University of Bozen-Bolzano Faculty
More informationFunctional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute
Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute Module # 02 Lecture - 03 Characters and Strings So, let us turn our attention to a data type we have
More informationProcessadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)
Processadors de Llenguatge II Functional Paradigm Pratt A.7 Robert Harper s SML tutorial (Sec II) Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra Paradigm Shift Imperative Paradigm State Machine
More informationOperators (2A) Young Won Lim 10/2/13
Operators (2A) Copyright (c) 2013 Young W. Lim. 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
More informationExample Programming Exercises for Prolog
2007 February 3 3401 Example Prolog Programs Page 1 of 3 Example Programming Exercises for Prolog 1. Write a Prolog predicate countbt(tree, Count) to count the number of des in a binary tree that have
More informationFunctional Programming in Haskell Part I : Basics
Functional Programming in Haskell Part I : Basics Madhavan Mukund Chennai Mathematical Institute 92 G N Chetty Rd, Chennai 600 017, India madhavan@cmi.ac.in http://www.cmi.ac.in/ madhavan Madras Christian
More informationProlog Core Concepts and Notation
Prolog Core Concepts and Notation Yves Lespérance Adapted from Peter Roosen-Runge Readings: C & M Ch 1, 2, 3.1-3.3, 8 1 declarative/logic programming idea: write a program that is a logical theory about
More informationRecursion & Iteration
Recursion & Iteration York University Department of Computer Science and Engineering 1 Overview Recursion Examples Iteration Examples Iteration vs. Recursion Example [ref.: Chap 5,6 Wilensky] 2 Recursion
More information