-
Notifications
You must be signed in to change notification settings - Fork 0
/
first_order_logic_parse.py
1131 lines (1027 loc) · 41.2 KB
/
first_order_logic_parse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is imported by first_order_logic_parser.ipynb.
#region Tokenization comments
#### Constants
# emptyset, infty
#### Function symbols
# binary infix function symbols:
# precedence 1: +, -
# precedence 2: *, /, %
# precedence 3: ^
# unary prefix function symbols:
# precedence 1: -
# unary postfix function symbols:
# precedence 4: !, ', ^#, ^+, ^-, ^*, ^o, ^inv
# all other functions are n-ary prefix function symbols with n >= 1
# Certain symbols can be found in multiple categories. In such instances,
# the symbol's type and arity are initially assigned during tokenization
# and later confirmed during parsing.
# The appearance of a symbol can vary depending on its type. For example,
# the symbol "*" is typically displayed as an infix operator. However,
# when used as a postfix operator, it is rendered as a superscript.
#### Predicate symbols
# binary infix predicate symbols: (precedences are all the same)
# !=, <, <=, >, >=
# in, nin, subseteq, nsubseteq, subsetneqq,
# supseteq, nsupseteq, supsetneqq, divides, ndivides,
# sim, simeq, cong, equiv, approx
# all other predicates are n-ary prefix predicate symbols with n >= 0
#### Rendering
# We will use LaTeX for rendering. Once a formula is parsed, it is natural
# to render it in polish notation or RPN(reverse polish notation). But for
# human readers, infix notation is more natural. So we need to convert
# the parsed tree back into infix notation---but this time we use LaTeXed
# token values. Or we draw a tree diagram for the parsed tree(AST), in which
# the nodes are labeled with the LaTeXed token values.
#### Precedence
# Precedences are used to determine the order of parsing and/or evaluation.
# The higher the precedence, the earlier the parsing and/or evaluation.
# The precedence of a token have meaning only among tokens of the same type
# as follows: Parenthesis > Function > Predicate > Quantifiers > Connectives
#### Associativity
# Syntactic associativity is used to determine the order of parsing.
# Left associativity means a op b op c = (a op b) op c.
# Right associativity means a op b op c = a op (b op c).
# Most operators are left associative.
# Right associativity is used for exponentiation(function) and
# implication(connective). Unary prefix operators are right associative too
# but this is trivial because they are always followed by a term.
# Semantic associativity is not relevant in parsing.
# But it is relevant in rendering. For instance, you don't need parentheses
# in A and B and C when rendering, although it is parsed as (A and B) and C
# by the default left associativity.
# On the other hand, A and B or C is a legal expression but should be
# rendered as (A and B) or C because it is parsed this way by the default
# left associativity.
#endregion
import re
class Token:
CONSTS = [ "emptyset", "infty" ]
OPER_PRE = [ "-" ]
OPER_POST = [ "!", "'", "^#", "^+", "^-", "^*", "^o", "^inv" ]
OPER_IN_1 = [ "+", "-", "cap", "cup", "oplus" ]
OPER_IN_2 = [ "*", "/", "%", "times", "div", "otimes", "cdot" ]
OPER_IN_2N = [ "/", "%", "div" ]
OPER_IN_3 = [ "^" ]
PRED_IN = [ "!=", "<", "<=", ">", ">=", "nless", "nleq", "ngtr", "ngeq",
"divides", "in", "nin", "subseteq",
"nsubseteq", "subsetneqq", "supseteq", "nsupseteq",
"supsetneqq", "divides", "ndivides", "sim", "simeq",
"cong", "equiv", "approx" ]
RESERVED_WORDS = set(CONSTS + OPER_PRE + OPER_POST + OPER_IN_1 +
OPER_IN_2 + OPER_IN_3 + PRED_IN)
SPECIAL_CHARS = "-!'^#+*/%=<>()[]{},"
FMLA_TOKENS = ("pred_pre", "pred_in", "equality", "prop_letter",
'conn_0ary')
# an expression is a formula iff it has a token in FMLA_TOKENS
# A member of FMLA_TOKENS is precisely the root of a prime formula.
FMLA_ROOTS = FMLA_TOKENS + ("conn_1ary", "conn_2ary", "conn_arrow",
"quantifier", "var_determiner")
# a parsed node is a formula iff it has a token in FMLA_ROOTS
NON_PRIME_ROOTS = ("conn_1ary", "conn_2ary", "conn_arrow")
PRIME_ROOTS = ("pred_pre", "pred_in", "equality", "prop_letter",
"quantifier", "conn_0ary")
def __init__(self, value):
CONSTS = self.CONSTS
OPER_PRE = self.OPER_PRE
OPER_POST = self.OPER_POST
OPER_IN_1 = self.OPER_IN_1
OPER_IN_2 = self.OPER_IN_2
OPER_IN_3 = self.OPER_IN_3
PRED_IN = self.PRED_IN
self.value = value # a string
self.token_type = None
self.arity = None
self.precedence = None
# reserved words (equality, connectives, quantifiers, parentheses, comma)
if value == "=":
self.token_type = 'equality'
self.arity = 2
elif value in ("imp", "iff", "xor"):
self.token_type = 'conn_arrow'
self.arity = 2
self.precedence = 1
elif value in ("and", "or"):
self.token_type = 'conn_2ary'
self.arity = 2
self.precedence = 2
elif value == "not":
self.token_type = "conn_1ary"
self.arity = 1
self.precedence = 3
elif value == 'bot':
self.token_type = 'conn_0ary'
self.arity = 0
self.precedence = 9
elif value in ("forall", "exists"):
self.token_type = 'quantifier'
self.arity = 1
self.precedence = 4
elif value == "(":
self.token_type = 'lparen'
elif value == ")":
self.token_type = 'rparen'
elif value == ",":
self.token_type = 'comma'
# reserved words (constants)
elif value in CONSTS:
self.token_type = 'const'
self.precedence = 9
# reserved words (function symbols)
elif value in OPER_IN_1:
self.token_type = 'oper_in_1'
self.arity = 2
self.precedence = 1
elif value in OPER_IN_2:
self.token_type = 'oper_in_2'
self.arity = 2
self.precedence = 2
elif value in OPER_IN_3:
self.token_type = 'oper_in_3'
self.arity = 2
self.precedence = 3
elif value in OPER_PRE: # this won't be used
# 'oper_pre' type is set in the parser
self.token_type = 'oper_pre' # unary '-', ..
self.arity = 1
self.precedence = 1
elif value in OPER_POST:
self.token_type = 'oper_post'
self.arity = 1
self.precedence = 4
# reserved words (predicate symbols)
elif value in PRED_IN:
self.token_type = 'pred_in'
self.arity = 2
else:
# numeral, variable, constant, func_pre, pred_pre
len_s = len(value)
if self.isnumeral(value):
self.token_type = 'numeral'
self.precedence = 9
elif value[0] in "uvw" + "xyz" + "ijk" + "lmn":
# we use concatenation to avoid the stupid cSpell warning
if (len_s==1 or
(len_s >= 3 and value[1]=='_'
and Token.isword(value[2:], "decimal"))):
self.token_type = 'var'
self.precedence = 9
else:
raise ValueError(f"'{value}' is invalid variable symbol (Token)")
elif value[0] in "abcde" :
if len_s==1 or Token.isword(value[1:]):
self.token_type = 'const'
self.precedence = 9
else:
raise ValueError(f"'{value}' is invalid constant symbol (Token)")
elif value[0] in "fgh":
if len_s==1 or Token.isword(value[1:]):
self.token_type = 'func_pre'
self.arity = self.get_arity(value)
else:
raise ValueError(f"'{value}' is invalid function symbol (Token)")
elif Token.isword(value, "upper"):
if len_s==1 or Token.isword(value[1:]):
self.arity = self.get_arity(value)
self.token_type = 'pred_pre' if self.arity > 0 else 'prop_letter'
else:
raise ValueError(f"'{value}' is invalid predicate symbol (Token)")
else:
raise ValueError(f"'{value}' is invalid (Token)")
@staticmethod
def isnumeral(s: str) -> bool:
# str is assumed to be isascii().
return s.isdecimal() and (len(s) == 1 or s[0]!='0')
@staticmethod
def isword(s: str, opt: str="all") -> bool:
if(not s.replace('_','').isalnum()):
return False
if(opt=="alpha"):
return s[0].isalpha()
elif(opt=="lower"):
return s[0].islower()
elif(opt=="upper"):
return s[0].isupper()
elif(opt=="decimal"):
return s.isdecimal()
elif(opt=="numeral"):
return Token.isnumeral(s)
else: # opt=="all"
return True
@staticmethod
def get_arity(value: str) -> int:
#region cmt
# Get arity of a function symbol (starts with [fgh])
# or a predicate symbol (starts with [A-Z]).
# If the last character is a decimal digit, and if it does not follow
# the underscore character, then it is the arity.
# Otherwise, if the first character is [fgh], it is 1.
# Otherwise, it is 0. (prop_letter)
# Arity 0 is not allowed for function symbols because it is
# reserved for constants.
# Arity cannot exceed 9 unless it is declared explicitly in
# other ways.
# Arities are not rendered because it can be inferred from
# the number of arguments.
#endregion cmt
if (not (d := value[-1]).isdecimal()
or (len(value) >= 2 and value[-2] == '_')):
if value[0] in "fgh":
return 1 # unary function
elif value[0].isupper():
return 0 # propositional letter
else:
raise ValueError(
f"'{value}' is invalid in get_arity(), " +
"function or predicate symbol expected"
)
else:
if value[0] in "fgh" and d=="0":
raise ValueError(
f"'{value}' is invalid, 0-ary function not allowed (Token)"
)
else:
return int(d)
def __str__(self):
if self.arity is not None:
s_arity = f", arity={self.arity}"
else:
s_arity = ""
if self.precedence is not None:
s_precedence = f", precedence={self.precedence}"
else:
s_precedence = ""
return f"{self.value} ({self.token_type}{s_arity}{s_precedence})"
#region token class helper
def tokenizer(input_text):
import re
tokens = []
# split the input text into a list of tokens at word boundaries and
# whitespaces then remove empty strings and strip off leading and
# trailing whitespaces
li = [s.strip() for s in re.split(r"\b|\s", input_text, re.ASCII)
if s.strip()]
for s in li: # s is a string
if not s.isascii():
raise ValueError(f"'{s}' is invalid (non-ASCII)")
if not (set(s).issubset(Token.SPECIAL_CHARS) or
Token.isnumeral(s) or Token.isword(s)):
raise ValueError(f"'{s}' is invalid (illegal character)")
if set(s).issubset(Token.SPECIAL_CHARS) and len(s) > 1:
# split string of consecutive special chars into
# individual characters or !=, <=, >=, ^*, ^+, ^-, ^#
for c in s: # c is a special char
if c == "=" and tokens and tokens[-1].value in "!<>":
token1 = tokens.pop()
token1.value += c
tokens.append(Token(token1.value))
elif (c in ("*", "+", "-", "#") and
tokens and tokens[-1].value=="^"):
token1 = tokens.pop()
token1.value += c
tokens.append(Token(token1.value))
else:
tokens.append(Token(c))
elif s in ("o", "inv"):
# '^o' and '^inv' are postfix unary function symbols
if tokens and tokens[-1].value=="^":
token1 = tokens.pop()
token1.value += s
tokens.append(Token(token1.value))
else:
tokens.append(Token(s))
else:
tokens.append(Token(s))
return tokens
def testTokenizer(input_text):
try:
tokens = tokenizer(input_text)
except ValueError as e:
print(f"Tokenizer: {e}")
else:
for t in tokens:
print(t)
def print_in_chunk(li, chunk_size=5): # li is any iterable
for i, s in enumerate(li):
print(s, end=" " if i % chunk_size != chunk_size-1 else "\n")
#endregion token class helper
#region Comment
# <formula> ::= { <comp_fmla1> "imp" } <comp_fmla1> |
# <comp_fmla1> { ( "iff" | "xor") <comp_fmla1> }
# <comp_fmla1> ::= <comp_fmla2> { ("and" | "or") <comp_fmla2> }
# <comp_fmla2> ::= { ("not" | <determiner>) }
# ( '(' <formula> ')' | <atom> | "bot" )
# <determiner> ::= <quantifier> <var>
# <quantifier> ::= "forall" | "exists"
# <atom> ::= <prop_letter> | <pred_pre> "(" <term> {',' <term>} ")" |
# <term> <pred_in> <term>
# <term> ::= (<term1> | <nterm1>) { <oper_in_1> <term1> }
# <nterm1> ::= <oper_pre> { <oper_pre> } <term1>
# <term1> ::= <factor> { <oper_in_2> <factor> }
# <factor> ::= { <factor_exp> <oper_in_3> } <factor_exp>
# <factor_exp> ::= <factor_postfix> { <oper_postfix> }
# <factor_postfix> ::= "(" <term> ")" | <func_call> | <identifier>
# <func_call> ::= <func_pre> '(' <term> {',' <term>} ')'
# <identifier> ::= <const> | <numeral> | <var>
# # oper_in_1, oper_pre, oper_in_2, oper_in_3, oper_post,
# # func_pre, const, numeral, var are defined in the Token class.
#endregion
class Node:
from typing import List
import copy
LATEX_DICT = dict(
[("not", r"\neg"), ("and", r"\wedge"), ("or", r"\vee"),
("imp", r"\rightarrow"), ("iff", r"\leftrightarrow"),
("xor", r"\nleftrightarrow"), ("nin", r"\not\in"), ("bot", r"\bot"),
("emptyset", r"\varnothing"), ("^o", r"^{\circ}"), ("^inv", r"^{-1}"),
("^#", r"^\#"), ("%", r"\%"), ("<=", r"\le"), (">=", r"\ge"),
("divides", r"\,\vert\,"), ("ndivides", r"\;\vert\mskip-14mu\not\;\;"),
("forall", r"\forall"), ("exists", r"\exists")])
#region Comment
# Other than the above and '^', for all reserved tokens, just use
# ("token.value", r"\token.value") for the mapping.
# Special care is needed for '^'. See the build_infix_formula()
# method below.
# For user-defined tokens, use the static method ident2latex(opt).
#endregion
@staticmethod
def token2latex(token: Token, opt: str='latex') -> str:
# for oper_*, pred_in (declared in Token class)
label = token.value
if opt == 'latex':
if (latex_str := Node.LATEX_DICT.get(label)):
return latex_str
elif label.isalnum():
return f"\\{label}"
else:
return label
else: # opt == 'text'
return label
@staticmethod
def ident2latex(token: Token, opt: str='latex') -> str:
#region Comment
# for const, var, func_pre, pred_pre, prop_letter, numeral
# Identifier means the token.value when token.token_type is "var",
# "const", "numeral", "func_pre", "pred_pre".
# All but the last occurrence of an underscore in the string
# are escaped with a backslash.
# Identifier string is romanized except the end substrings after
# the last underscore, which are subscripted with _{}.
# If the last character is a decimal and the previous char is not
# underscore, then it is not rendered because it is interpreted as
# the arity of the symbol.
#endregion
label = token.value
if opt == 'latex':
pos_underscore = label.rfind('_')
if (label[-1].isdecimal() and
token.token_type in ("func_pre", "pred_pre") and
(pos_underscore < 0 or pos_underscore != len(label)-2)):
label = label[:-1] # chop-off the last character
if pos_underscore >= 0: # underscore exists in the identifier
str1 = label[:pos_underscore]
str2 = label[pos_underscore+1:]
subscript = r"_{" + str2 + r"}" if str2 else ""
else:
str1 = label
subscript = ""
if len(str1) > 1 and not str1.isdecimal():
left_str = r"{\rm " + str1.replace("_", r"\_") + r"}"
else:
left_str = str1
return left_str + subscript
else: # opt == 'text'
return label
@staticmethod
def display_latex_li(str_li: List[str]) -> None:
from IPython.display import display, Math
node_li = [parse_text(s) for s in str_li]
latex_str_li = [node.build_infix('latex') for node in node_li]
latex_str = ',\\: '.join(latex_str_li)
display(Math('$[\\,' + latex_str + '\\,]$'))
def __init__(self, token, children=None):
self.token = token # the node is labeled with a Token object
self.children = children if children else [] # list of Node objects
self.type = ('formula' if self.token.token_type in Token.FMLA_ROOTS
else 'term')
self.index = -1 # 0,1,2,.. for truth tree
self.bValue = -1 # 0,1 for truth tree
self.level = -1 # 0,1,2,.. for truth tree
self.alt_str = '' # P_1, P_1, .. for truth tree
def __str__(self):
return self.build_polish_notation()
def __eq__(self, other):
infix_self = self.build_infix('text')
infix_other = other.build_infix('text')
return infix_self == infix_other
def build_polish_notation(self, verbose=False) -> str:
ret_str = f"{self.token}" if verbose else f"{self.token.value}"
if self.children:
ret_str += ' '
ret_str += ' '.join(child.build_polish_notation(verbose)
for child in self.children)
return ret_str
def build_RPN(self, verbose=False) -> str:
ret_str = ''
if self.children:
ret_str += ' '.join(child.build_RPN(verbose)
for child in self.children) + ' '
ret_str += f"{self.token}" if verbose else f"{self.token.value}"
return ret_str
def build_infix(self, opt: str='latex') -> str:
if self.type == 'term':
return self.build_infix_term(opt)
else: # self.type == 'formula'
return self.build_infix_formula(opt)
def build_infix_term(self, opt: str) -> str:
LATEX_DICT = self.LATEX_DICT
if not self.children: # leaf node ::= variable | const | numeral
return self.ident2latex(self.token, opt)
else: # non-leaf node
# token_type ::= func_pre | oper_in_1 | oper_in_2 | oper_in_3 |
# oper_pre | oper_post
ret_str = ''
if self.token.token_type == 'func_pre':
label = self.ident2latex(self.token, opt)
args = ', '.join(kid.build_infix(opt) for kid in self.children)
ret_str += label + '(' + args + ')'
else: # token is an operator with various arities and precedences
if self.token.precedence == 1:
# oper_pre(unary) or oper_in_1(binary, +, -, cap, cup, oplus)
if self.token.token_type == 'oper_pre':
kid1 = self.children[0]
kid1_str = kid1.build_infix(opt)
if kid1.token.precedence == 1:
kid1_str = '(' + kid1_str + ')'
# else pass
ret_str += self.token2latex(self.token, opt) + kid1_str
else: # oper_in_1
kid1, kid2 = self.children
kid1_str = kid1.build_infix(opt)
kid2_str = kid2.build_infix(opt)
if ((self.token.value in Token.OPER_PRE and kid2.token.precedence == 1) or
kid2.token.token_type == 'oper_pre'):
kid2_str = '(' + kid2_str + ')'
# else pass
ret_str += kid1_str + ' ' + self.token2latex(self.token, opt) + ' ' + kid2_str
elif self.token.precedence == 2: # oper_in_2(binary, *, /, %, ...)
kid1, kid2 = self.children
kid1_str = kid1.build_infix(opt)
kid2_str = kid2.build_infix(opt)
# determine if parentheses are needed
if (kid2.token.precedence < self.token.precedence or
# '/', '%', 'div' are non-associative
(kid2.token.precedence == self.token.precedence and
self.token.value in Token.OPER_IN_2N)):
kid2_str = '(' + kid2_str + ')'
if kid1.token.precedence < self.token.precedence:
kid1_str = '(' + kid1_str + ')'
ret_str += kid1_str + ' ' + self.token2latex(self.token, opt) + \
' ' + kid2_str
elif self.token.precedence == 3: # oper_in_3(binary, ^ exponentiation)
kid1, kid2 = self.children
kid1_str = kid1.build_infix(opt)
kid2_str = kid2.build_infix(opt)
# determine if parentheses are needed
if kid1.token.precedence <= 4:
# '^' is right-associative, and we want parentheses in (a')^2
kid1_str = '(' + kid1_str + ')'
if kid2.token.precedence < self.token.precedence:
pass # In a^(b+c), we don't need parentheses around b+c
# when it is LaTeXed.
ret_str += kid1_str + '^' + '{' + kid2_str + '}'
else: # precedence = 4. Must be of type OPER_POST.
kid1 = self.children[0]
kid1_str = kid1.build_infix(opt)
if kid1.token.precedence <= self.token.precedence:
# true unless kid1 is an atomic term
kid1_str = '(' + kid1_str + ')'
ret_str += kid1_str + self.token2latex(self.token, opt)
return ret_str
def build_infix_formula(self, opt: str='text') -> str:
LATEX_DICT = self.LATEX_DICT
# 1. atomic formulas and bot
if not self.children: # 'prop_letter' or 'conn_0ary'
# 1.1 terminal nodes
if self.token.token_type == 'prop_letter':
return self.ident2latex(self.token, opt)
else: # self.token.value must be 'bot'
return (LATEX_DICT[self.token.value] if opt=='latex'
else self.token.value)
elif self.token.token_type in Token.FMLA_TOKENS:
# 'pred_pre', 'pred_in', 'equality'
# 1.2 internal nodes
if self.token.token_type == 'pred_pre': # prefix predicate
label = self.ident2latex(self.token, opt)
args = ', '.join(kid.build_infix(opt) for kid in self.children)
return label + '(' + args + ')'
else: # 'pred_in' or 'equality' # infix predicate
kid1, kid2 = self.children
kid1_str = kid1.build_infix(opt)
kid2_str = kid2.build_infix(opt)
return (kid1_str + ' ' + self.token2latex(self.token, opt) +
' ' + kid2_str)
# 2. compound formulas except bot -- i.e., connectives
# and quantifiers
else:
ret_str = ''
if self.token.arity == 2:
# 2.1 binary connectives
token_str = (r'\: ' + LATEX_DICT[self.token.value] + r'\: '
if opt=='latex' else f" {self.token.value} ")
kid1, kid2 = self.children
kid1_str = kid1.build_infix(opt)
kid2_str = kid2.build_infix(opt)
if self.token.token_type == 'conn_arrow': # 'imp', 'iff', 'xor'
# determine whether we need parentheses around kid1
if kid1.token.token_type == 'conn_arrow':
if self.token.value != kid1.token.value:
kid1_str = f"({kid1_str})"
else:
if self.token.value == "imp":
kid1_str = f"({kid1_str})"
else:
pass # iff and xor are associative
# determine whether we need parentheses around kid2
if kid2.token.token_type == 'conn_arrow':
if self.token.value != kid2.token.value:
kid2_str = f"({kid2_str})"
else:
pass # even 'imp' is right-associative
else: # 'and', 'or' (precedence == 2)
# determine whether we need parentheses around kid1
if kid1.token.token_type == 'conn_arrow':
kid1_str = f"({kid1_str})"
elif (kid1.token.token_type == 'conn_2ary' and
self.token.value != kid1.token.value):
kid1_str = f"({kid1_str})"
# determine whether we need parentheses around kid2
if kid2.token.token_type == 'conn_arrow':
kid2_str = f"({kid2_str})"
elif (kid2.token.token_type == 'conn_2ary' and
self.token.value != kid2.token.value):
kid2_str = f"({kid2_str})"
# x < y = z case
if self.token.value == 'and':
if (v_str := self.seq_infix(opt)):
return v_str
else:
pass
ret_str += kid1_str + token_str + kid2_str
elif self.token.token_type == 'conn_1ary':
# 2.2 unary connectives (actually, negation only)
token_str = (LATEX_DICT[self.token.value] + r'\, ' if opt=='latex'
else self.token.value + ' ')
kid1 = self.children[0]
kid1_str = kid1.build_infix(opt)
# determine whether we need parentheses around kid1
if kid1.token.token_type in ('conn_2ary', 'conn_arrow', 'pred_in', 'equality'):
kid1_str = f"({kid1_str})"
ret_str += token_str + kid1_str
else:
# 2.3 quantifier
token_str = (LATEX_DICT[self.token.value] if opt=='latex'
else self.token.value) + ' '
kid1 = self.children[0] # a variable for determiner
kid1_str = self.ident2latex(kid1.token, opt)
kid11 = kid1.children[0]
kid11_str = kid11.build_infix(opt)
# determine whether we need parentheses around kid11
if kid11.token.token_type in ('pred_in', 'equality'):
kid11_str = f"({kid11_str})"
ret_str += (token_str + kid1_str + (r"\, " if opt=='latex' else " ") +
kid11_str)
return ret_str
def seq_infix(self, opt) -> str:
# sequence of terms connected by infix operators: i.e., x < y = z, which
# is parsed as x < y and y = z.
# This method is called iff self.token.value == 'and'.
kid1, kid2 = self.children
if kid2.token.token_type in ('pred_in', 'equality'):
if kid1.token.token_type in ('pred_in', 'equality'):
if kid1.children[1] == kid2.children[0]:
kid1_str = kid1.build_infix(opt)
kid2_token_str = kid2.token2latex(kid2.token, opt)
kid22_str = kid2.children[1].build_infix(opt)
return ' '.join([kid1_str, kid2_token_str, kid22_str])
else:
return ''
elif kid1.token.value == 'and':
if (kid1.children[1].children[1] == kid2.children[0] and
(kid1_str := kid1.seq_infix(opt))):
kid2_token_str = kid2.token2latex(kid2.token, opt)
kid22_str = kid2.children[1].build_infix(opt)
return ' '.join([kid1_str, kid2_token_str, kid22_str])
else:
return ''
else:
return ''
else:
return ''
def display_infix(self, opt: str='latex'):
from IPython.display import display, Math
s = self.build_infix(opt)
if opt == 'latex':
display(Math(f"${s}$"))
else:
print(s)
#region comment
# bussproof tree has the following structure:
# 1. terminal node: \AxiomC{..}
# terms, prop letters, and bot
# 2. non-terminal node: \UnaryInfC{..}, \BinaryInfC{..}, \TrinaryInfC{..}
# predicate symbol(prefix and infix), equality
# prefix predicate's arity is at most 3
# connectives(unary and binary)
# quantifier + var_determiner
#endregion
def build_bussproof(self): # wrapper of build_bussproof_rec()
the_str = self.build_bussproof_rec()
return r"\begin{prooftree}" + "\n" + the_str + r"\end{prooftree}" + "\n"
def build_bussproof_rec(self):
LATEX_DICT = self.LATEX_DICT
if self.type == 'term':
# terminal node. use \AxiomC{..}
label = self.build_infix_term('latex')
the_str = r"\AxiomC" + r"{$" + label + "$}\n"
# self.type == 'formula'
elif not self.children:
# terminal node. use \AxiomC{..}
if self.token.token_type == 'prop_letter':
label = self.ident2latex(self.token)
else: # self.token.value must be 'bot'
label = LATEX_DICT[self.token.value]
the_str = r"\AxiomC" + r"{$" + label + "$}\n"
else: # pred_pre, pred_in, equality,
# conn_1ary, conn_2ary, conn_arrow, quantifier
label = (self.ident2latex(self.token)
if self.token.token_type == 'pred_pre'
else self.token2latex(self.token))
arity = self.token.arity # must be 1, 2, or 3
if arity == 1: # not, forall, exists, unary predicate
if self.token.token_type in ('conn_1ary', 'pred_pre'):
kid1 = self.children[0]
kid1_str = kid1.build_bussproof_rec()
the_str = kid1_str + r"\UnaryInfC" + r"{$" + label + "$}\n"
else: # quantifier
kid1 = self.children[0] # a variable for determiner
kid1_str = self.ident2latex(kid1.token)
kid11 = kid1.children[0]
kid11_str = kid11.build_bussproof_rec()
the_str = (kid11_str + r"\UnaryInfC" + r"{$" + label + ' ' +
kid1_str + "$}\n")
elif arity == 2:
kid1, kid2 = self.children
kid1_str = kid1.build_bussproof_rec()
kid2_str = kid2.build_bussproof_rec()
the_str = (kid1_str + kid2_str + r"\BinaryInfC" + r"{$" +
label + "$}\n")
elif arity == 3:
kid1, kid2, kid3 = self.children
kid1_str = kid1.build_bussproof_rec()
kid2_str = kid2.build_bussproof_rec()
kid3_str = kid3.build_bussproof_rec()
the_str = (kid1_str + kid2_str + kid3_str + r"\TrinaryInfC" + r"{$" +
label + "$}\n")
else:
raise ValueError(f"arity of predicate symbol cannot be {arity}")
return the_str
def draw_tree(self, verbose=False):
url = 'https://raw.githubusercontent.com/jhjeong314/mytest/main'
import httpimport
with httpimport.remote_repo(url):
from draw_tree import draw_ast
draw_ast(self, verbose)
#region syntactic manipulations
def node_at(self, pos: List[int]):
# The return value may be a term or a formula.
if pos == []:
return self
else:
ast = self
for i in pos:
ast = ast.children[i]
return ast
def replace_node_at(self, pos: List[int], new_node,
dupl: str = ''):
# Make sure to replace subformula by a formula and
# a subterm by a term.
# dupl is either '' or 'dupl'. In the 1st case, We update self
# and return None. In the 2nd case, we create a new node by
# the replacement and return the new node.
# pos must be nonempty.
import copy
assert isinstance(new_node, Node), \
"Node.replace_node_at(): new_node must be a Node object"
# I had to type check in this way.
# type hinting "new_node: Node" does not work.
node0 = self if dupl == '' else copy.deepcopy(self)
node = node0
for i in pos[:-1]:
assert len(node.children) > i, \
"Node.replace_node_at(): pos is out of range"
node = node.children[i]
node.children[pos[-1]] = copy.deepcopy(new_node)
if dupl == 'dupl':
return node0
def replace_nodes_at(self, pos_li: List[List[int]],
new_node_li, dupl: str=''):
# This method is a multiple version of replace_node_at().
# Members of pos_li must be incomparable.
import copy
assert len(pos_li) == len(new_node_li)
if dupl != 'dupl':
for i in range(len(pos_li)):
self.replace_node_at(pos_li[i], new_node_li[i])
else:
node0 = copy.deepcopy(self)
for i in range(len(pos_li)):
node0.replace_node_at(pos_li[i], new_node_li[i])
return node0
def substitute(self, var: str, new_node, dupl: str = ''):
# Input argument var is a string, which can be either an individual
# variable/constant or a propositional variable.
# There is no difference in the code for handling these two cases.
import copy
node = copy.deepcopy(self) if dupl == 'dupl' else self
if node.token.value == var:
node = copy.deepcopy(new_node)
elif node.children:
for i, kid in enumerate(node.children):
# note that dupl=='dupl' is passed to the recursive call
node.children[i] = kid.substitute(var, new_node, 'dupl')
if dupl == 'dupl':
return node
#endregion syntactic manipulations
# end of class Node
class Parser:
AND_TOKEN = Token('and')
def __init__(self, tokens):
self.tokens = tokens
self.current_token = None
self.index = -1
self.advance() # set self.current_token to
# the first(i.e. self.index=0) element of tokens
def advance(self): # increment self.index and set self.current_token
self.index += 1
if self.index < len(self.tokens):
self.current_token = self.tokens[self.index]
else:
self.current_token = None
def check_token_type(self, token_types) -> bool:
# token_types can be a string or a tuple of strings
# Check if self.current_token is of type token_types if token_types
# is a string, or belongs to token_types if token_types
# is a tuple of strings.
token = self.current_token
if token is None:
return False
elif type(token_types) is not tuple: # must be a string in this case
return token.token_type == token_types
else:
return token.token_type in token_types
def check_token_value(self, token_values) -> bool:
# Check if self.current_token is of value token_value.
token = self.current_token
if token is None:
return False
elif type(token_values) is not tuple: # must be a string in this case
return token.value == token_values
else:
return token.value in token_values
def parse(self) -> Node:
# determine the type of self.tokens, whether it is a formula or a term
is_formula = any([token.token_type in Token.FMLA_TOKENS
for token in self.tokens])
if is_formula:
return self.formula()
else:
return self.term()
def formula(self) -> Node:
node = self.comp_fmla1() # compound formula type 1
while self.check_token_type('conn_arrow'): # 'imp', 'iff', 'xor'
token = self.current_token
if token is None:
raise SyntaxError(f"Expected a token at {self.index}," +
f" in expr(), but {self.current_token} is None.")
self.advance()
if token.value == 'imp':
right_node = self.formula() # recursive call for right-assoc
else:
right_node = self.comp_fmla1() # left-assoc
node = Node(token, [node, right_node])
return node
def comp_fmla1(self) -> Node:
node = self.comp_fmla2() # compound formula type 2
while self.check_token_type('conn_2ary'): # 'and', 'or'
token = self.current_token
self.advance()
right_node = self.comp_fmla2()
node = Node(token, [node, right_node]) # left-assoc
return node
def comp_fmla2(self) -> Node:
if self.check_token_type('lparen'):
self.advance()
node = self.formula()
if self.check_token_type('rparen'):
self.advance()
else:
raise SyntaxError(f"Expected ')' at {self.index}," +
f" in comp_fmla2(), but {self.current_token} is given.")
elif self.check_token_type('conn_0ary'):
token = self.current_token
self.advance()
node = Node(token)
elif self.check_token_type('conn_1ary'): # 'not'
token = self.current_token
self.advance()
right_node = self.comp_fmla2() # recursive call for right-assoc
node = Node(token, [right_node])
elif self.check_token_type('quantifier'):
token_q = self.current_token
self.advance()
token_v = self.current_token
if token_v is None or token_v.token_type != 'var':
raise SyntaxError(f"Expected a variable at {self.index}," +
f" in comp_fmla2(), but {self.current_token} is given.")
token_v.token_type = 'var_determiner'
token_v.arity = 1
self.advance()
right_node = self.comp_fmla2() # recursive call for right-assoc
node = Node(token_q, [Node(token_v, [right_node])])
else:
# atomic formula (not identifier or equivalently, not the atomic term)
# formulas like a < b = c are considered as atomic formulas
node = self.atom()
return node
def atom(self) -> Node: # type: ignore # ignore Pylance error
# formulas like a < b = c are considered as atomic formulas
if self.current_token is not None:
token = self.current_token
if self.check_token_type('prop_letter'):
# atomic formula case
self.advance()
return Node(token)
elif self.check_token_type('pred_pre'):
# P(t1,t2,...) case
self.advance()
if self.check_token_type('lparen'):
self.advance()
args = []
while True:
args.append(self.term())
if self.check_token_type('comma'):
self.advance()
elif self.check_token_type('rparen'):
break
else:
raise SyntaxError(
f"Expected ',' or ')' after predicate argument at " +
f"{self.index} in atom(), but {self.current_token}" +
f" is given.")
# arity check
if token.arity is None or token.arity != len(args):
raise SyntaxError(
f"Predicate {token.value} expects {token.arity} " +
f"arguments, but {len(args)} were given")
self.advance()
return Node(token, args)
else:
raise SyntaxError(
f"Expected '(' after predicate symbol at {self.index}" +
f" in atom(), but {self.current_token} is given.")
else:
# t1 pred_in t2 case (such as t1 = t2, t1 < t2 etc.)
# t1 = t2 < t3 ~ t4 is parsed as
# (t1 = t2 and t2 < t3) and t3 ~ t4
node = self.term()
saved_node = None
while self.check_token_type(('equality', 'pred_in')):
token = self.current_token
self.advance()
right_node = self.term()
if saved_node is None:
node = Node(token, [node, right_node])
else:
new_node = Node(token, [saved_node, right_node])
node = Node(self.AND_TOKEN, [node, new_node])
saved_node = right_node
return node
else:
raise SyntaxError("Unexpected end of input, in atom()")
def term(self) -> Node:
if self.current_token and \
self.current_token.value in Token.OPER_PRE: