CPT114 CPT104 Introduction to Prolog

March 29, 2018 | Author: kim1029 | Category: Mathematical Logic, Variable (Mathematics), Axiom, First Order Logic, Computer Programming


Comments



Description

CPT114/CPT104 Logic and Applications/ Introduction to Logic and AbstractionProf Madya Rosalina Abdul Salam School of Computer Sciences, USM [email protected] Room: 721, Tel: 04-6532486 5.2 Introduction to Prolog The relation between Prolog and logic Defining relations by facts Defining relations by rules Recursive rules How Prolog answers questions Declarative and procedural meaning of programs Reference: Bratko(2001) CPT114/CPT104 Shahida S. (2008) 2 Win-Prolog Programming in Logic (Prolog) Many different compilers     WinProlog Amzi Swi Others«.. For this course please use win-prolog. CPT114/CPT104 Shahida S. (2008) 3 co.Win-Prolog Website: http://www. (2008) 4 .lpa.uk/ Download free trial version There are manuals available CPT114/CPT104 Shahida S. WinProlog CPT114/CPT104 Shahida S. (2008) 5 . (2008) 6 .Open new file and start typing CPT114/CPT104 Shahida S. Load a file CPT114/CPT104 Shahida S. (2008) 7 . . (2008) 8 .pl file CPT114/CPT104 Shahida S. (2008) 9 .Using the prompt CPT114/CPT104 Shahida S. quantifiers are not explicitly written) Logic First-order predicate logic (predicates and quantification) Horn clauses (clauses that have at most one positive literal) Procedural meaning Matching using ³=³ Resolution principle for mechanical theorem proving Unification: substitution instance CPT114/CPT104 Shahida S.The Relation Between Prolog and Logic Prolog is related to mathematical logic. so its syntax and meaning can be specified most concisely with references to logic. Prolog Clause form (conjunctive normal form. (2008) 10 . nonnumeric computation. It is suitable for solving problems that involve objects and relations between objects. bob). Arguments: concrete object/ constant (atom) / general object (variable) initial lower case Shahida S. (2008) 11 CPT114/CPT104 . parent(tom.Defining Relations by Facts Prolog: a programming language for symbolic. A relation: The set of all its instances. parent(tom. Name of relation A clause terminates with a full stop. Below is a particular instance (relationship) of the parent relation. X). A sequence of goals means conjunction of the goals: X is a parent of Ann. An answer can be positive (satisfiable/ succeeded) or negative (unsatisfiable/ failed). parent(X. If several answers satisfy the question then Prolog will find all. parent(X. CPT114/CPT104 Shahida S. and X is a parent of Pat. Queries/questions to the system consists of one or more goals to be satisfied e.Prolog System Define a relation by stating n-tuples of objects that satisfy the relation. ann). pat).g. User can query the Prolog system about relations defined in the program. (2008) 12 . bob). A particular instance of the parent relation e. bob). parent(pat. ann). parent(tom. CPT114/CPT104 Shahida S. (2008) 13 . jim). parent(bob. There are 6 clauses. Each clause declares one fact about the parent relation. parent(bob. bob) is also called relationships. liz). parent(tom.Define a Relation for a family parent(pam. Relation: the set of all instances.g. parent(tom. pat). Y).parent(Y. jim).parent(X. Will the answer be the same? 14 bob liz Prolog answer Prolog query ann pat jim Answer will be the same although we change the order of the 2 requirements as the logical meaning are same. jim).Example: A Family Tree pam tom Prolog system Type questions via terminal: ?. yes ?. Y). Y = pat X = bob ?. CPT114/CPT104 Shahida S.parent(bob. X = tom ?. liz). parent(Y.parent(X. parent(X. (2008) . pat). liz). no jim ?.Exercise pam tom Who are Pam¶s grandchildren? ?.parent(X. (2008) 15 bob liz ann pat . X = bob Type semicolon Y = ann. X). jim). X = pat CPT114/CPT104 Shahida S. Y).parent(ann. for more X = bob solutions. parent(X.parent(X. type Y = pat enter to stop. Do Bob and Liz have the same parent? ?. X = tom ?. X).parent(pam. bob). parent(X. g. an alternative piece of program: gender(pam. add the following facts to the program: female(pam). CPT114/CPT104 Shahida S. (2008) 16 . male(jim). feminine). E. masculine). gender(tom.g.Defining Relations by Rules Types of relations: Unary (one-place): can be used to declare simple yes/no properties of objects. Binary: defines relations between pairs of objects. E. X) :.) Difference between facts and rules: The clause below is a fact that is unconditionally true. liz). (2008) A condition part/ body of a clause 17 . E.g.parent(X. parent(tom. A conclusion part/ head of a clause CPT114/CPT104 Shahida S. Y). Rules specify things that are true if some condition is satisfied. child(Y.Defining Relations by Rules (cont. parent(X. tom) Replaced with: parent(tom. Y). Prolog will answer: yes 18 bob liz pat jim CPT114/CPT104 Shahida S. liz) The conclusion part is true.child(liz. liz). So we get. Thus. Prolog tries to find whether the condition is true.Example pam tom Ask whether Liz is a child of Tom: ?. initial goal: child(liz. (2008) . X) :. tom). child(liz. tom) :.parent(tom. Instantiate variable X and Y: child(Y. hasachild(X) :.parent(X. CPT114/CPT104 Shahida S. X has a child if there is some Y such that X is a parent of Y. For all X and Y. if X is a parent of Y then X has a child. hasachild(X) :.Exercise Translate the following statements into Prolog rules: For all X. Y). (2008) 19 . Y).parent(X. A comma between the conditions indicates the conjunction of the condition. X is the mother of Y if X is a parent of Y and X is a female mother(X. female(X). (2008) 20 . Y) :. Y).parent(X.Another Exercise Translate the following statements into Prolog rules: For all X and Y. Thus both conditions must be true. CPT114/CPT104 Shahida S. CPT114/CPT104 X X is an indirect predecessor of Z Y1 predecessor parent Y2 parent Z Shahida S. Z).Recursive Rules: Predecessor Relation Some X is an indirect predecessor of some Z if there is a partnership chain of people between X and Z. (2008) predecessor predecessor(X. Z) :parent(X. parent(Y1. Z). Y1). Z) :parent(X. 21 . X is a direct predecessor of Z parent X parent Z predecessor(X. Y2). parent(Y2. (2008) 22 . assuming that the relations in the program are true or  Demonstrate that the goal logically follows from the facts and rules in the program. CPT114/CPT104 Shahida S.  Demonstrate that the goal is true. If not found. If the questions contain variables. Prolog¶s answer to the question will be µno¶.How Prolog Answers Questions Prolog tries to satisfy all goals when answering a question. The particular instantiation of variables to these objects is displayed to the user. Prolog has to find the particular objects (in place of variables) for which the goals are satisfied. CPT114/CPT104 Shahida S. and the user¶s question as conjectured theorem i. to demonstrate that it can be logically derived from the axioms. guessed/assumed theorem. A theorem that logically follows from these two axioms: Socrates is imperfect. (2008) 23 . The first axiom above can be rewritten as: For all X. Then it tries to prove this theorem i. Socrates is a man.e. Let the axioms be: All men are imperfect.Example Prolog accepts facts and rules as a set of axioms. if X is a man then X is imperfect.e. Example (cont.) Translation into Prolog: imperfect(X) :- man(X). man(socrates). ?- imperfect(socrates). yes %All men are imperfect %Socrates is a man %Socrates is imperfect? CPT114/CPT104 Shahida S. (2008) 24 Exercise: Recall Predicate Logic All actors are stars. axiom Will Smith is an actor. axiom Therefore, Will Smith is a star. theorem Translate into Prolog star(X) :- actor(X). actor(willSmith). ?- star(willSmith). yes %All actors are stars %Will Smith is an actor % Will Smith is a star? CPT114/CPT104 Shahida S. (2008) 25 Declarative & Procedural Meaning of Programs Two levels of meaning of Prolog programs:  Declarative meaning: relations defined by the program that determines what will be the output of the program.  Procedural meaning: determines how this output is obtained i.e. how the relations are actually evaluated by the Prolog system. Programmers can concentrate mainly on:  declarative meaning and  reduce/avoid being distracted by execution details. CPT114/CPT104 Shahida S. (2008) 26 5. (2008) 27 .3 Syntax & Meaning of Prolog Programs Data objects Matching Declarative meaning of Prolog programs Procedural meaning Order of clauses and goals CPT114/CPT104 Shahida S. Data Objects Prolog system recognizes the type of an object in the program by its syntactic form. (2008) 28 . Types of Prolog objects:  Atoms and numbers  Variables  Structures data objects simple objects structures constants atoms numbers variables CPT114/CPT104 Shahida S. µBob¶ µBob_John¶ Numbers include integer numbers and real numbers. 1. b. Z  Lower-case letters a. «. B. digits and underscore character starting with lower case letter bob x_30 bob_john  String of special characters <==> « ::=  String of characters enclosed in single quotes. «.Atoms and Numbers May have strings of the following characters:  Upper-case letters A. 2.* / < > : . CPT114/CPT104 Shahida S. + . & _ ~ Can be constructed in 3 ways:  String of letters. 9  Special characters e.g. z  Digits 0. (2008) 29 . «. hasachild(X) :. (2008) 30 . then it signifies two different variables. digits and underscore characters that start with upper-case letter or underscore character.g. The lexical scope of variable names is one clause e.parent(X. X Mark2 _xy MarkList Anonymous variable: can be used when a variable appears in a clause once only. the name X1 occurs in two clauses. But each occurrence of X1 within the same clause means the same variable.Variables Variables: strings of letters. _). CPT114/CPT104 Shahida S. Structures Also known as structured objects that have several components. (2008) variable CPT114/CPT104 31 . year. 2007) functor Arguments: can be constants. the date can be viewed as structure with 3 components: day. E. june. 2007) date(Day. june. variables. or other structures Shahida S. month. Functor: combine the components into a single object. date(1.g. The most important operation on terms is matching.Matching Terms can be used to represent complex data objects. (2008) 32 . CPT114/CPT104 Shahida S. we say that they match if:  They are identical. or  The variables in both terms can be instantiated to objects in such a way that after the substitution of variables by these objects the terms become identical. Given 2 terms. CPT114/CPT104 Shahida S. date(D1. 2007). date(D1. (2008) 33 . 1400).Example The 2 terms below match: date(D. One instantiation that makes both terms identical is:  D is instantiated to D1  J is instantiated to june  Y1 is instantiated to 2007 The terms below do not match: (why?) date(D. J1. J. 2007). june. J. Y1). D) = +(E. 2) D = 2. what are resulting instantiations of variables? point(A. E) = point(X1. B = 12 . B) = point(10. 3) = 5 no +(2. Z) no plus(2.Exercise Will the following matching operations succeed or fail? If they succeed. (2008) 34 A = 10. 12) point(D. Y. E = 2 CPT114/CPT104 Shahida S. To satisfy P. From Q and R follows P.Declarative Meaning of Prolog Programs Consider a clause: P :. Procedural reading of the clause: To solve problem P. R Declarative reading of the clause: P is true if Q and R are true. first satisfy Q and then R. (2008) 35 . CPT114/CPT104 Shahida S. first solve the subproblem Q and then the subproblem R.Q. Declarative Meaning (cont. all goals must be true %disjunction of goals. A question is a list of goals separated by commas. P :. CPT114/CPT104 %conjunction of goals. R. R.) Given a program and a goal G.Q. any one has to be true Shahida S. or logically follows from the program) if and only if: 1) there is a clause C in the program such that 2) there is a clause instance I of C such that a) the head of I is identical to G.Q. the declarative meaning says: A goal G is true (that is. (2008) 36 . P :. satisfiable. A list of goals is true if all the goals in the list are true for the same instantiation of variables. and b) all the goals in the body of I are true. Procedural Meaning Specifies how Prolog answers questions or try to satisfy a list of goals. (2008) 37 . r ra success/failure indicat r al list execute instantiati n f variables CPT114/CPT104 Shahida S. It is a procedure for executing a list of goals with respect to a given program. goal list shrinks to: big(cat).brown(Z). big(elephant). Thus backtrack to step (3) and undo the instantiation X = cat. 3. big(X). big(X). Backtrack to step (2) and continue below clause7. Successful termination: X = bear. Clause7 found: dark(Z) :. Scan to match brown(X). The goal lists shrinks to empty. Not found. found brown(bear). %clause1 %clause2 %clause3 %clause4 %clause5 %clause6 EXECUTION TRACE 1. Continue scanning below clause5. Clause5 found: black(cat). No clause found. 2.%clause8 %anything brown is dark QUESTION ?. Replace by instantiate body: black(X).black(Z). black(cat). Initial goal list: dark(X). The goal list is: black(X). (2008) 38 . dark(Z) :. brown(bear). %clause7 %anything black is dark dark(Z) :. gray(elephant). 4. 5. small(cat). 6. Clause8 found. Scan program top to bottom. %who is dark and big? CPT114/CPT104 Shahida S.black(Z). big(X). big(X).dark(X). big(X). Scan to find a match with black(X). Scan and find clause big(bear). Replace 1st goal: brown(X).brown(Z). The goal list shrinks to big(bear). find a clause whose head matches the 1st goal dark(X). dark(Z) :. Scan for the goal big(cat). This clause has no body.Example PROGRAM big(bear). Concentrate on declarative aspects of the problem. ?. if it fails procedurally try rearrange the clauses and goals into a suitable order. p :.g.g.predecessor(X. test resulting program. predecessor(X.  Not suitable ordering of clauses and goals: Prolog will not satisfy goal because it chooses a wrong path. Z) :. Z).p. procedurally wrong e.Order of Clauses and Goals Danger of indefinite loop:  Prolog enters infinite loop without any progress e. CPT114/CPT104 Shahida S. (2008) 39 .p. More techniques needed to deal with path finding. problem solving and search. Program variations through reordering of clauses and goals: the rule is µtry simplest things 1st¶ Combining declarative and procedural views: declaratively correct. %version1: swap clauses of original one pred2(X. Z) :. pred3(X.parent(X. no. Z).pred3(X.parent(X. Z). Z).pred3(liz.pred1(tom. Infinite sequence of pred4(X.parent(X. pat). Y).parent(X. parent(Y. Y). Z).Exercise PROGRAM %original version pred1(X.parent(X. (yes.pred4(tom. pred2(X. yes ?. pred1(X. Z) :. Z) :. Z) :. yes ?.pred4(X. pat). stack overflow recursive call. Y). Z) :. Can¶t stack overflow %version3: swap goals & clauses of original one find answer ?. pred1(Y. Z) :.pred3(tom. Z). Z) :. fail?) ?. CPT114/CPT104 Shahida S. pam bob tom liz pat jim Determine whether Tom is a predecessor of Pat using predecessor relation. pred4(X.pred2(tom. Z) :. yes ?. parent(Y. Z). Z). jim). Z). Y). pat).parent(X. %version2: swap goals in 2nd clause of original one pred3(X. (2008) 40 . pred2(Y. pat). 5. Operators. Arithmetic Representations of lists Operations on lists Operator notation Arithmetic CPT114/CPT104 Shahida S. (2008) 41 .4 Lists. Representation of Lists The list is a simple data structure widely used in nonnumeric programming. Tail) [nicol. . badminton] All Prolog objects are trees including lists (binary trees) that can be either empty [ ] or non-empty consists of items: head and tail. badminton] head: can be any Prolog objects CPT114/CPT104 Shahida S. hafiz. squash. (2008) tail: must be a list 42 .(Head. A list is a sequence of any number of items written in Prolog as: [nicol. squash. hafiz. . tom. (2008) 43 . L = [ann.Example1 The following conversation with Prolog: ?. music]. Hobbies2]. Hobbies1. food]. food] L = [ann. Hobbies1 = [tennis. Hobbies2 = [skiing.(tennis. tom. music] Hobbies2 = [skiing.Hobbies1 = . [ ] ) ). [skiing. [tennis. food] ] CPT114/CPT104 Shahida S.(music. c] L = . c | [ ] ] Special notation for lists: [Item1. Item2. c] = [a | [b. b. b. «| Others] CPT114/CPT104 Shahida S. b. c] ] = [a. «] or [Head | Tail] or [Item1.(a. Tail) L = [a | Tail] [a. Item2.Example2 Treat the whole tail as a single object: L = [a. b | [c] ] = [a. c] Tail = [b. (2008) 44 . Operations on Lists Membership Concatenation Adding an item Deleting an item Sublist Permutations CPT114/CPT104 Shahida S. (2008) 45 . %true CPT114/CPT104 Shahida S.c]] ).[b. [Head | Tail] ) :member(X. %false. %simple fact %rule Example: member(b. the tail is [b. member(X. [X | Tail] ).Membership X is a member of L either: 1) X is the head of L. or 2) X is a member of the tail of L. member(X. (2008) 46 .c] member([b. Tail).c]] ).[a.c].c] ). [a.[b. %true member(b.b. [a. L) Can be written in two clauses: member(X. L = [a. [ ]. L3 is their concatenation: conc(L1.b. [c.conc( [a.b].[ ].b]. [1. ?.d] ). d. [a.2. (2008) 47 .Concatenation Define the relation conc where L1 and L2 are lists. [a. conc( [a.c.3] ?.d]. [b. L).b].3]. b] %true %false CPT114/CPT104 Shahida S.c. L = [a.c].b. L3) Example: conc( [a.c. [c. [a.d]. L2.2.a.conc( [a.c].d]. a.[b.b.1.b.c].d] ). L). L get the list L1: del(X.b.a].a]. L).a].del(a.a].Add & Delete Item To add an item. [X | L] add(X. X from a list.g.a. L. (2008) 48 . L: put it in front the list as a new head e. X to a list. L1). L = [a.g. L = [a. no CPT114/CPT104 Shahida S.b. L. ?. [a. [X | L] ).a. L = [b. If there are several occurrences of X in the list then del will delete any one of them by backtracking until del fails to delete any more item e. Delete an item.b. c].g.c.b]. S = [a.Sublist The sublist relation has 2 arguments: list L and list S such that S occurs within L as its sublist: sublist( [c. « CPT114/CPT104 Shahida S.e]. %true %false It can also be used to find all sublists of a given list e.c.d. [a. ?. S = [a].e].b. b.d. S = [b].sublist( S.c] ). S = [ ].b. [a. S = [ ]. [a.e.d. S = [a. sublist( [c.f] ). (2008) 49 .f] ).e.b. permutation( [a. blue]. P ). « Insert X obtain a X L Example: permutation of [X | L] ?.c].c]. red.a. green]. green]. (2008) L1 L1 is a permutation of L 50 . ?. green.green]. P = [blue. green.c].permutation( [red.c. P = [a. red]. blue. blue].b. no CPT114/CPT104 Shahida S. red.b]. P = [b. P ). P = [green. P = [red. P = [green. P = [red.b.blue. P = [a. red]. blue.Permutations The permutation relation with 2 arguments of 2 lists such that one is a permutation of the other. P = [blue. c) ) Programmers can define their own operators e. pet). + * 2 * c a b Tree representation Prolog terms using functor (the operator with the highest precedence (assume range: 1 to 1200) and is the principal functor of the term: +( *(2. peter has pet.a).g. has(peter. b are arguments. (2008) 51 .Operator Notation Expressions in Mathematics such as: 2*a + b*c  where + and * are infix operators that appear between the 2 arguments. a. *(b. %the fact equivalent to« CPT114/CPT104 Shahida S.  2. ) 3 groups of operator types where arguments x whose precedence < operator. 0 b c prec. 500 52 prec. (2008) a prec.Operator Notation (cont. y whose precedence e operator and f is infix:  infix operators of 3 types: xfx fx fy xfy yfx  prefix operators of 2 types:  postfix operators of 2 types: xf yf Interpretation a ± (b ± c) is invalid because prec. 500 a CPT114/CPT104 . b ± c is not less than µ-¶ The expression below has infix of yfx type: a±b±c c b prec. 0 Shahida S. X=3 Comparing numerical values e.300 * 35 > 10000. yes CPT114/CPT104 Shahida S.Arithmetic Predefined operators for basic arithmetic operations:  addition + subtraction multiplication *  division / power ** integer division //  modula.g. (2008) 53 .X is 1 + 2. ?. the remainder of integer division mod Example: ?. (2008) 54 .5.5 Using Structures: Example Program Retrieving structured information from a database Doing data abstraction Example: Travel agent CPT114/CPT104 Shahida S. 15200) ). fox. job (unemployed or working organization & salary).1983). may. fox. [person(pat. (2008) . DOB. date(7.may. unemployed)] ).1960). unemployed). person(jim. works(bbc. Example: structuring information about a family of 4 components: name. pat 5 person fox date unemp. unemployed). date(5. 1983 ‡ ‡ person date [] may jim fox 5 may unemp.may. date(9. 1961).1983).may. person(ann. date(5. fox. 1983 55 CPT114/CPT104 Shahida S. family( person(tom. family person tom fox date 7 may 1960 works bbc 15200 ann fox 9 may person date 1961 unemp. surname. fox.Retrieving Structured Information from a Database A database can be represented in Prolog as a set of facts. _ ) Refer all families with 3 children: family( _. Surname. _. leave the arguments as anonymous variables._ ). _] ) Find all married women with at least 3 children: ?. CPT114/CPT104 Shahida S._. [_. fox. [_. (2008) 56 . Refer to all Fox family as: family(person( _. _. _. _ | _] ).Example: Retrieving Database Specify objects not by content but by their structure.family( person(Name. _). Programmer makes use of information possible without having to think about the details of how the information is actually represented. Select particular components using selectors relation with 2 arguments: object that contains the component and the component itself. Component_selected) CPT114/CPT104 Shahida S. selector_relation(Object. It is structuring the information into some conceptually meaningful form. (2008) 57 .Data Abstraction Data abstraction: a process of organizing various pieces of information into natural units (possibly hierarchically). Family.Example family( person(tom. Child) :children(Family. children(family(_. _. nth_member(N.may. Generalize selector for Nth element of a list: nthchild(N. _ ). 15200) ). works(bbc. _ ). (2008) 58 . _. _). date(7. Wife). [person(pat. unemployed)] ) Selectors for family structure: husband(family(Husband. Surname. _ ). ChildList). born(person(_. Date.1960). surname(person(_.may. Wife.1983).may. CPT114/CPT104 Shahida S. date(5. ChildList. date(5. _. _ ). Husband). _. unemployed). _. %Nth element of a list Selectors for object person: firstname(person(Name. _. Date). fox. wife(family(_. ChildList). fox. Surname). Name). fox. Child).1983). person(jim. ChildList). [9:40/10:50/ba4733/alldays. 19:40/20:50/ba4833/[mo. (2008) 59 . ListOfFlights) ListOfFlights is a list of structured items: DepartureTime/ArrivalTime/FlightNumber/ListOfDays E. edinburgh.tu.Example: Travel Agent A program that gives advice on planning air travel to answer some questions such as:  What days of the week is there a direct evening flight from Ljubljana to London? Database holds flight information: timetable(Place1.g. Place2.th.we. of clause of the timetable relation: timetable(london. CPT114/CPT104 Shahida S.su] ] ).fr. flight(Place1. Day. FlightNum. between Place1 and Place2 on the day of the week Day with specified departure and arrival times. DepTime. deptime(Route. FlightNum. Day. Time2) CPT114/CPT104 Shahida S. Route) Route is a list of structured objects: From/To/FlightNumber/Departure_time Auxiliary predicates:  There is a flight.) Route relation: route(Place1. Place2. Place2.Example: Travel Agent (cont. (2008) 60 . transfer(Time1. Time)  There is at least 40 minutes between Time1 and Time2 that is sufficient for transfer between 2 flights. ArrTime)  Departure time of Route is Time. % :% Direct flight | RestRoute] ) :Indirect connection Departure time of Route Enough time for transfer flight( Place1. P2. [ P1 / P2 / Fnum / Deptime ] ) flight( P1. P2. Day. Dep1. Arr1). deptime( RestRoute. Place2. Day. P2. flyday( Day. Place2.Prolog Program % A FLIGHT ROUTE PLANNER :op( 50. Deptime. % ending at Place2 route( P1. member( Deptime / Arrtime / Fnum / Daylist . Flightlist). RestRoute). Fnum1. Day. % route( Place1. _). CPT114/CPT104 Shahida S. Dep2). Day. Route): % Route is a sequence of flights on Day. Place2. xfy. P2. % transfer( Arr1. route( P1. Fnum. Daylist). Arrtime) :timetable( Place1. Day. starting at Place1. :). [ (P1 / P3 / Fnum1 / Dep1) % route( P3. (2008) 61 . Deptime. Flightlist). Day. Day. flight( P1. P3. Fnum. Dep2). [X | L] ). deptime( [ P1 / P2 / Fnum / Dep | _]. flyday( Day.su] ).we. alldays) :member( Day. member( X.Mins1 >= 40. [Y | L] ) member( X. (2008) 62 . Dep). L).th. Daylist) :member( Day. [mo.) % A FLIGHT ROUTE PLANNER (cont.Hours1) + Mins2 .Prolog Program (cont.) flyday( Day. member( X.fr.sa.tu. Daylist). :- CPT114/CPT104 Shahida S. transfer( Hours1:Mins1. Hours2:Mins2) :60 * (Hours2 . we.we.fr.th] ] ). (2008) 63 .Prolog Database % A FLIGHT DATABASE timetable( edinburgh. ljubljana. 13:40 / 14:50 / ba4773 / alldays. / 19:50 / ba4822 / [mo. 11:00 / 13:50 / az459 / alldays ] ). timetable( ljubljana. milan. timetable( london.su]. [ 13:20 / 16:20 / jp212 / [mo. / 10:50 / ba4732 / alldays.we.su] ] ). / 12:50 / ba4752 / alldays.tu.sa] ] ). [ 8:30 / 11:20 / ba510 / alldays. [ 9:40 / 10:50 / ba4733 / alldays.th. zurich.fr] ] ). timetable( london.fr. timetable( london. london.th. 14:45 / 17:20 / sr805 / alldays ] ).we. zurich. [ 9:40 11:40 18:40 edinburgh. [ 9:10 / 11:45 / ba614 / alldays. timetable( london. 16:30 / 19:30 / ba473 / [mo. CPT114/CPT104 Shahida S. [ 11:30 / 12:40 / jp322 / [tu.tu.tu. 19:40 / 20:50 / ba4833 / [mo.th. sa].th] ] ).th.) % A FLIGHT DATABASE (cont.fr. (2008) 64 . 12:20 / 13:10 / ba511 / alldays ] ).fr. timetable( milan. milan. [ 9:00 / 9:40 / ba613 / [mo. [ 9:10 / 10:00 / az458 / alldays. zurich. [ 9:25 / 10:15 / sr621 / alldays. timetable( milan. 12:45 / 13:35 / sr623 / alldays ] ).we. 16:10 / 16:55 / sr806 / [mo.we.th.we.tu. timetable( zurich. london.we. london.tu. [ 11:10 / 12:20 / jp211 / [mo. timetable( zurich.Prolog Database (cont. london. CPT114/CPT104 Shahida S.) timetable( ljubljana. [ 7:55 / 8:45 / sr620 / alldays ] ).su] ] ).tu. 20:30 / 21:30 / ba472 / [mo.su]. [ 13:30 / 14:40 / jp323 / [tu.fr. timetable( zurich.sa] ] ). ljubljana.th. City3. we. flight( london. permutation( [].[City1.FN2.L2. Arr3). FN2. City3. del( X. flight( City1. L).City3]).City2. L. P). Dep2. flight( City3. Dep4.L2. Arr2). City1. [X | P]) del( X. london. conc([]. FN1. permutation( L1. FN3.FN3. [X|L3]) :conc(L1. City2. [Y|L]. Dep1.zurich]. conc([X|L1].ljubljana. flight( City2.L3). del( X. Arr4). []). tu. CPT114/CPT104 :- :- Shahida S. L1).FN4) :permutation( [milan. [X|L]. Dep3. L1). permutation( L. Arr1). FN4.FN1. L). th.City2.Operations query3(City1. fr. (2008) 65 . L. [Y|L1]) del( X. L. DeptHour:_. _). « How can I get from Ljubljana to Edinburg on Thursday? ?. edinburg. london.flight(ljubljana. DeptHour >= 18. zurich/london/sr806/16:10. Day. Day = we. Day = mo. london/edinburgh/ba4822/18:40] CPT114/CPT104 Shahida S. R).Example of Queries What days of the week is there a direct evening flight from Ljubljana to London? ?. (2008) 66 . th.route(ljubljana. _. R = [ljubljana/zurich/jp322/11:30.
Copyright © 2024 DOKUMEN.SITE Inc.