-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathfunctions.py
115 lines (78 loc) · 3.96 KB
/
functions.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
"""The goal in this module is to define functions that take a formula as input and
do some computation on its syntactic structure. """
from formula import *
def length(formula: Formula):
"""Determines the length of a formula in propositional logic."""
if isinstance(formula, Atom):
return 1
if isinstance(formula, Not):
return length(formula.inner) + 1
if isinstance(formula, Implies) or isinstance(formula, And) or isinstance(formula, Or):
return length(formula.left) + length(formula.right) + 1
def subformulas(formula: Formula):
"""Returns the set of all subformulas of a formula.
For example, observe the piece of code below.
my_formula = Implies(Atom('p'), Or(Atom('p'), Atom('s')))
for subformula in subformulas(my_formula):
print(subformula)
This piece of code prints p, s, (p v s), (p → (p v s))
(Note that there is no repetition of p)
"""
if isinstance(formula, Atom):
return {formula}
if isinstance(formula, Not):
return {formula}.union(subformulas(formula.inner))
if isinstance(formula, Implies) or isinstance(formula, And) or isinstance(formula, Or):
sub1 = subformulas(formula.left)
sub2 = subformulas(formula.right)
return {formula}.union(sub1).union(sub2)
# we have shown in class that, for all formula A, len(subformulas(A)) <= length(A).
def atoms(formula: Formula):
"""Returns the set of all atoms occurring in a formula.
For example, observe the piece of code below.
my_formula = Implies(Atom('p'), Or(Atom('p'), Atom('s')))
for atom in atoms(my_formula):
print(atom)
This piece of code above prints: p, s
(Note that there is no repetition of p)
"""
pass # ======== REMOVE THIS LINE AND INSERT YOUR CODE HERE ========
def number_of_atoms(formula: Formula):
"""Returns the number of atoms occurring in a formula.
For instance,
number_of_atoms(Implies(Atom('q'), And(Atom('p'), Atom('q'))))
must return 3 (Observe that this function counts the repetitions of atoms)
"""
pass # ======== REMOVE THIS LINE AND INSERT YOUR CODE HERE ========
def number_of_connectives(formula: Formula):
"""Returns the number of connectives occurring in a formula."""
pass # ======== REMOVE THIS LINE AND INSERT YOUR CODE HERE ========
def is_literal(formula: Formula):
"""Returns True if formula is a literal. It returns False, otherwise"""
pass # ======== REMOVE THIS LINE AND INSERT YOUR CODE HERE ========
def substitution(formula: Formula, old_subformula: Formula, new_subformula: Formula):
"""Returns a new formula obtained by replacing all occurrences
of old_subformula in the input formula by new_subformula."""
pass # ======== REMOVE THIS LINE AND INSERT YOUR CODE HERE ========
def is_clause(formula: Formula):
"""Returns True if formula is a clause. It returns False, otherwise"""
pass # ======== REMOVE THIS LINE AND INSERT YOUR CODE HERE ========
def is_negation_normal_form(formula: Formula):
"""Returns True if formula is in negation normal form.
Returns False, otherwise."""
pass # ======== REMOVE THIS LINE AND INSERT YOUR CODE HERE ========
def is_cnf(formula: Formula):
"""Returns True if formula is in conjunctive normal form.
Returns False, otherwise."""
pass # ======== REMOVE THIS LINE AND INSERT YOUR CODE HERE ========
def is_term(formula: Formula):
"""Returns True if formula is a term. It returns False, otherwise"""
pass # ======== REMOVE THIS LINE AND INSERT YOUR CODE HERE ========
def is_dnf(formula: Formula):
"""Returns True if formula is in disjunctive normal form.
Returns False, otherwise."""
pass # ======== REMOVE THIS LINE AND INSERT YOUR CODE HERE ========
def is_decomposable_negation_normal_form(formula: Formula):
"""Returns True if formula is in decomposable negation normal form.
Returns False, otherwise."""
pass # ======== REMOVE THIS LINE AND INSERT YOUR CODE HERE ========