Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

r""" 

Evaluation of Boolean Formulas 

 

AUTHORS: 

 

- Chris Gorecki (2006): initial version 

 

- Paul Scurek (2013-08-05): updated docstring formatting 

 

EXAMPLES: 

 

We can assign values to the variables and evaluate a formula:: 

 

sage: import sage.logic.booleval as booleval 

sage: t = ['|', ['&', 'a', 'b'], ['&', 'a', 'c']] 

sage: d = {'a' : True, 'b' : False, 'c' : True} 

sage: booleval.eval_formula(t, d) 

True 

 

We can change our assignment of values by modifying the dictionary:: 

 

sage: d['a'] = False 

sage: booleval.eval_formula(t, d) 

False 

""" 

from __future__ import absolute_import 

#***************************************************************************** 

# Copyright (C) 2006 Chris Gorecki <chris.k.gorecki@gmail.com> 

# Copyright (C) 2013 Paul Scurek <scurek86@gmail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# as published by the Free Software Foundation; either version 2 of 

# the License, or (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

 

from . import logicparser 

 

# dictionary containing variable keys and boolean values 

__vars = {} 

 

 

def eval_formula(tree, vdict): 

r""" 

Evaluate the tree and return a boolean value. 

 

INPUT: 

 

- ``tree`` -- a list of three elements corresponding to a branch of a 

parse tree 

 

- ``vdict`` -- a dictionary containing variable keys and boolean values 

 

OUTPUT: 

 

The result of the evaluation as a boolean value. 

 

EXAMPLES: 

 

This example illustrates evaluating a boolean formula:: 

 

sage: import sage.logic.booleval as booleval 

sage: t = ['|', ['&', 'a', 'b'], ['&', 'a', 'c']] 

sage: d = {'a' : True, 'b' : False, 'c' : True} 

sage: booleval.eval_formula(t, d) 

True 

 

:: 

 

sage: d['a'] = False 

sage: booleval.eval_formula(t, d) 

False 

""" 

global __vars 

__vars = vdict 

b = logicparser.apply_func(tree, eval_f) 

return b 

 

def eval_f(tree): 

r""" 

Evaluate the tree. 

 

INPUT: 

 

- ``tree`` -- a list of three elements corresponding to a branch of a 

parse tree 

 

OUTPUT: 

 

The result of the evaluation as a boolean value. 

 

EXAMPLES: 

 

This example illustrates how to evaluate a parse tree:: 

 

sage: import sage.logic.booleval as booleval 

sage: booleval.eval_f(['&', True, False]) 

False 

 

sage: booleval.eval_f(['^', True, True]) 

False 

 

sage: booleval.eval_f(['|', False, True]) 

True 

""" 

return eval_op(tree[0], tree[1], tree[2]) 

 

def eval_op(op, lv, rv): 

r""" 

Evaluate ``lv`` and ``rv`` according to the operator ``op``. 

 

INPUT: 

 

- ``op`` -- a string or character representing a boolean operator 

 

- ``lv`` -- a boolean or variable 

 

- ``rv`` -- a boolean or variable 

 

OUTPUT: 

 

The evaluation of ``lv op rv`` as a boolean value. 

 

EXAMPLES: 

 

We can evaluate an operator given the values on either side:: 

 

sage: import sage.logic.booleval as booleval 

sage: booleval.eval_op('&', True, False) 

False 

 

sage: booleval.eval_op('^', True, True) 

False 

 

sage: booleval.eval_op('|', False, True) 

True 

""" 

lval = rval = None 

if lv is False: 

lval = False 

elif lv is True: 

lval = True 

elif lv is not None: 

lval = __vars[lv] 

 

if rv is False: 

rval = False 

elif rv is True: 

rval = True 

elif rv is not None: 

rval = __vars[rv] 

 

if op == '~': 

return not lval 

elif op == '&': 

return lval and rval 

elif op == '|': 

return lval or rval 

elif op == '^': 

return lval ^ rval 

elif op == '->': 

return (not lval) or rval 

elif op == '<->': 

return (not lval or rval) and (not rval or lval) 

else: # one variable 

return __vars[op]