/tutorial/backtracking/tiny_prolog/prolog_builtins.e

http://github.com/tybor/Liberty · Specman e · 189 lines · 184 code · 4 blank · 1 comment · 17 complexity · be32044e99ec5228589e8d96b7750df0 MD5 · raw file

  1. class PROLOG_BUILTINS
  2. inherit PROLOG_COMMONS ABSTRACT_BACKTRACKING
  3. creation
  4. make
  5. feature
  6. make is
  7. do
  8. init_builtins
  9. end
  10. feature
  11. current_term: PROLOG_TERM
  12. current_compound: PROLOG_COMPOUND
  13. current_atom: PROLOG_ATOM
  14. current_functor: PROLOG_FUNCTOR
  15. current_proc: ROUTINE[like Current, TUPLE[]]
  16. current_data: LINKED_LIST[LINKED_LIST[PROLOG_TERM]]
  17. feature
  18. evaluate_current_state is
  19. local
  20. atom: PROLOG_ATOM
  21. compound: PROLOG_COMPOUND
  22. functor: PROLOG_FUNCTOR
  23. do
  24. current_compound ?= current_term
  25. if current_compound /= Void then
  26. current_atom := current_compound.atom
  27. check current_atom /= Void end
  28. else
  29. current_atom ?= current_term
  30. end
  31. if current_atom = Void then
  32. -- TODO gerer les exeception de call
  33. partial_fail
  34. else
  35. current_functor.make(current_atom,current_term.arity)
  36. current_proc := builtins.reference_at(current_functor)
  37. if current_proc /= Void then
  38. current_proc.call([Current])
  39. else
  40. evaluate_from_database
  41. end
  42. end
  43. end
  44. feature
  45. evaluate_from_database is
  46. do
  47. current_data := database.reference_at(current_functor)
  48. if current_data = Void then
  49. partial_fail
  50. else
  51. end
  52. end
  53. feature {}
  54. init_builtins is
  55. do
  56. if builtins.is_empty then really_init_builtins end
  57. end
  58. really_init_builtins is
  59. do
  60. add_bi(once "fail", 0, agent {like Current}.bi_fail)
  61. add_bi(once "true", 0, agent {like Current}.bi_true)
  62. add_bi(once "atom", 1, agent {like Current}.bi_atom)
  63. add_bi(once "integer", 1, agent {like Current}.bi_integer)
  64. add_bi(once "float", 1, agent {like Current}.bi_float)
  65. add_bi(once "number", 1, agent {like Current}.bi_number)
  66. add_bi(once "atomic", 1, agent {like Current}.bi_atomic)
  67. add_bi(once "compound", 1, agent {like Current}.bi_compound)
  68. add_bi(once "callable", 1, agent {like Current}.bi_callable)
  69. add_bi(once "var", 1, agent {like Current}.bi_var)
  70. add_bi(once "nonvar", 1, agent {like Current}.bi_nonvar)
  71. add_bi(once "=", 2, agent {like Current}.bi_unify)
  72. add_bi(once "\=", 2, agent {like Current}.bi_not_unify)
  73. end
  74. add_bi(name: STRING; arity: INTEGER; proc: like current_proc) is
  75. local
  76. functor: PROLOG_FUNCTOR
  77. do
  78. builtins.add(proc,get_functor_by_name(name,arity))
  79. end
  80. feature {}
  81. partial_switch(success: BOOLEAN) is
  82. do
  83. if success then
  84. partial_success
  85. else
  86. partial_fail
  87. end
  88. end
  89. feature {}
  90. bi_fail is
  91. do
  92. partial_fail
  93. end
  94. bi_true is
  95. do
  96. partial_success
  97. end
  98. feature {} -- type testing
  99. bi_atom is
  100. do
  101. inspect current_compound.args.first.type
  102. when type_atom then partial_success
  103. else partial_fail
  104. end
  105. end
  106. bi_integer is
  107. do
  108. inspect current_compound.args.first.type
  109. when type_integer then partial_success
  110. else partial_fail
  111. end
  112. end
  113. bi_float is
  114. do
  115. inspect current_compound.args.first.type
  116. when type_float then partial_success
  117. else partial_fail
  118. end
  119. end
  120. bi_number is
  121. do
  122. inspect current_compound.args.first.type
  123. when type_integer, type_float then partial_success
  124. else partial_fail
  125. end
  126. end
  127. bi_atomic is
  128. do
  129. inspect current_compound.args.first.type
  130. when type_atom, type_integer, type_float then partial_success
  131. else partial_fail
  132. end
  133. end
  134. bi_compound is
  135. do
  136. inspect current_compound.args.first.type
  137. when type_compound then partial_success
  138. else partial_fail
  139. end
  140. end
  141. bi_callable is
  142. do
  143. inspect current_compound.args.first.type
  144. when type_atom, type_compound then partial_success
  145. else partial_fail
  146. end
  147. end
  148. bi_var is
  149. do
  150. inspect current_compound.args.first.type
  151. when type_var then partial_success
  152. else partial_fail
  153. end
  154. end
  155. bi_nonvar is
  156. do
  157. inspect current_compound.args.first.type
  158. when type_var then partial_fail
  159. else partial_success
  160. end
  161. end
  162. feature {} -- unification
  163. bi_unify is
  164. do
  165. if unify(current_compound.args.item(0),current_compound.args.item(1)) then
  166. partial_success
  167. else
  168. partial_fail
  169. end
  170. end
  171. bi_not_unify is
  172. do
  173. if unify(current_compound.args.item(0),current_compound.args.item(1)) then
  174. partial_fail
  175. else
  176. partial_success
  177. end
  178. end
  179. bi_functor is
  180. do
  181. end
  182. bi_call is
  183. do
  184. end
  185. end