fork download
  1. /*References:
  2. * https://w...content-available-to-author-only...c.uk/~mas02gw/prolog_tutorial/prologpages/
  3. *
  4. * */
  5.  
  6. /************
  7. * Facts
  8. * Always start with lowercase letter, end with full stop
  9. * You could use _ in facts (names), but please avoid math operators
  10. */
  11.  
  12. thanksgivingsoon.
  13. sunny.
  14. wanttoplay.
  15.  
  16. /*These are all facts. This whole page is your database for your facts & rules.
  17. You can also ask questions/query about your database.*/
  18.  
  19. /*Now, on the right side, after the ?- ask your first query:
  20. * sunny.
  21. * Hit run and see if the answer is yes
  22. * the ?- means asking a question, it is already provided by this complier.
  23. * You would need to write it in other compliers in format: ?- sunny.
  24. */
  25.  
  26. /*Now, ask query: ?- raining.
  27. * What is the output? */
  28.  
  29. /*__your answer here___*/
  30.  
  31. /*You can defind facts with arguments. (just like a function with input variables)
  32. * It is just relationship between items.
  33. * Relation names must begin with lowercase letter as well.*/
  34.  
  35. likes(mary,food).
  36. likes(mary,wine).
  37. likes(john,wine).
  38. likes(john,mary).
  39.  
  40. /*Ask query: ?- likes(mary,food).*/
  41.  
  42. /*__your answer here___*/
  43.  
  44. /*OK, some KOANS about facts: */
  45.  
  46. eats(fred,oranges). /* "Fred eats oranges" */
  47. /*__your answer here___*/ /* "Fred eats T-bone steaks" */
  48. /*__your answer here___*/ /* "Tony eats apples" */
  49. /*__your answer here___*/ /* "John eats apples" */
  50.  
  51. /*Ask some queries on the right side:
  52. * Does Fred eats T-bone steaks?
  53. * ?- eats(tony,pears). means what? What is the result?*/
  54.  
  55. /*__your answer here___*/
  56.  
  57. /*You can also link up two sentances with ,
  58. * Try query:
  59. * ?- eats(fred,X),
  60. * X=oranges
  61. * What is the output? What is the meaning of this query?*/
  62. /*__your answer here___*/
  63.  
  64. age(ian,2). /* Ian is 2 */
  65.  
  66. /*will query ?- age(ian, two). give yes? Why?*/
  67.  
  68. /*__your answer here___*/
  69.  
  70. /*************
  71. * Variables
  72. * With only the facts, we cannot really do anything productive with this PL.
  73. * Just like other PLs, we need variables.
  74. * Variables have to start with a CAPITAL letter
  75. * The process of matching items with variables is known as unification.*/
  76. /*
  77. * X: vairable X
  78. * Myvar: another legal variable
  79. * Myvar_current: also ok*/
  80.  
  81. /*What is the result of query ?- eats(fred,FoodX). ?
  82. *Make sure you press next button to see all the results.
  83. *In other editors, you would type ; to represent show next result.*/
  84.  
  85. /*__your answer here___*/
  86.  
  87. /***********
  88. * Rules
  89. * Each rule can have several variations -- clauses.
  90. * Rules provide ways to get inferences about facts.
  91. */
  92.  
  93. /*Operator :- means if*/
  94. mortal(X) :-
  95. human(X). /*X is mortal if X is human*/
  96. human(petter). /*give a fact that petter is a human*/
  97.  
  98. /*__your answer here___*/ /*Ask query: is petter mortal?*/
  99. /*What is the result of your query?*/
  100. /*__your answer here___*/
  101.  
  102. /*__your answer here___*/ /*Ask query: who is mortal?*/
  103. /*__your answer here___*/ /*What is the result of your query?*/
  104.  
  105. /*One rule can have several variations.
  106. * The following example shows 3 ways that some thing is fun*/
  107.  
  108. fun(X) :- /* an item is fun if */
  109. red(X), /* the item is red */
  110. car(X). /* and it is a car */
  111. fun(X) :- /* or an item is fun if */
  112. blue(X), /* the item is blue */
  113. bike(X). /* and it is a bike */
  114. fun(ice_cream). /* and ice cream is also fun. */
  115.  
  116. /*Together with some facts:*/
  117. car(vw_beatle).
  118. car(ford_escort).
  119. bike(harley_davidson).
  120. red(vw_beatle).
  121. red(ford_escort).
  122. blue(harley_davidson).
  123.  
  124. /*__your answer here___*/ /*Ask query: is harley_davidson fun?*/
  125. /*__your answer here___*/ /*What is the result of your query?*/
  126. /*What are the logical steps that Prolog took to find the answer for you?
  127. * Hint: start matching from the first fun clause.*/
  128. /*__your answer here___*/
  129.  
  130. /*__your answer here___*/ /*Ask query: What is fun?*/
  131.  
  132. /****************
  133. * Recursion
  134. */
  135. /*some facts:*/
  136. move(home,taxi,halifax). /*__your answer here___*/
  137. move(halifax,train,gatwick). /*__your answer here___*/
  138. move(gatwick,plane,rome). /*__your answer here___*/
  139.  
  140. /*on_route is a recursive predicate*/
  141. on_route(rome).
  142. on_route(Place):- /*what does this mean?*/ /*__your answer here___*/
  143. move(Place,_Method,NewPlace), /*__your answer here___*/
  144. on_route(NewPlace). /*__your answer here___*/
  145.  
  146. /* Query ?- on_route(home). is asking whether you can reach rome from home*/
  147. /*What are the steps that Prolog can use to find the answer for you?*/
  148. /*__your answer here___*/
  149.  
  150. /*Change _Method to Method, what will happen?*/
  151. /*By adding _ in front of a variable, we make it an anonymous variable and let the complier know
  152. * this var is not important so stop complaining about it.
  153. * Does this remind you of some thing from F#?*/
  154.  
  155. /*****************
  156. * List
  157. */
  158. [a,b,c,d,e,f,g]. /*a list with simple facts*/
  159. /*__your answer here___*/ /*an empty list*/
  160. /*__your answer here___*/ /*a list with 3 names as facts*/
  161.  
  162. /*Just like F#, list in Prolog matches Head and Tail.*/
  163. p([H|T], H, T). /*bind/unify list [...] with head item H, and tail item T*/
  164. /*__your answer here___*/ /*What is the meaning of query ?-p([a,b,c], X, Y). ?*/
  165. /*__your answer here___*/ /*Output?*/
  166.  
  167. /*write queries to cut H and T out from the above 3 lists.*/
  168. /*__your answer here___*/
  169.  
  170. /*Search an item from a list, write down the meaning of each line:*/
  171. inList(Item,[Item,Rest]). /*__your answer here___*/
  172. inList(Item,[IgnoreHead|Tail]) :- /*__your answer here___*/
  173. inList(Item,Tail). /*__your answer here___*/
  174.  
  175. /*Write a query that test if apple is in the list [apple, pear, banana]*/
  176. /*__your answer here___*/
  177. /*What are the steps that Prolog can use to find the answer for you?*/
  178. /*__your answer here___*/
  179. /*Write a query that test if apple is in the list [pear, apple, banana]*/
  180. /*__your answer here___*/
  181. /*What are the steps that Prolog can use to find the answer for you?*/
  182. /*__your answer here___*/
  183.  
  184. /*Does this example reminds you of some thing from F#?*/
  185. /*__your answer here___*/
  186.  
  187.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.pl:158: fatal error: redefining built-in predicate '.'/2
compilation failed
stdout
Standard output is empty