wk3.pl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. sentence(sentence(NP, VP)) --> np(Num, NP), vp(Num, _, VP).
  2. np(Num, np(D, N)) --> det(D), noun(Num, N).
  3. np(Num, np(D, N, Rel)) --> det(D), noun(Num, N), relcl(Num, Rel).
  4. np(Num, np(D, A, N)) --> det(D), adj(A), noun(Num, N).
  5. np(Num, np(D, A, N, Rel)) --> det(D), adj(A), noun(Num, N), relcl(Num, Rel).
  6. np(Num, np(N)) --> noun(Num, N).
  7. np(Num, np(N, Rel)) --> noun(Num, N), relcl(Num, Rel).
  8. relcl(_, rel(R, NP, VP)) --> rel(obj, R), np(Num, NP), vp(Num, _, VP, obj).
  9. relcl(Num, rel(R, VP)) --> rel(subj, R), vp(Num, _, VP, subj).
  10. vp(Num, be, vp(V, A)) --> verb(Num, be, V), adj(A).
  11. vp(Num, tr, vp(V, NP)) --> verb(Num, tr, V), np(_, NP).
  12. vp(Num, intr, vp(V)) --> verb(Num, intr, V).
  13. vp(Num, tr, vp(V), obj) --> verb(Num, tr, V).
  14. vp(Num, tr, vp(V, NP), subj) --> verb(Num, tr, V), np(_, NP).
  15. vp(Num, intr, vp(V), subj) --> verb(Num, intr, V).
  16. vp(Num, be, vp(V, A), subj) --> verb(Num, be, V), adj(A).
  17. det(det(D)) --> [D], {is_det(D)}.
  18. noun(Num, noun(N)) --> [N], {is_noun(Num, N)}.
  19. rel(Obj, rel(Rel)) --> [Rel], {is_rel(Obj, Rel)}.
  20. verb(Num, Tr, verb(V)) --> [V], {is_verb(Num, Tr, V)}.
  21. adj(adj(A)) --> [A], {is_adj(A)}.
  22. is_noun(sg, apple).
  23. is_noun(sg, boy).
  24. is_noun(sg, girl).
  25. is_noun(sg, government).
  26. is_noun(sg, watermelon).
  27. is_noun(sg, flavor).
  28. is_noun(sg, person).
  29. is_noun(pl, people).
  30. is_noun(pl, Np) :- is_noun(sg, N), atom_chars(N, A), atom_chars(Np, B), append(A, [s], B).
  31. is_det(a).
  32. is_det(an).
  33. is_det(the).
  34. is_det(all).
  35. is_det(some).
  36. is_verb(pl, tr, conscript).
  37. is_verb(pl, tr, eat).
  38. is_verb(pl, tr, like).
  39. is_verb(pl, intr, run).
  40. is_verb(sg, be, is).
  41. is_verb(pl, be, are).
  42. is_verb(pl, tr, contain).
  43. is_verb(sg, Tr, Vs) :- is_verb(pl, Tr, V), atom_chars(V, A), atom_chars(Vs, B), append(A, [s], B).
  44. is_adj(evil).
  45. is_adj(divine).
  46. is_adj(big).
  47. is_adj(tasty).
  48. is_adj(pacifist).
  49. is_rel(obj, that).
  50. is_rel(obj, whom).
  51. is_rel(subj, who).
  52. is_rel(subj, which).
  53. ?- op(500,xfy,&).
  54. ?- op(600,xfy,=>).
  55. np(Var, np(DET, NOUN), P3) :-
  56. n(Var, NOUN, P1),
  57. det(Var, DET, P1, P3).
  58. np(Var,np(DET,ADJ,NOUN),P3) :-
  59. adj(Var,ADJ,P1),
  60. n(Var,NOUN,P2),
  61. det(Var,DET,P1,P2,P3).
  62. np(Var, np(DET, NOUN, REL), P3) :-
  63. n(Var, NOUN, P1),
  64. det(Var, DET, P1, P2),
  65. rel(Var, REL, P2, P3).
  66. np(Var, np(DET, ADJ, NOUN, REL), P4) :-
  67. n(Var, NOUN, P2),
  68. adj(Var, ADJ, P1),
  69. det(Var, DET, P1, P2, P3),
  70. rel(Var, REL, P3, P4).
  71. det(Var,DET,P1,P2,P3) :- % noun-adj, universal
  72. DET =.. [det, all],
  73. P3 =.. [all, Var, (P2 & P1)].
  74. det(Var, DET, P1, P2) :- % noun, universal
  75. DET =.. [det, all],
  76. P2 =.. [all, Var, P1].
  77. det(Var, DET, P1, P2, P3) :- % noun-adj, existential
  78. DET =.. [det, some],
  79. P3 =.. [exists, Var, (P2 & P1)].
  80. det(Var, DET, P1, P2) :- % noun, existential
  81. DET =.. [det, some],
  82. P2 =.. [exists, Var, P1].
  83. adj(Var,ADJ,P) :-
  84. ADJ =.. [adj, A],
  85. P =.. [A,Var].
  86. n(Var,NOUN,P) :-
  87. NOUN =.. [noun, N],
  88. P =.. [N,Var].
  89. rel(Var, rel(_, VP), DET, P3) :-
  90. vp(Var, DET, VP, true, P3).
  91. vp(Var, all(A, B), vp(BE, ADJ), P3) :- % 'to be' verb w/ adj
  92. BE =.. [verb, BB],
  93. is_verb(_, be, BB),
  94. adj(Var, ADJ, C),
  95. P3 =.. [all, A, (B => C)].
  96. vp(Var, all(A, B), vp(VERB), P3) :- % intransitive verb
  97. VERB =..[verb, V],
  98. C =.. [V, Var],
  99. P3 =.. [all, A, (B => C)].
  100. vp(Var, all(A, B), vp(VERB, NP), P3) :- % transitive verb w/ object
  101. VERB =.. [verb, V],
  102. gensym(x, Var2),
  103. np(Var2, NP, C),
  104. D =.. [V, Var, C],
  105. P3 =.. [all, A, (B => D)].
  106. vp(Var, exists(A, B), vp(BE, ADJ), P3) :-
  107. BE =.. [verb, BB],
  108. is_verb(_, be, BB),
  109. adj(Var, ADJ, C),
  110. P3 =.. [exists, A, B, C].
  111. vp(Var, exists(A, B), vp(VERB), P3) :-
  112. VERB =.. [verb, V],
  113. C =.. [V, Var],
  114. P3 =.. [exists, A, B, C].
  115. vp(Var, exists(A, B), vp(VERB, NP), P3) :-
  116. VERB =.. [verb, V],
  117. gensym(x, Var2),
  118. np(Var2, NP, C),
  119. D =.. [V, Var, C],
  120. P3 =.. [exists, A, B, D].
  121. vp(Var, all(A, B), vp(BE, ADJ), true, P3) :- % 'to be' verb w/ adj
  122. BE =.. [verb, BB],
  123. is_verb(_, be, BB),
  124. adj(Var, ADJ, C),
  125. P3 =.. [all, A, (B & C)].
  126. vp(Var, all(A, B), vp(VERB), true, P3) :- % intransitive verb
  127. VERB =..[verb, V],
  128. C =.. [V, Var],
  129. P3 =.. [all, A, (B & C)].
  130. vp(Var, all(A, B), vp(VERB, NP), true, P3) :- % transitive verb w/ object
  131. VERB =.. [verb, V],
  132. gensym(x, Var2),
  133. np(Var2, NP, C),
  134. D =.. [V, Var, C],
  135. P3 =.. [all, A, (B & D)].
  136. vp(Var, exists(A, B), VP, true, P3) :-
  137. vp(Var, exists(A, B), VP, P3).
  138. s(sentence(NP,VP),F) :-
  139. gensym(x,X),
  140. np(X,NP,T), vp(X,T,VP,F).
  141. prlogic(English, Logic) :- sentence(T, English, []), s(T, Logic).