logic.py 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055
  1. # Natural Language Toolkit: Logic
  2. #
  3. # Author: Dan Garrette <dhgarrette@gmail.com>
  4. #
  5. # Copyright (C) 2001-2020 NLTK Project
  6. # URL: <http://nltk.org>
  7. # For license information, see LICENSE.TXT
  8. """
  9. A version of first order predicate logic, built on
  10. top of the typed lambda calculus.
  11. """
  12. import re
  13. import operator
  14. from collections import defaultdict
  15. from functools import reduce, total_ordering
  16. from nltk.util import Trie
  17. from nltk.internals import Counter
  18. APP = "APP"
  19. _counter = Counter()
  20. class Tokens(object):
  21. LAMBDA = "\\"
  22. LAMBDA_LIST = ["\\"]
  23. # Quantifiers
  24. EXISTS = "exists"
  25. EXISTS_LIST = ["some", "exists", "exist"]
  26. ALL = "all"
  27. ALL_LIST = ["all", "forall"]
  28. # Punctuation
  29. DOT = "."
  30. OPEN = "("
  31. CLOSE = ")"
  32. COMMA = ","
  33. # Operations
  34. NOT = "-"
  35. NOT_LIST = ["not", "-", "!"]
  36. AND = "&"
  37. AND_LIST = ["and", "&", "^"]
  38. OR = "|"
  39. OR_LIST = ["or", "|"]
  40. IMP = "->"
  41. IMP_LIST = ["implies", "->", "=>"]
  42. IFF = "<->"
  43. IFF_LIST = ["iff", "<->", "<=>"]
  44. EQ = "="
  45. EQ_LIST = ["=", "=="]
  46. NEQ = "!="
  47. NEQ_LIST = ["!="]
  48. # Collections of tokens
  49. BINOPS = AND_LIST + OR_LIST + IMP_LIST + IFF_LIST
  50. QUANTS = EXISTS_LIST + ALL_LIST
  51. PUNCT = [DOT, OPEN, CLOSE, COMMA]
  52. TOKENS = BINOPS + EQ_LIST + NEQ_LIST + QUANTS + LAMBDA_LIST + PUNCT + NOT_LIST
  53. # Special
  54. SYMBOLS = [x for x in TOKENS if re.match(r"^[-\\.(),!&^|>=<]*$", x)]
  55. def boolean_ops():
  56. """
  57. Boolean operators
  58. """
  59. names = ["negation", "conjunction", "disjunction", "implication", "equivalence"]
  60. for pair in zip(names, [Tokens.NOT, Tokens.AND, Tokens.OR, Tokens.IMP, Tokens.IFF]):
  61. print("%-15s\t%s" % pair)
  62. def equality_preds():
  63. """
  64. Equality predicates
  65. """
  66. names = ["equality", "inequality"]
  67. for pair in zip(names, [Tokens.EQ, Tokens.NEQ]):
  68. print("%-15s\t%s" % pair)
  69. def binding_ops():
  70. """
  71. Binding operators
  72. """
  73. names = ["existential", "universal", "lambda"]
  74. for pair in zip(names, [Tokens.EXISTS, Tokens.ALL, Tokens.LAMBDA]):
  75. print("%-15s\t%s" % pair)
  76. class LogicParser(object):
  77. """A lambda calculus expression parser."""
  78. def __init__(self, type_check=False):
  79. """
  80. :param type_check: bool should type checking be performed?
  81. to their types.
  82. """
  83. assert isinstance(type_check, bool)
  84. self._currentIndex = 0
  85. self._buffer = []
  86. self.type_check = type_check
  87. """A list of tuples of quote characters. The 4-tuple is comprised
  88. of the start character, the end character, the escape character, and
  89. a boolean indicating whether the quotes should be included in the
  90. result. Quotes are used to signify that a token should be treated as
  91. atomic, ignoring any special characters within the token. The escape
  92. character allows the quote end character to be used within the quote.
  93. If True, the boolean indicates that the final token should contain the
  94. quote and escape characters.
  95. This method exists to be overridden"""
  96. self.quote_chars = []
  97. self.operator_precedence = dict(
  98. [(x, 1) for x in Tokens.LAMBDA_LIST]
  99. + [(x, 2) for x in Tokens.NOT_LIST]
  100. + [(APP, 3)]
  101. + [(x, 4) for x in Tokens.EQ_LIST + Tokens.NEQ_LIST]
  102. + [(x, 5) for x in Tokens.QUANTS]
  103. + [(x, 6) for x in Tokens.AND_LIST]
  104. + [(x, 7) for x in Tokens.OR_LIST]
  105. + [(x, 8) for x in Tokens.IMP_LIST]
  106. + [(x, 9) for x in Tokens.IFF_LIST]
  107. + [(None, 10)]
  108. )
  109. self.right_associated_operations = [APP]
  110. def parse(self, data, signature=None):
  111. """
  112. Parse the expression.
  113. :param data: str for the input to be parsed
  114. :param signature: ``dict<str, str>`` that maps variable names to type
  115. strings
  116. :returns: a parsed Expression
  117. """
  118. data = data.rstrip()
  119. self._currentIndex = 0
  120. self._buffer, mapping = self.process(data)
  121. try:
  122. result = self.process_next_expression(None)
  123. if self.inRange(0):
  124. raise UnexpectedTokenException(self._currentIndex + 1, self.token(0))
  125. except LogicalExpressionException as e:
  126. msg = "%s\n%s\n%s^" % (e, data, " " * mapping[e.index - 1])
  127. raise LogicalExpressionException(None, msg)
  128. if self.type_check:
  129. result.typecheck(signature)
  130. return result
  131. def process(self, data):
  132. """Split the data into tokens"""
  133. out = []
  134. mapping = {}
  135. tokenTrie = Trie(self.get_all_symbols())
  136. token = ""
  137. data_idx = 0
  138. token_start_idx = data_idx
  139. while data_idx < len(data):
  140. cur_data_idx = data_idx
  141. quoted_token, data_idx = self.process_quoted_token(data_idx, data)
  142. if quoted_token:
  143. if not token:
  144. token_start_idx = cur_data_idx
  145. token += quoted_token
  146. continue
  147. st = tokenTrie
  148. c = data[data_idx]
  149. symbol = ""
  150. while c in st:
  151. symbol += c
  152. st = st[c]
  153. if len(data) - data_idx > len(symbol):
  154. c = data[data_idx + len(symbol)]
  155. else:
  156. break
  157. if Trie.LEAF in st:
  158. # token is a complete symbol
  159. if token:
  160. mapping[len(out)] = token_start_idx
  161. out.append(token)
  162. token = ""
  163. mapping[len(out)] = data_idx
  164. out.append(symbol)
  165. data_idx += len(symbol)
  166. else:
  167. if data[data_idx] in " \t\n": # any whitespace
  168. if token:
  169. mapping[len(out)] = token_start_idx
  170. out.append(token)
  171. token = ""
  172. else:
  173. if not token:
  174. token_start_idx = data_idx
  175. token += data[data_idx]
  176. data_idx += 1
  177. if token:
  178. mapping[len(out)] = token_start_idx
  179. out.append(token)
  180. mapping[len(out)] = len(data)
  181. mapping[len(out) + 1] = len(data) + 1
  182. return out, mapping
  183. def process_quoted_token(self, data_idx, data):
  184. token = ""
  185. c = data[data_idx]
  186. i = data_idx
  187. for start, end, escape, incl_quotes in self.quote_chars:
  188. if c == start:
  189. if incl_quotes:
  190. token += c
  191. i += 1
  192. while data[i] != end:
  193. if data[i] == escape:
  194. if incl_quotes:
  195. token += data[i]
  196. i += 1
  197. if len(data) == i: # if there are no more chars
  198. raise LogicalExpressionException(
  199. None,
  200. "End of input reached. "
  201. "Escape character [%s] found at end." % escape,
  202. )
  203. token += data[i]
  204. else:
  205. token += data[i]
  206. i += 1
  207. if len(data) == i:
  208. raise LogicalExpressionException(
  209. None, "End of input reached. " "Expected: [%s]" % end
  210. )
  211. if incl_quotes:
  212. token += data[i]
  213. i += 1
  214. if not token:
  215. raise LogicalExpressionException(None, "Empty quoted token found")
  216. break
  217. return token, i
  218. def get_all_symbols(self):
  219. """This method exists to be overridden"""
  220. return Tokens.SYMBOLS
  221. def inRange(self, location):
  222. """Return TRUE if the given location is within the buffer"""
  223. return self._currentIndex + location < len(self._buffer)
  224. def token(self, location=None):
  225. """Get the next waiting token. If a location is given, then
  226. return the token at currentIndex+location without advancing
  227. currentIndex; setting it gives lookahead/lookback capability."""
  228. try:
  229. if location is None:
  230. tok = self._buffer[self._currentIndex]
  231. self._currentIndex += 1
  232. else:
  233. tok = self._buffer[self._currentIndex + location]
  234. return tok
  235. except IndexError:
  236. raise ExpectedMoreTokensException(self._currentIndex + 1)
  237. def isvariable(self, tok):
  238. return tok not in Tokens.TOKENS
  239. def process_next_expression(self, context):
  240. """Parse the next complete expression from the stream and return it."""
  241. try:
  242. tok = self.token()
  243. except ExpectedMoreTokensException:
  244. raise ExpectedMoreTokensException(
  245. self._currentIndex + 1, message="Expression expected."
  246. )
  247. accum = self.handle(tok, context)
  248. if not accum:
  249. raise UnexpectedTokenException(
  250. self._currentIndex, tok, message="Expression expected."
  251. )
  252. return self.attempt_adjuncts(accum, context)
  253. def handle(self, tok, context):
  254. """This method is intended to be overridden for logics that
  255. use different operators or expressions"""
  256. if self.isvariable(tok):
  257. return self.handle_variable(tok, context)
  258. elif tok in Tokens.NOT_LIST:
  259. return self.handle_negation(tok, context)
  260. elif tok in Tokens.LAMBDA_LIST:
  261. return self.handle_lambda(tok, context)
  262. elif tok in Tokens.QUANTS:
  263. return self.handle_quant(tok, context)
  264. elif tok == Tokens.OPEN:
  265. return self.handle_open(tok, context)
  266. def attempt_adjuncts(self, expression, context):
  267. cur_idx = None
  268. while cur_idx != self._currentIndex: # while adjuncts are added
  269. cur_idx = self._currentIndex
  270. expression = self.attempt_EqualityExpression(expression, context)
  271. expression = self.attempt_ApplicationExpression(expression, context)
  272. expression = self.attempt_BooleanExpression(expression, context)
  273. return expression
  274. def handle_negation(self, tok, context):
  275. return self.make_NegatedExpression(self.process_next_expression(Tokens.NOT))
  276. def make_NegatedExpression(self, expression):
  277. return NegatedExpression(expression)
  278. def handle_variable(self, tok, context):
  279. # It's either: 1) a predicate expression: sees(x,y)
  280. # 2) an application expression: P(x)
  281. # 3) a solo variable: john OR x
  282. accum = self.make_VariableExpression(tok)
  283. if self.inRange(0) and self.token(0) == Tokens.OPEN:
  284. # The predicate has arguments
  285. if not isinstance(accum, FunctionVariableExpression) and not isinstance(
  286. accum, ConstantExpression
  287. ):
  288. raise LogicalExpressionException(
  289. self._currentIndex,
  290. "'%s' is an illegal predicate name. "
  291. "Individual variables may not be used as "
  292. "predicates." % tok,
  293. )
  294. self.token() # swallow the Open Paren
  295. # curry the arguments
  296. accum = self.make_ApplicationExpression(
  297. accum, self.process_next_expression(APP)
  298. )
  299. while self.inRange(0) and self.token(0) == Tokens.COMMA:
  300. self.token() # swallow the comma
  301. accum = self.make_ApplicationExpression(
  302. accum, self.process_next_expression(APP)
  303. )
  304. self.assertNextToken(Tokens.CLOSE)
  305. return accum
  306. def get_next_token_variable(self, description):
  307. try:
  308. tok = self.token()
  309. except ExpectedMoreTokensException as e:
  310. raise ExpectedMoreTokensException(e.index, "Variable expected.")
  311. if isinstance(self.make_VariableExpression(tok), ConstantExpression):
  312. raise LogicalExpressionException(
  313. self._currentIndex,
  314. "'%s' is an illegal variable name. "
  315. "Constants may not be %s." % (tok, description),
  316. )
  317. return Variable(tok)
  318. def handle_lambda(self, tok, context):
  319. # Expression is a lambda expression
  320. if not self.inRange(0):
  321. raise ExpectedMoreTokensException(
  322. self._currentIndex + 2,
  323. message="Variable and Expression expected following lambda operator.",
  324. )
  325. vars = [self.get_next_token_variable("abstracted")]
  326. while True:
  327. if not self.inRange(0) or (
  328. self.token(0) == Tokens.DOT and not self.inRange(1)
  329. ):
  330. raise ExpectedMoreTokensException(
  331. self._currentIndex + 2, message="Expression expected."
  332. )
  333. if not self.isvariable(self.token(0)):
  334. break
  335. # Support expressions like: \x y.M == \x.\y.M
  336. vars.append(self.get_next_token_variable("abstracted"))
  337. if self.inRange(0) and self.token(0) == Tokens.DOT:
  338. self.token() # swallow the dot
  339. accum = self.process_next_expression(tok)
  340. while vars:
  341. accum = self.make_LambdaExpression(vars.pop(), accum)
  342. return accum
  343. def handle_quant(self, tok, context):
  344. # Expression is a quantified expression: some x.M
  345. factory = self.get_QuantifiedExpression_factory(tok)
  346. if not self.inRange(0):
  347. raise ExpectedMoreTokensException(
  348. self._currentIndex + 2,
  349. message="Variable and Expression expected following quantifier '%s'."
  350. % tok,
  351. )
  352. vars = [self.get_next_token_variable("quantified")]
  353. while True:
  354. if not self.inRange(0) or (
  355. self.token(0) == Tokens.DOT and not self.inRange(1)
  356. ):
  357. raise ExpectedMoreTokensException(
  358. self._currentIndex + 2, message="Expression expected."
  359. )
  360. if not self.isvariable(self.token(0)):
  361. break
  362. # Support expressions like: some x y.M == some x.some y.M
  363. vars.append(self.get_next_token_variable("quantified"))
  364. if self.inRange(0) and self.token(0) == Tokens.DOT:
  365. self.token() # swallow the dot
  366. accum = self.process_next_expression(tok)
  367. while vars:
  368. accum = self.make_QuanifiedExpression(factory, vars.pop(), accum)
  369. return accum
  370. def get_QuantifiedExpression_factory(self, tok):
  371. """This method serves as a hook for other logic parsers that
  372. have different quantifiers"""
  373. if tok in Tokens.EXISTS_LIST:
  374. return ExistsExpression
  375. elif tok in Tokens.ALL_LIST:
  376. return AllExpression
  377. else:
  378. self.assertToken(tok, Tokens.QUANTS)
  379. def make_QuanifiedExpression(self, factory, variable, term):
  380. return factory(variable, term)
  381. def handle_open(self, tok, context):
  382. # Expression is in parens
  383. accum = self.process_next_expression(None)
  384. self.assertNextToken(Tokens.CLOSE)
  385. return accum
  386. def attempt_EqualityExpression(self, expression, context):
  387. """Attempt to make an equality expression. If the next token is an
  388. equality operator, then an EqualityExpression will be returned.
  389. Otherwise, the parameter will be returned."""
  390. if self.inRange(0):
  391. tok = self.token(0)
  392. if tok in Tokens.EQ_LIST + Tokens.NEQ_LIST and self.has_priority(
  393. tok, context
  394. ):
  395. self.token() # swallow the "=" or "!="
  396. expression = self.make_EqualityExpression(
  397. expression, self.process_next_expression(tok)
  398. )
  399. if tok in Tokens.NEQ_LIST:
  400. expression = self.make_NegatedExpression(expression)
  401. return expression
  402. def make_EqualityExpression(self, first, second):
  403. """This method serves as a hook for other logic parsers that
  404. have different equality expression classes"""
  405. return EqualityExpression(first, second)
  406. def attempt_BooleanExpression(self, expression, context):
  407. """Attempt to make a boolean expression. If the next token is a boolean
  408. operator, then a BooleanExpression will be returned. Otherwise, the
  409. parameter will be returned."""
  410. while self.inRange(0):
  411. tok = self.token(0)
  412. factory = self.get_BooleanExpression_factory(tok)
  413. if factory and self.has_priority(tok, context):
  414. self.token() # swallow the operator
  415. expression = self.make_BooleanExpression(
  416. factory, expression, self.process_next_expression(tok)
  417. )
  418. else:
  419. break
  420. return expression
  421. def get_BooleanExpression_factory(self, tok):
  422. """This method serves as a hook for other logic parsers that
  423. have different boolean operators"""
  424. if tok in Tokens.AND_LIST:
  425. return AndExpression
  426. elif tok in Tokens.OR_LIST:
  427. return OrExpression
  428. elif tok in Tokens.IMP_LIST:
  429. return ImpExpression
  430. elif tok in Tokens.IFF_LIST:
  431. return IffExpression
  432. else:
  433. return None
  434. def make_BooleanExpression(self, factory, first, second):
  435. return factory(first, second)
  436. def attempt_ApplicationExpression(self, expression, context):
  437. """Attempt to make an application expression. The next tokens are
  438. a list of arguments in parens, then the argument expression is a
  439. function being applied to the arguments. Otherwise, return the
  440. argument expression."""
  441. if self.has_priority(APP, context):
  442. if self.inRange(0) and self.token(0) == Tokens.OPEN:
  443. if (
  444. not isinstance(expression, LambdaExpression)
  445. and not isinstance(expression, ApplicationExpression)
  446. and not isinstance(expression, FunctionVariableExpression)
  447. and not isinstance(expression, ConstantExpression)
  448. ):
  449. raise LogicalExpressionException(
  450. self._currentIndex,
  451. ("The function '%s" % expression)
  452. + "' is not a Lambda Expression, an "
  453. "Application Expression, or a "
  454. "functional predicate, so it may "
  455. "not take arguments.",
  456. )
  457. self.token() # swallow then open paren
  458. # curry the arguments
  459. accum = self.make_ApplicationExpression(
  460. expression, self.process_next_expression(APP)
  461. )
  462. while self.inRange(0) and self.token(0) == Tokens.COMMA:
  463. self.token() # swallow the comma
  464. accum = self.make_ApplicationExpression(
  465. accum, self.process_next_expression(APP)
  466. )
  467. self.assertNextToken(Tokens.CLOSE)
  468. return accum
  469. return expression
  470. def make_ApplicationExpression(self, function, argument):
  471. return ApplicationExpression(function, argument)
  472. def make_VariableExpression(self, name):
  473. return VariableExpression(Variable(name))
  474. def make_LambdaExpression(self, variable, term):
  475. return LambdaExpression(variable, term)
  476. def has_priority(self, operation, context):
  477. return self.operator_precedence[operation] < self.operator_precedence[
  478. context
  479. ] or (
  480. operation in self.right_associated_operations
  481. and self.operator_precedence[operation] == self.operator_precedence[context]
  482. )
  483. def assertNextToken(self, expected):
  484. try:
  485. tok = self.token()
  486. except ExpectedMoreTokensException as e:
  487. raise ExpectedMoreTokensException(
  488. e.index, message="Expected token '%s'." % expected
  489. )
  490. if isinstance(expected, list):
  491. if tok not in expected:
  492. raise UnexpectedTokenException(self._currentIndex, tok, expected)
  493. else:
  494. if tok != expected:
  495. raise UnexpectedTokenException(self._currentIndex, tok, expected)
  496. def assertToken(self, tok, expected):
  497. if isinstance(expected, list):
  498. if tok not in expected:
  499. raise UnexpectedTokenException(self._currentIndex, tok, expected)
  500. else:
  501. if tok != expected:
  502. raise UnexpectedTokenException(self._currentIndex, tok, expected)
  503. def __repr__(self):
  504. if self.inRange(0):
  505. msg = "Next token: " + self.token(0)
  506. else:
  507. msg = "No more tokens"
  508. return "<" + self.__class__.__name__ + ": " + msg + ">"
  509. def read_logic(s, logic_parser=None, encoding=None):
  510. """
  511. Convert a file of First Order Formulas into a list of {Expression}s.
  512. :param s: the contents of the file
  513. :type s: str
  514. :param logic_parser: The parser to be used to parse the logical expression
  515. :type logic_parser: LogicParser
  516. :param encoding: the encoding of the input string, if it is binary
  517. :type encoding: str
  518. :return: a list of parsed formulas.
  519. :rtype: list(Expression)
  520. """
  521. if encoding is not None:
  522. s = s.decode(encoding)
  523. if logic_parser is None:
  524. logic_parser = LogicParser()
  525. statements = []
  526. for linenum, line in enumerate(s.splitlines()):
  527. line = line.strip()
  528. if line.startswith("#") or line == "":
  529. continue
  530. try:
  531. statements.append(logic_parser.parse(line))
  532. except LogicalExpressionException:
  533. raise ValueError("Unable to parse line %s: %s" % (linenum, line))
  534. return statements
  535. @total_ordering
  536. class Variable(object):
  537. def __init__(self, name):
  538. """
  539. :param name: the name of the variable
  540. """
  541. assert isinstance(name, str), "%s is not a string" % name
  542. self.name = name
  543. def __eq__(self, other):
  544. return isinstance(other, Variable) and self.name == other.name
  545. def __ne__(self, other):
  546. return not self == other
  547. def __lt__(self, other):
  548. if not isinstance(other, Variable):
  549. raise TypeError
  550. return self.name < other.name
  551. def substitute_bindings(self, bindings):
  552. return bindings.get(self, self)
  553. def __hash__(self):
  554. return hash(self.name)
  555. def __str__(self):
  556. return self.name
  557. def __repr__(self):
  558. return "Variable('%s')" % self.name
  559. def unique_variable(pattern=None, ignore=None):
  560. """
  561. Return a new, unique variable.
  562. :param pattern: ``Variable`` that is being replaced. The new variable must
  563. be the same type.
  564. :param term: a set of ``Variable`` objects that should not be returned from
  565. this function.
  566. :rtype: Variable
  567. """
  568. if pattern is not None:
  569. if is_indvar(pattern.name):
  570. prefix = "z"
  571. elif is_funcvar(pattern.name):
  572. prefix = "F"
  573. elif is_eventvar(pattern.name):
  574. prefix = "e0"
  575. else:
  576. assert False, "Cannot generate a unique constant"
  577. else:
  578. prefix = "z"
  579. v = Variable("%s%s" % (prefix, _counter.get()))
  580. while ignore is not None and v in ignore:
  581. v = Variable("%s%s" % (prefix, _counter.get()))
  582. return v
  583. def skolem_function(univ_scope=None):
  584. """
  585. Return a skolem function over the variables in univ_scope
  586. param univ_scope
  587. """
  588. skolem = VariableExpression(Variable("F%s" % _counter.get()))
  589. if univ_scope:
  590. for v in list(univ_scope):
  591. skolem = skolem(VariableExpression(v))
  592. return skolem
  593. class Type(object):
  594. def __repr__(self):
  595. return "%s" % self
  596. def __hash__(self):
  597. return hash("%s" % self)
  598. @classmethod
  599. def fromstring(cls, s):
  600. return read_type(s)
  601. class ComplexType(Type):
  602. def __init__(self, first, second):
  603. assert isinstance(first, Type), "%s is not a Type" % first
  604. assert isinstance(second, Type), "%s is not a Type" % second
  605. self.first = first
  606. self.second = second
  607. def __eq__(self, other):
  608. return (
  609. isinstance(other, ComplexType)
  610. and self.first == other.first
  611. and self.second == other.second
  612. )
  613. def __ne__(self, other):
  614. return not self == other
  615. __hash__ = Type.__hash__
  616. def matches(self, other):
  617. if isinstance(other, ComplexType):
  618. return self.first.matches(other.first) and self.second.matches(other.second)
  619. else:
  620. return self == ANY_TYPE
  621. def resolve(self, other):
  622. if other == ANY_TYPE:
  623. return self
  624. elif isinstance(other, ComplexType):
  625. f = self.first.resolve(other.first)
  626. s = self.second.resolve(other.second)
  627. if f and s:
  628. return ComplexType(f, s)
  629. else:
  630. return None
  631. elif self == ANY_TYPE:
  632. return other
  633. else:
  634. return None
  635. def __str__(self):
  636. if self == ANY_TYPE:
  637. return "%s" % ANY_TYPE
  638. else:
  639. return "<%s,%s>" % (self.first, self.second)
  640. def str(self):
  641. if self == ANY_TYPE:
  642. return ANY_TYPE.str()
  643. else:
  644. return "(%s -> %s)" % (self.first.str(), self.second.str())
  645. class BasicType(Type):
  646. def __eq__(self, other):
  647. return isinstance(other, BasicType) and ("%s" % self) == ("%s" % other)
  648. def __ne__(self, other):
  649. return not self == other
  650. __hash__ = Type.__hash__
  651. def matches(self, other):
  652. return other == ANY_TYPE or self == other
  653. def resolve(self, other):
  654. if self.matches(other):
  655. return self
  656. else:
  657. return None
  658. class EntityType(BasicType):
  659. def __str__(self):
  660. return "e"
  661. def str(self):
  662. return "IND"
  663. class TruthValueType(BasicType):
  664. def __str__(self):
  665. return "t"
  666. def str(self):
  667. return "BOOL"
  668. class EventType(BasicType):
  669. def __str__(self):
  670. return "v"
  671. def str(self):
  672. return "EVENT"
  673. class AnyType(BasicType, ComplexType):
  674. def __init__(self):
  675. pass
  676. @property
  677. def first(self):
  678. return self
  679. @property
  680. def second(self):
  681. return self
  682. def __eq__(self, other):
  683. return isinstance(other, AnyType) or other.__eq__(self)
  684. def __ne__(self, other):
  685. return not self == other
  686. __hash__ = Type.__hash__
  687. def matches(self, other):
  688. return True
  689. def resolve(self, other):
  690. return other
  691. def __str__(self):
  692. return "?"
  693. def str(self):
  694. return "ANY"
  695. TRUTH_TYPE = TruthValueType()
  696. ENTITY_TYPE = EntityType()
  697. EVENT_TYPE = EventType()
  698. ANY_TYPE = AnyType()
  699. def read_type(type_string):
  700. assert isinstance(type_string, str)
  701. type_string = type_string.replace(" ", "") # remove spaces
  702. if type_string[0] == "<":
  703. assert type_string[-1] == ">"
  704. paren_count = 0
  705. for i, char in enumerate(type_string):
  706. if char == "<":
  707. paren_count += 1
  708. elif char == ">":
  709. paren_count -= 1
  710. assert paren_count > 0
  711. elif char == ",":
  712. if paren_count == 1:
  713. break
  714. return ComplexType(
  715. read_type(type_string[1:i]), read_type(type_string[i + 1 : -1])
  716. )
  717. elif type_string[0] == "%s" % ENTITY_TYPE:
  718. return ENTITY_TYPE
  719. elif type_string[0] == "%s" % TRUTH_TYPE:
  720. return TRUTH_TYPE
  721. elif type_string[0] == "%s" % ANY_TYPE:
  722. return ANY_TYPE
  723. else:
  724. raise LogicalExpressionException(
  725. None, "Unexpected character: '%s'." % type_string[0]
  726. )
  727. class TypeException(Exception):
  728. def __init__(self, msg):
  729. super(TypeException, self).__init__(msg)
  730. class InconsistentTypeHierarchyException(TypeException):
  731. def __init__(self, variable, expression=None):
  732. if expression:
  733. msg = (
  734. "The variable '%s' was found in multiple places with different"
  735. " types in '%s'." % (variable, expression)
  736. )
  737. else:
  738. msg = (
  739. "The variable '%s' was found in multiple places with different"
  740. " types." % (variable)
  741. )
  742. super(InconsistentTypeHierarchyException, self).__init__(msg)
  743. class TypeResolutionException(TypeException):
  744. def __init__(self, expression, other_type):
  745. super(TypeResolutionException, self).__init__(
  746. "The type of '%s', '%s', cannot be resolved with type '%s'"
  747. % (expression, expression.type, other_type)
  748. )
  749. class IllegalTypeException(TypeException):
  750. def __init__(self, expression, other_type, allowed_type):
  751. super(IllegalTypeException, self).__init__(
  752. "Cannot set type of %s '%s' to '%s'; must match type '%s'."
  753. % (expression.__class__.__name__, expression, other_type, allowed_type)
  754. )
  755. def typecheck(expressions, signature=None):
  756. """
  757. Ensure correct typing across a collection of ``Expression`` objects.
  758. :param expressions: a collection of expressions
  759. :param signature: dict that maps variable names to types (or string
  760. representations of types)
  761. """
  762. # typecheck and create master signature
  763. for expression in expressions:
  764. signature = expression.typecheck(signature)
  765. # apply master signature to all expressions
  766. for expression in expressions[:-1]:
  767. expression.typecheck(signature)
  768. return signature
  769. class SubstituteBindingsI(object):
  770. """
  771. An interface for classes that can perform substitutions for
  772. variables.
  773. """
  774. def substitute_bindings(self, bindings):
  775. """
  776. :return: The object that is obtained by replacing
  777. each variable bound by ``bindings`` with its values.
  778. Aliases are already resolved. (maybe?)
  779. :rtype: (any)
  780. """
  781. raise NotImplementedError()
  782. def variables(self):
  783. """
  784. :return: A list of all variables in this object.
  785. """
  786. raise NotImplementedError()
  787. class Expression(SubstituteBindingsI):
  788. """This is the base abstract object for all logical expressions"""
  789. _logic_parser = LogicParser()
  790. _type_checking_logic_parser = LogicParser(type_check=True)
  791. @classmethod
  792. def fromstring(cls, s, type_check=False, signature=None):
  793. if type_check:
  794. return cls._type_checking_logic_parser.parse(s, signature)
  795. else:
  796. return cls._logic_parser.parse(s, signature)
  797. def __call__(self, other, *additional):
  798. accum = self.applyto(other)
  799. for a in additional:
  800. accum = accum(a)
  801. return accum
  802. def applyto(self, other):
  803. assert isinstance(other, Expression), "%s is not an Expression" % other
  804. return ApplicationExpression(self, other)
  805. def __neg__(self):
  806. return NegatedExpression(self)
  807. def negate(self):
  808. """If this is a negated expression, remove the negation.
  809. Otherwise add a negation."""
  810. return -self
  811. def __and__(self, other):
  812. if not isinstance(other, Expression):
  813. raise TypeError("%s is not an Expression" % other)
  814. return AndExpression(self, other)
  815. def __or__(self, other):
  816. if not isinstance(other, Expression):
  817. raise TypeError("%s is not an Expression" % other)
  818. return OrExpression(self, other)
  819. def __gt__(self, other):
  820. if not isinstance(other, Expression):
  821. raise TypeError("%s is not an Expression" % other)
  822. return ImpExpression(self, other)
  823. def __lt__(self, other):
  824. if not isinstance(other, Expression):
  825. raise TypeError("%s is not an Expression" % other)
  826. return IffExpression(self, other)
  827. def __eq__(self, other):
  828. raise NotImplementedError()
  829. def __ne__(self, other):
  830. return not self == other
  831. def equiv(self, other, prover=None):
  832. """
  833. Check for logical equivalence.
  834. Pass the expression (self <-> other) to the theorem prover.
  835. If the prover says it is valid, then the self and other are equal.
  836. :param other: an ``Expression`` to check equality against
  837. :param prover: a ``nltk.inference.api.Prover``
  838. """
  839. assert isinstance(other, Expression), "%s is not an Expression" % other
  840. if prover is None:
  841. from nltk.inference import Prover9
  842. prover = Prover9()
  843. bicond = IffExpression(self.simplify(), other.simplify())
  844. return prover.prove(bicond)
  845. def __hash__(self):
  846. return hash(repr(self))
  847. def substitute_bindings(self, bindings):
  848. expr = self
  849. for var in expr.variables():
  850. if var in bindings:
  851. val = bindings[var]
  852. if isinstance(val, Variable):
  853. val = self.make_VariableExpression(val)
  854. elif not isinstance(val, Expression):
  855. raise ValueError(
  856. "Can not substitute a non-expression "
  857. "value into an expression: %r" % (val,)
  858. )
  859. # Substitute bindings in the target value.
  860. val = val.substitute_bindings(bindings)
  861. # Replace var w/ the target value.
  862. expr = expr.replace(var, val)
  863. return expr.simplify()
  864. def typecheck(self, signature=None):
  865. """
  866. Infer and check types. Raise exceptions if necessary.
  867. :param signature: dict that maps variable names to types (or string
  868. representations of types)
  869. :return: the signature, plus any additional type mappings
  870. """
  871. sig = defaultdict(list)
  872. if signature:
  873. for key in signature:
  874. val = signature[key]
  875. varEx = VariableExpression(Variable(key))
  876. if isinstance(val, Type):
  877. varEx.type = val
  878. else:
  879. varEx.type = read_type(val)
  880. sig[key].append(varEx)
  881. self._set_type(signature=sig)
  882. return dict((key, sig[key][0].type) for key in sig)
  883. def findtype(self, variable):
  884. """
  885. Find the type of the given variable as it is used in this expression.
  886. For example, finding the type of "P" in "P(x) & Q(x,y)" yields "<e,t>"
  887. :param variable: Variable
  888. """
  889. raise NotImplementedError()
  890. def _set_type(self, other_type=ANY_TYPE, signature=None):
  891. """
  892. Set the type of this expression to be the given type. Raise type
  893. exceptions where applicable.
  894. :param other_type: Type
  895. :param signature: dict(str -> list(AbstractVariableExpression))
  896. """
  897. raise NotImplementedError()
  898. def replace(self, variable, expression, replace_bound=False, alpha_convert=True):
  899. """
  900. Replace every instance of 'variable' with 'expression'
  901. :param variable: ``Variable`` The variable to replace
  902. :param expression: ``Expression`` The expression with which to replace it
  903. :param replace_bound: bool Should bound variables be replaced?
  904. :param alpha_convert: bool Alpha convert automatically to avoid name clashes?
  905. """
  906. assert isinstance(variable, Variable), "%s is not a Variable" % variable
  907. assert isinstance(expression, Expression), (
  908. "%s is not an Expression" % expression
  909. )
  910. return self.visit_structured(
  911. lambda e: e.replace(variable, expression, replace_bound, alpha_convert),
  912. self.__class__,
  913. )
  914. def normalize(self, newvars=None):
  915. """Rename auto-generated unique variables"""
  916. def get_indiv_vars(e):
  917. if isinstance(e, IndividualVariableExpression):
  918. return set([e])
  919. elif isinstance(e, AbstractVariableExpression):
  920. return set()
  921. else:
  922. return e.visit(
  923. get_indiv_vars, lambda parts: reduce(operator.or_, parts, set())
  924. )
  925. result = self
  926. for i, e in enumerate(sorted(get_indiv_vars(self), key=lambda e: e.variable)):
  927. if isinstance(e, EventVariableExpression):
  928. newVar = e.__class__(Variable("e0%s" % (i + 1)))
  929. elif isinstance(e, IndividualVariableExpression):
  930. newVar = e.__class__(Variable("z%s" % (i + 1)))
  931. else:
  932. newVar = e
  933. result = result.replace(e.variable, newVar, True)
  934. return result
  935. def visit(self, function, combinator):
  936. """
  937. Recursively visit subexpressions. Apply 'function' to each
  938. subexpression and pass the result of each function application
  939. to the 'combinator' for aggregation:
  940. return combinator(map(function, self.subexpressions))
  941. Bound variables are neither applied upon by the function nor given to
  942. the combinator.
  943. :param function: ``Function<Expression,T>`` to call on each subexpression
  944. :param combinator: ``Function<list<T>,R>`` to combine the results of the
  945. function calls
  946. :return: result of combination ``R``
  947. """
  948. raise NotImplementedError()
  949. def visit_structured(self, function, combinator):
  950. """
  951. Recursively visit subexpressions. Apply 'function' to each
  952. subexpression and pass the result of each function application
  953. to the 'combinator' for aggregation. The combinator must have
  954. the same signature as the constructor. The function is not
  955. applied to bound variables, but they are passed to the
  956. combinator.
  957. :param function: ``Function`` to call on each subexpression
  958. :param combinator: ``Function`` with the same signature as the
  959. constructor, to combine the results of the function calls
  960. :return: result of combination
  961. """
  962. return self.visit(function, lambda parts: combinator(*parts))
  963. def __repr__(self):
  964. return "<%s %s>" % (self.__class__.__name__, self)
  965. def __str__(self):
  966. return self.str()
  967. def variables(self):
  968. """
  969. Return a set of all the variables for binding substitution.
  970. The variables returned include all free (non-bound) individual
  971. variables and any variable starting with '?' or '@'.
  972. :return: set of ``Variable`` objects
  973. """
  974. return self.free() | set(
  975. p for p in self.predicates() | self.constants() if re.match("^[?@]", p.name)
  976. )
  977. def free(self):
  978. """
  979. Return a set of all the free (non-bound) variables. This includes
  980. both individual and predicate variables, but not constants.
  981. :return: set of ``Variable`` objects
  982. """
  983. return self.visit(
  984. lambda e: e.free(), lambda parts: reduce(operator.or_, parts, set())
  985. )
  986. def constants(self):
  987. """
  988. Return a set of individual constants (non-predicates).
  989. :return: set of ``Variable`` objects
  990. """
  991. return self.visit(
  992. lambda e: e.constants(), lambda parts: reduce(operator.or_, parts, set())
  993. )
  994. def predicates(self):
  995. """
  996. Return a set of predicates (constants, not variables).
  997. :return: set of ``Variable`` objects
  998. """
  999. return self.visit(
  1000. lambda e: e.predicates(), lambda parts: reduce(operator.or_, parts, set())
  1001. )
  1002. def simplify(self):
  1003. """
  1004. :return: beta-converted version of this expression
  1005. """
  1006. return self.visit_structured(lambda e: e.simplify(), self.__class__)
  1007. def make_VariableExpression(self, variable):
  1008. return VariableExpression(variable)
  1009. class ApplicationExpression(Expression):
  1010. r"""
  1011. This class is used to represent two related types of logical expressions.
  1012. The first is a Predicate Expression, such as "P(x,y)". A predicate
  1013. expression is comprised of a ``FunctionVariableExpression`` or
  1014. ``ConstantExpression`` as the predicate and a list of Expressions as the
  1015. arguments.
  1016. The second is a an application of one expression to another, such as
  1017. "(\x.dog(x))(fido)".
  1018. The reason Predicate Expressions are treated as Application Expressions is
  1019. that the Variable Expression predicate of the expression may be replaced
  1020. with another Expression, such as a LambdaExpression, which would mean that
  1021. the Predicate should be thought of as being applied to the arguments.
  1022. The logical expression reader will always curry arguments in a application expression.
  1023. So, "\x y.see(x,y)(john,mary)" will be represented internally as
  1024. "((\x y.(see(x))(y))(john))(mary)". This simplifies the internals since
  1025. there will always be exactly one argument in an application.
  1026. The str() method will usually print the curried forms of application
  1027. expressions. The one exception is when the the application expression is
  1028. really a predicate expression (ie, underlying function is an
  1029. ``AbstractVariableExpression``). This means that the example from above
  1030. will be returned as "(\x y.see(x,y)(john))(mary)".
  1031. """
  1032. def __init__(self, function, argument):
  1033. """
  1034. :param function: ``Expression``, for the function expression
  1035. :param argument: ``Expression``, for the argument
  1036. """
  1037. assert isinstance(function, Expression), "%s is not an Expression" % function
  1038. assert isinstance(argument, Expression), "%s is not an Expression" % argument
  1039. self.function = function
  1040. self.argument = argument
  1041. def simplify(self):
  1042. function = self.function.simplify()
  1043. argument = self.argument.simplify()
  1044. if isinstance(function, LambdaExpression):
  1045. return function.term.replace(function.variable, argument).simplify()
  1046. else:
  1047. return self.__class__(function, argument)
  1048. @property
  1049. def type(self):
  1050. if isinstance(self.function.type, ComplexType):
  1051. return self.function.type.second
  1052. else:
  1053. return ANY_TYPE
  1054. def _set_type(self, other_type=ANY_TYPE, signature=None):
  1055. """:see Expression._set_type()"""
  1056. assert isinstance(other_type, Type)
  1057. if signature is None:
  1058. signature = defaultdict(list)
  1059. self.argument._set_type(ANY_TYPE, signature)
  1060. try:
  1061. self.function._set_type(
  1062. ComplexType(self.argument.type, other_type), signature
  1063. )
  1064. except TypeResolutionException:
  1065. raise TypeException(
  1066. "The function '%s' is of type '%s' and cannot be applied "
  1067. "to '%s' of type '%s'. Its argument must match type '%s'."
  1068. % (
  1069. self.function,
  1070. self.function.type,
  1071. self.argument,
  1072. self.argument.type,
  1073. self.function.type.first,
  1074. )
  1075. )
  1076. def findtype(self, variable):
  1077. """:see Expression.findtype()"""
  1078. assert isinstance(variable, Variable), "%s is not a Variable" % variable
  1079. if self.is_atom():
  1080. function, args = self.uncurry()
  1081. else:
  1082. # It's not a predicate expression ("P(x,y)"), so leave args curried
  1083. function = self.function
  1084. args = [self.argument]
  1085. found = [arg.findtype(variable) for arg in [function] + args]
  1086. unique = []
  1087. for f in found:
  1088. if f != ANY_TYPE:
  1089. if unique:
  1090. for u in unique:
  1091. if f.matches(u):
  1092. break
  1093. else:
  1094. unique.append(f)
  1095. if len(unique) == 1:
  1096. return list(unique)[0]
  1097. else:
  1098. return ANY_TYPE
  1099. def constants(self):
  1100. """:see: Expression.constants()"""
  1101. if isinstance(self.function, AbstractVariableExpression):
  1102. function_constants = set()
  1103. else:
  1104. function_constants = self.function.constants()
  1105. return function_constants | self.argument.constants()
  1106. def predicates(self):
  1107. """:see: Expression.predicates()"""
  1108. if isinstance(self.function, ConstantExpression):
  1109. function_preds = set([self.function.variable])
  1110. else:
  1111. function_preds = self.function.predicates()
  1112. return function_preds | self.argument.predicates()
  1113. def visit(self, function, combinator):
  1114. """:see: Expression.visit()"""
  1115. return combinator([function(self.function), function(self.argument)])
  1116. def __eq__(self, other):
  1117. return (
  1118. isinstance(other, ApplicationExpression)
  1119. and self.function == other.function
  1120. and self.argument == other.argument
  1121. )
  1122. def __ne__(self, other):
  1123. return not self == other
  1124. __hash__ = Expression.__hash__
  1125. def __str__(self):
  1126. # uncurry the arguments and find the base function
  1127. if self.is_atom():
  1128. function, args = self.uncurry()
  1129. arg_str = ",".join("%s" % arg for arg in args)
  1130. else:
  1131. # Leave arguments curried
  1132. function = self.function
  1133. arg_str = "%s" % self.argument
  1134. function_str = "%s" % function
  1135. parenthesize_function = False
  1136. if isinstance(function, LambdaExpression):
  1137. if isinstance(function.term, ApplicationExpression):
  1138. if not isinstance(function.term.function, AbstractVariableExpression):
  1139. parenthesize_function = True
  1140. elif not isinstance(function.term, BooleanExpression):
  1141. parenthesize_function = True
  1142. elif isinstance(function, ApplicationExpression):
  1143. parenthesize_function = True
  1144. if parenthesize_function:
  1145. function_str = Tokens.OPEN + function_str + Tokens.CLOSE
  1146. return function_str + Tokens.OPEN + arg_str + Tokens.CLOSE
  1147. def uncurry(self):
  1148. """
  1149. Uncurry this application expression
  1150. return: A tuple (base-function, arg-list)
  1151. """
  1152. function = self.function
  1153. args = [self.argument]
  1154. while isinstance(function, ApplicationExpression):
  1155. # (\x.\y.sees(x,y)(john))(mary)
  1156. args.insert(0, function.argument)
  1157. function = function.function
  1158. return (function, args)
  1159. @property
  1160. def pred(self):
  1161. """
  1162. Return uncurried base-function.
  1163. If this is an atom, then the result will be a variable expression.
  1164. Otherwise, it will be a lambda expression.
  1165. """
  1166. return self.uncurry()[0]
  1167. @property
  1168. def args(self):
  1169. """
  1170. Return uncurried arg-list
  1171. """
  1172. return self.uncurry()[1]
  1173. def is_atom(self):
  1174. """
  1175. Is this expression an atom (as opposed to a lambda expression applied
  1176. to a term)?
  1177. """
  1178. return isinstance(self.pred, AbstractVariableExpression)
  1179. @total_ordering
  1180. class AbstractVariableExpression(Expression):
  1181. """This class represents a variable to be used as a predicate or entity"""
  1182. def __init__(self, variable):
  1183. """
  1184. :param variable: ``Variable``, for the variable
  1185. """
  1186. assert isinstance(variable, Variable), "%s is not a Variable" % variable
  1187. self.variable = variable
  1188. def simplify(self):
  1189. return self
  1190. def replace(self, variable, expression, replace_bound=False, alpha_convert=True):
  1191. """:see: Expression.replace()"""
  1192. assert isinstance(variable, Variable), "%s is not an Variable" % variable
  1193. assert isinstance(expression, Expression), (
  1194. "%s is not an Expression" % expression
  1195. )
  1196. if self.variable == variable:
  1197. return expression
  1198. else:
  1199. return self
  1200. def _set_type(self, other_type=ANY_TYPE, signature=None):
  1201. """:see Expression._set_type()"""
  1202. assert isinstance(other_type, Type)
  1203. if signature is None:
  1204. signature = defaultdict(list)
  1205. resolution = other_type
  1206. for varEx in signature[self.variable.name]:
  1207. resolution = varEx.type.resolve(resolution)
  1208. if not resolution:
  1209. raise InconsistentTypeHierarchyException(self)
  1210. signature[self.variable.name].append(self)
  1211. for varEx in signature[self.variable.name]:
  1212. varEx.type = resolution
  1213. def findtype(self, variable):
  1214. """:see Expression.findtype()"""
  1215. assert isinstance(variable, Variable), "%s is not a Variable" % variable
  1216. if self.variable == variable:
  1217. return self.type
  1218. else:
  1219. return ANY_TYPE
  1220. def predicates(self):
  1221. """:see: Expression.predicates()"""
  1222. return set()
  1223. def __eq__(self, other):
  1224. """Allow equality between instances of ``AbstractVariableExpression``
  1225. subtypes."""
  1226. return (
  1227. isinstance(other, AbstractVariableExpression)
  1228. and self.variable == other.variable
  1229. )
  1230. def __ne__(self, other):
  1231. return not self == other
  1232. def __lt__(self, other):
  1233. if not isinstance(other, AbstractVariableExpression):
  1234. raise TypeError
  1235. return self.variable < other.variable
  1236. __hash__ = Expression.__hash__
  1237. def __str__(self):
  1238. return "%s" % self.variable
  1239. class IndividualVariableExpression(AbstractVariableExpression):
  1240. """This class represents variables that take the form of a single lowercase
  1241. character (other than 'e') followed by zero or more digits."""
  1242. def _set_type(self, other_type=ANY_TYPE, signature=None):
  1243. """:see Expression._set_type()"""
  1244. assert isinstance(other_type, Type)
  1245. if signature is None:
  1246. signature = defaultdict(list)
  1247. if not other_type.matches(ENTITY_TYPE):
  1248. raise IllegalTypeException(self, other_type, ENTITY_TYPE)
  1249. signature[self.variable.name].append(self)
  1250. def _get_type(self):
  1251. return ENTITY_TYPE
  1252. type = property(_get_type, _set_type)
  1253. def free(self):
  1254. """:see: Expression.free()"""
  1255. return set([self.variable])
  1256. def constants(self):
  1257. """:see: Expression.constants()"""
  1258. return set()
  1259. class FunctionVariableExpression(AbstractVariableExpression):
  1260. """This class represents variables that take the form of a single uppercase
  1261. character followed by zero or more digits."""
  1262. type = ANY_TYPE
  1263. def free(self):
  1264. """:see: Expression.free()"""
  1265. return set([self.variable])
  1266. def constants(self):
  1267. """:see: Expression.constants()"""
  1268. return set()
  1269. class EventVariableExpression(IndividualVariableExpression):
  1270. """This class represents variables that take the form of a single lowercase
  1271. 'e' character followed by zero or more digits."""
  1272. type = EVENT_TYPE
  1273. class ConstantExpression(AbstractVariableExpression):
  1274. """This class represents variables that do not take the form of a single
  1275. character followed by zero or more digits."""
  1276. type = ENTITY_TYPE
  1277. def _set_type(self, other_type=ANY_TYPE, signature=None):
  1278. """:see Expression._set_type()"""
  1279. assert isinstance(other_type, Type)
  1280. if signature is None:
  1281. signature = defaultdict(list)
  1282. if other_type == ANY_TYPE:
  1283. # entity type by default, for individuals
  1284. resolution = ENTITY_TYPE
  1285. else:
  1286. resolution = other_type
  1287. if self.type != ENTITY_TYPE:
  1288. resolution = resolution.resolve(self.type)
  1289. for varEx in signature[self.variable.name]:
  1290. resolution = varEx.type.resolve(resolution)
  1291. if not resolution:
  1292. raise InconsistentTypeHierarchyException(self)
  1293. signature[self.variable.name].append(self)
  1294. for varEx in signature[self.variable.name]:
  1295. varEx.type = resolution
  1296. def free(self):
  1297. """:see: Expression.free()"""
  1298. return set()
  1299. def constants(self):
  1300. """:see: Expression.constants()"""
  1301. return set([self.variable])
  1302. def VariableExpression(variable):
  1303. """
  1304. This is a factory method that instantiates and returns a subtype of
  1305. ``AbstractVariableExpression`` appropriate for the given variable.
  1306. """
  1307. assert isinstance(variable, Variable), "%s is not a Variable" % variable
  1308. if is_indvar(variable.name):
  1309. return IndividualVariableExpression(variable)
  1310. elif is_funcvar(variable.name):
  1311. return FunctionVariableExpression(variable)
  1312. elif is_eventvar(variable.name):
  1313. return EventVariableExpression(variable)
  1314. else:
  1315. return ConstantExpression(variable)
  1316. class VariableBinderExpression(Expression):
  1317. """This an abstract class for any Expression that binds a variable in an
  1318. Expression. This includes LambdaExpressions and Quantified Expressions"""
  1319. def __init__(self, variable, term):
  1320. """
  1321. :param variable: ``Variable``, for the variable
  1322. :param term: ``Expression``, for the term
  1323. """
  1324. assert isinstance(variable, Variable), "%s is not a Variable" % variable
  1325. assert isinstance(term, Expression), "%s is not an Expression" % term
  1326. self.variable = variable
  1327. self.term = term
  1328. def replace(self, variable, expression, replace_bound=False, alpha_convert=True):
  1329. """:see: Expression.replace()"""
  1330. assert isinstance(variable, Variable), "%s is not a Variable" % variable
  1331. assert isinstance(expression, Expression), (
  1332. "%s is not an Expression" % expression
  1333. )
  1334. # if the bound variable is the thing being replaced
  1335. if self.variable == variable:
  1336. if replace_bound:
  1337. assert isinstance(expression, AbstractVariableExpression), (
  1338. "%s is not a AbstractVariableExpression" % expression
  1339. )
  1340. return self.__class__(
  1341. expression.variable,
  1342. self.term.replace(variable, expression, True, alpha_convert),
  1343. )
  1344. else:
  1345. return self
  1346. else:
  1347. # if the bound variable appears in the expression, then it must
  1348. # be alpha converted to avoid a conflict
  1349. if alpha_convert and self.variable in expression.free():
  1350. self = self.alpha_convert(unique_variable(pattern=self.variable))
  1351. # replace in the term
  1352. return self.__class__(
  1353. self.variable,
  1354. self.term.replace(variable, expression, replace_bound, alpha_convert),
  1355. )
  1356. def alpha_convert(self, newvar):
  1357. """Rename all occurrences of the variable introduced by this variable
  1358. binder in the expression to ``newvar``.
  1359. :param newvar: ``Variable``, for the new variable
  1360. """
  1361. assert isinstance(newvar, Variable), "%s is not a Variable" % newvar
  1362. return self.__class__(
  1363. newvar, self.term.replace(self.variable, VariableExpression(newvar), True)
  1364. )
  1365. def free(self):
  1366. """:see: Expression.free()"""
  1367. return self.term.free() - set([self.variable])
  1368. def findtype(self, variable):
  1369. """:see Expression.findtype()"""
  1370. assert isinstance(variable, Variable), "%s is not a Variable" % variable
  1371. if variable == self.variable:
  1372. return ANY_TYPE
  1373. else:
  1374. return self.term.findtype(variable)
  1375. def visit(self, function, combinator):
  1376. """:see: Expression.visit()"""
  1377. return combinator([function(self.term)])
  1378. def visit_structured(self, function, combinator):
  1379. """:see: Expression.visit_structured()"""
  1380. return combinator(self.variable, function(self.term))
  1381. def __eq__(self, other):
  1382. r"""Defines equality modulo alphabetic variance. If we are comparing
  1383. \x.M and \y.N, then check equality of M and N[x/y]."""
  1384. if isinstance(self, other.__class__) or isinstance(other, self.__class__):
  1385. if self.variable == other.variable:
  1386. return self.term == other.term
  1387. else:
  1388. # Comparing \x.M and \y.N. Relabel y in N with x and continue.
  1389. varex = VariableExpression(self.variable)
  1390. return self.term == other.term.replace(other.variable, varex)
  1391. else:
  1392. return False
  1393. def __ne__(self, other):
  1394. return not self == other
  1395. __hash__ = Expression.__hash__
  1396. class LambdaExpression(VariableBinderExpression):
  1397. @property
  1398. def type(self):
  1399. return ComplexType(self.term.findtype(self.variable), self.term.type)
  1400. def _set_type(self, other_type=ANY_TYPE, signature=None):
  1401. """:see Expression._set_type()"""
  1402. assert isinstance(other_type, Type)
  1403. if signature is None:
  1404. signature = defaultdict(list)
  1405. self.term._set_type(other_type.second, signature)
  1406. if not self.type.resolve(other_type):
  1407. raise TypeResolutionException(self, other_type)
  1408. def __str__(self):
  1409. variables = [self.variable]
  1410. term = self.term
  1411. while term.__class__ == self.__class__:
  1412. variables.append(term.variable)
  1413. term = term.term
  1414. return (
  1415. Tokens.LAMBDA
  1416. + " ".join("%s" % v for v in variables)
  1417. + Tokens.DOT
  1418. + "%s" % term
  1419. )
  1420. class QuantifiedExpression(VariableBinderExpression):
  1421. @property
  1422. def type(self):
  1423. return TRUTH_TYPE
  1424. def _set_type(self, other_type=ANY_TYPE, signature=None):
  1425. """:see Expression._set_type()"""
  1426. assert isinstance(other_type, Type)
  1427. if signature is None:
  1428. signature = defaultdict(list)
  1429. if not other_type.matches(TRUTH_TYPE):
  1430. raise IllegalTypeException(self, other_type, TRUTH_TYPE)
  1431. self.term._set_type(TRUTH_TYPE, signature)
  1432. def __str__(self):
  1433. variables = [self.variable]
  1434. term = self.term
  1435. while term.__class__ == self.__class__:
  1436. variables.append(term.variable)
  1437. term = term.term
  1438. return (
  1439. self.getQuantifier()
  1440. + " "
  1441. + " ".join("%s" % v for v in variables)
  1442. + Tokens.DOT
  1443. + "%s" % term
  1444. )
  1445. class ExistsExpression(QuantifiedExpression):
  1446. def getQuantifier(self):
  1447. return Tokens.EXISTS
  1448. class AllExpression(QuantifiedExpression):
  1449. def getQuantifier(self):
  1450. return Tokens.ALL
  1451. class NegatedExpression(Expression):
  1452. def __init__(self, term):
  1453. assert isinstance(term, Expression), "%s is not an Expression" % term
  1454. self.term = term
  1455. @property
  1456. def type(self):
  1457. return TRUTH_TYPE
  1458. def _set_type(self, other_type=ANY_TYPE, signature=None):
  1459. """:see Expression._set_type()"""
  1460. assert isinstance(other_type, Type)
  1461. if signature is None:
  1462. signature = defaultdict(list)
  1463. if not other_type.matches(TRUTH_TYPE):
  1464. raise IllegalTypeException(self, other_type, TRUTH_TYPE)
  1465. self.term._set_type(TRUTH_TYPE, signature)
  1466. def findtype(self, variable):
  1467. assert isinstance(variable, Variable), "%s is not a Variable" % variable
  1468. return self.term.findtype(variable)
  1469. def visit(self, function, combinator):
  1470. """:see: Expression.visit()"""
  1471. return combinator([function(self.term)])
  1472. def negate(self):
  1473. """:see: Expression.negate()"""
  1474. return self.term
  1475. def __eq__(self, other):
  1476. return isinstance(other, NegatedExpression) and self.term == other.term
  1477. def __ne__(self, other):
  1478. return not self == other
  1479. __hash__ = Expression.__hash__
  1480. def __str__(self):
  1481. return Tokens.NOT + "%s" % self.term
  1482. class BinaryExpression(Expression):
  1483. def __init__(self, first, second):
  1484. assert isinstance(first, Expression), "%s is not an Expression" % first
  1485. assert isinstance(second, Expression), "%s is not an Expression" % second
  1486. self.first = first
  1487. self.second = second
  1488. @property
  1489. def type(self):
  1490. return TRUTH_TYPE
  1491. def findtype(self, variable):
  1492. """:see Expression.findtype()"""
  1493. assert isinstance(variable, Variable), "%s is not a Variable" % variable
  1494. f = self.first.findtype(variable)
  1495. s = self.second.findtype(variable)
  1496. if f == s or s == ANY_TYPE:
  1497. return f
  1498. elif f == ANY_TYPE:
  1499. return s
  1500. else:
  1501. return ANY_TYPE
  1502. def visit(self, function, combinator):
  1503. """:see: Expression.visit()"""
  1504. return combinator([function(self.first), function(self.second)])
  1505. def __eq__(self, other):
  1506. return (
  1507. (isinstance(self, other.__class__) or isinstance(other, self.__class__))
  1508. and self.first == other.first
  1509. and self.second == other.second
  1510. )
  1511. def __ne__(self, other):
  1512. return not self == other
  1513. __hash__ = Expression.__hash__
  1514. def __str__(self):
  1515. first = self._str_subex(self.first)
  1516. second = self._str_subex(self.second)
  1517. return Tokens.OPEN + first + " " + self.getOp() + " " + second + Tokens.CLOSE
  1518. def _str_subex(self, subex):
  1519. return "%s" % subex
  1520. class BooleanExpression(BinaryExpression):
  1521. def _set_type(self, other_type=ANY_TYPE, signature=None):
  1522. """:see Expression._set_type()"""
  1523. assert isinstance(other_type, Type)
  1524. if signature is None:
  1525. signature = defaultdict(list)
  1526. if not other_type.matches(TRUTH_TYPE):
  1527. raise IllegalTypeException(self, other_type, TRUTH_TYPE)
  1528. self.first._set_type(TRUTH_TYPE, signature)
  1529. self.second._set_type(TRUTH_TYPE, signature)
  1530. class AndExpression(BooleanExpression):
  1531. """This class represents conjunctions"""
  1532. def getOp(self):
  1533. return Tokens.AND
  1534. def _str_subex(self, subex):
  1535. s = "%s" % subex
  1536. if isinstance(subex, AndExpression):
  1537. return s[1:-1]
  1538. return s
  1539. class OrExpression(BooleanExpression):
  1540. """This class represents disjunctions"""
  1541. def getOp(self):
  1542. return Tokens.OR
  1543. def _str_subex(self, subex):
  1544. s = "%s" % subex
  1545. if isinstance(subex, OrExpression):
  1546. return s[1:-1]
  1547. return s
  1548. class ImpExpression(BooleanExpression):
  1549. """This class represents implications"""
  1550. def getOp(self):
  1551. return Tokens.IMP
  1552. class IffExpression(BooleanExpression):
  1553. """This class represents biconditionals"""
  1554. def getOp(self):
  1555. return Tokens.IFF
  1556. class EqualityExpression(BinaryExpression):
  1557. """This class represents equality expressions like "(x = y)"."""
  1558. def _set_type(self, other_type=ANY_TYPE, signature=None):
  1559. """:see Expression._set_type()"""
  1560. assert isinstance(other_type, Type)
  1561. if signature is None:
  1562. signature = defaultdict(list)
  1563. if not other_type.matches(TRUTH_TYPE):
  1564. raise IllegalTypeException(self, other_type, TRUTH_TYPE)
  1565. self.first._set_type(ENTITY_TYPE, signature)
  1566. self.second._set_type(ENTITY_TYPE, signature)
  1567. def getOp(self):
  1568. return Tokens.EQ
  1569. ### Utilities
  1570. class LogicalExpressionException(Exception):
  1571. def __init__(self, index, message):
  1572. self.index = index
  1573. Exception.__init__(self, message)
  1574. class UnexpectedTokenException(LogicalExpressionException):
  1575. def __init__(self, index, unexpected=None, expected=None, message=None):
  1576. if unexpected and expected:
  1577. msg = "Unexpected token: '%s'. " "Expected token '%s'." % (
  1578. unexpected,
  1579. expected,
  1580. )
  1581. elif unexpected:
  1582. msg = "Unexpected token: '%s'." % unexpected
  1583. if message:
  1584. msg += " " + message
  1585. else:
  1586. msg = "Expected token '%s'." % expected
  1587. LogicalExpressionException.__init__(self, index, msg)
  1588. class ExpectedMoreTokensException(LogicalExpressionException):
  1589. def __init__(self, index, message=None):
  1590. if not message:
  1591. message = "More tokens expected."
  1592. LogicalExpressionException.__init__(
  1593. self, index, "End of input found. " + message
  1594. )
  1595. def is_indvar(expr):
  1596. """
  1597. An individual variable must be a single lowercase character other than 'e',
  1598. followed by zero or more digits.
  1599. :param expr: str
  1600. :return: bool True if expr is of the correct form
  1601. """
  1602. assert isinstance(expr, str), "%s is not a string" % expr
  1603. return re.match(r"^[a-df-z]\d*$", expr) is not None
  1604. def is_funcvar(expr):
  1605. """
  1606. A function variable must be a single uppercase character followed by
  1607. zero or more digits.
  1608. :param expr: str
  1609. :return: bool True if expr is of the correct form
  1610. """
  1611. assert isinstance(expr, str), "%s is not a string" % expr
  1612. return re.match(r"^[A-Z]\d*$", expr) is not None
  1613. def is_eventvar(expr):
  1614. """
  1615. An event variable must be a single lowercase 'e' character followed by
  1616. zero or more digits.
  1617. :param expr: str
  1618. :return: bool True if expr is of the correct form
  1619. """
  1620. assert isinstance(expr, str), "%s is not a string" % expr
  1621. return re.match(r"^e\d*$", expr) is not None
  1622. def demo():
  1623. lexpr = Expression.fromstring
  1624. print("=" * 20 + "Test reader" + "=" * 20)
  1625. print(lexpr(r"john"))
  1626. print(lexpr(r"man(x)"))
  1627. print(lexpr(r"-man(x)"))
  1628. print(lexpr(r"(man(x) & tall(x) & walks(x))"))
  1629. print(lexpr(r"exists x.(man(x) & tall(x) & walks(x))"))
  1630. print(lexpr(r"\x.man(x)"))
  1631. print(lexpr(r"\x.man(x)(john)"))
  1632. print(lexpr(r"\x y.sees(x,y)"))
  1633. print(lexpr(r"\x y.sees(x,y)(a,b)"))
  1634. print(lexpr(r"(\x.exists y.walks(x,y))(x)"))
  1635. print(lexpr(r"exists x.x = y"))
  1636. print(lexpr(r"exists x.(x = y)"))
  1637. print(lexpr("P(x) & x=y & P(y)"))
  1638. print(lexpr(r"\P Q.exists x.(P(x) & Q(x))"))
  1639. print(lexpr(r"man(x) <-> tall(x)"))
  1640. print("=" * 20 + "Test simplify" + "=" * 20)
  1641. print(lexpr(r"\x.\y.sees(x,y)(john)(mary)").simplify())
  1642. print(lexpr(r"\x.\y.sees(x,y)(john, mary)").simplify())
  1643. print(lexpr(r"all x.(man(x) & (\x.exists y.walks(x,y))(x))").simplify())
  1644. print(lexpr(r"(\P.\Q.exists x.(P(x) & Q(x)))(\x.dog(x))(\x.bark(x))").simplify())
  1645. print("=" * 20 + "Test alpha conversion and binder expression equality" + "=" * 20)
  1646. e1 = lexpr("exists x.P(x)")
  1647. print(e1)
  1648. e2 = e1.alpha_convert(Variable("z"))
  1649. print(e2)
  1650. print(e1 == e2)
  1651. def demo_errors():
  1652. print("=" * 20 + "Test reader errors" + "=" * 20)
  1653. demoException("(P(x) & Q(x)")
  1654. demoException("((P(x) &) & Q(x))")
  1655. demoException("P(x) -> ")
  1656. demoException("P(x")
  1657. demoException("P(x,")
  1658. demoException("P(x,)")
  1659. demoException("exists")
  1660. demoException("exists x.")
  1661. demoException("\\")
  1662. demoException("\\ x y.")
  1663. demoException("P(x)Q(x)")
  1664. demoException("(P(x)Q(x)")
  1665. demoException("exists x -> y")
  1666. def demoException(s):
  1667. try:
  1668. Expression.fromstring(s)
  1669. except LogicalExpressionException as e:
  1670. print("%s: %s" % (e.__class__.__name__, e))
  1671. def printtype(ex):
  1672. print("%s : %s" % (ex.str(), ex.type))
  1673. if __name__ == "__main__":
  1674. demo()
  1675. # demo_errors()