-
Notifications
You must be signed in to change notification settings - Fork 0
/
truthtable.typ
352 lines (306 loc) · 8.91 KB
/
truthtable.typ
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
// Logic "true" object
#let l-true(repr: $top$) = (
logic_dict: true,
logic_type: "true",
repr: [#repr],
value: m => true,
name: "TRUE",
skip: true,
children: ()
)
// Logic "false" object
#let l-false(repr: $bot$) = (
logic_dict: true,
logic_type: "false",
repr: [#repr],
name: "FALSE",
value: m => false,
skip: true,
children: ()
)
// Converts bools to their equivalent logic objects
#let l-bool(bool) = if bool { l-true() } else { l-false() }
// Logic atomic variable / proposition / ...
#let l-var(name, repr: none) = (
logic_dict: true,
logic_type: "var",
repr: if repr == none { $#name$ } else { [#repr] },
name: name,
value: mapping => if name in mapping { mapping.at(name) } else { false },
skip: false,
children: ()
)
// Convert a value to their equivalent logical object
// Boolean becomes a true or false object;
// A string becomes a logical variable;
// All else must be a dictionary created by this library.
#let l-logic-convert(val) = if type(val) == "boolean" {
l-bool(val)
} else if type(val) == "string" {
l-var(val)
} else if type(val) != "dictionary" {
panic("Failed to convert value (of type '" + type(val) + "') to a logic object: Not a boolean, string or dictionary.")
} else if "logic_dict" not in val or val.logic_dict != true {
panic("Invalid dictionary given as logic object (must be a valid logic dictionary).")
} else {
val
}
// Represents an arbitrary logic operator or functor
// Customize its representation with the 'repr' parameter
#let l-operator(
name,
..children,
value: mapping => false,
repr: none,
skip: false,
) = (
logic_dict: true,
logic_type: "operator",
name: str(name),
repr: if repr == none { [#name] } else { repr },
value: value,
skip: skip,
children: children.pos().map(l-logic-convert)
)
// Return an expression's representation,
// around parentheses if it has multiple children
#let l-parens-repr-if-composite(expr) = {
if expr.children.len() > 1 {
$(#expr.repr)$
} else {
$#expr.repr$
}
}
// Generate binary operator representation
#let l-gen-binary-operator-repr(op, a, b, parens: auto) = {
let a_repr = a.repr
let b_repr = b.repr
if parens == auto {
a_repr = l-parens-repr-if-composite(a)
b_repr = l-parens-repr-if-composite(b)
} else if parens { // force parens
a_repr = $(#a_repr)$
b_repr = $(#b_repr)$
}
$#a_repr #op #b_repr$
}
// Generate unry operator representation
#let l-gen-unary-operator-repr(op, a, parens: auto) = {
let a_repr = a.repr
if parens == auto {
a_repr = l-parens-repr-if-composite(a)
} else if parens { // force parens
a_repr = $(#a_repr)$
}
$#op #a_repr$
}
// Logic object for the AND operator
#let l-and(a, b, parens: auto, skip: false) = {
let a = l-logic-convert(a)
let b = l-logic-convert(b)
l-operator(
"AND",
a, b,
value: mapping => (a.value)(mapping) and (b.value)(mapping),
repr: l-gen-binary-operator-repr($and$, a, b, parens: parens),
skip: skip,
)
}
// Logic object for the OR operator
#let l-or(a, b, parens: auto, skip: false) = {
let a = l-logic-convert(a)
let b = l-logic-convert(b)
l-operator(
"OR",
a, b,
value: mapping => (a.value)(mapping) or (b.value)(mapping),
repr: l-gen-binary-operator-repr($or$, a, b, parens: parens),
skip: skip,
)
}
// Logic object for the NOT operator
#let l-not(a, parens: auto, skip: false) = {
let a = l-logic-convert(a)
l-operator(
"NOT",
a,
value: mapping => not (a.value)(mapping),
repr: l-gen-unary-operator-repr($not$, a, parens: parens),
skip: skip,
)
}
// Logic object for the IMPLIES operator
#let l-imp(a, b, parens: auto, skip: false) = {
let a = l-logic-convert(a)
let b = l-logic-convert(b)
l-operator(
"IMP",
a, b,
value: mapping => (not (a.value)(mapping)) or (b.value)(mapping),
repr: l-gen-binary-operator-repr($->$, a, b, parens: parens),
skip: skip,
)
}
// Logic object for the IF AND ONLY IF operator
#let l-iff(a, b, parens: auto, skip: false) = {
let a = l-logic-convert(a)
let b = l-logic-convert(b)
l-operator(
"IFF",
a, b,
value: mapping => (a.value)(mapping) == (b.value)(mapping),
repr: l-gen-binary-operator-repr($<->$, a, b, parens: parens),
skip: skip,
)
}
// Returns all atomic variables in the expression
#let l-search-variables(expr) = {
let children = expr.children
if (children.len() == 0) {
if (expr.logic_type not in ("true", "false")) {
(expr,)
} else {
()
}
} else {
children.map(c => l-search-variables(c)).flatten()
}
}
// Returns 'true' if two logic objects
// are likely to represent the same thing
#let l-compare-logic-objects(a, b) = (
(type(a) == "dictionary")
and (type(b) == "dictionary")
and "logic_dict" in a
and "logic_dict" in b
and a.logic_dict
and b.logic_dict
and a.logic_type == b.logic_type
and a.repr == b.repr
and a.name == b.name
and a.skip == b.skip
)
// Returns all sub-expressions of an expression until the given
// depth (-1 = unlimited)
// 'unique: true' is used to ensure elements do not repeat
// and 'initial_vars: true' ensures all variables are at the beginning
// of the tree
#let l-expr-tree(
expr,
max_depth: -1, unique: true, initial_vars: true
) = {
if max_depth == 0 { // reached the max
return ()
}
if expr.logic_type in ("true", "false") {
()
} else {
let subexprs = expr.children.map(
c => l-expr-tree(
c,
max_depth: calc.max(-1, max_depth - 1),
unique: false, // deal with it only on the first-level
initial_vars: initial_vars))
// don't allow going below -1 ^
let res = subexprs.flatten()
if initial_vars and expr.logic_type == "var" {
res.insert(0, expr) // variable at the beginning
} else {
res.push(expr)
}
if unique {
res.fold(
(),
(acc, expr) => {
if acc.filter(l-compare-logic-objects.with(expr)).len() == 0 {
acc + (expr,)
} else {
acc
}
}
)
} else {
res
}
}
}
// Generate all possible true/false combinations
// of certain variables
#let l-gen-true-false-maps(vars) = {
if vars.len() == 0 {
return ()
}
let head = vars.first()
// convert to string
if (
type(head) == "dictionary"
and "logic_dict" in head
and head.logic_dict
) {
head = head.name
} else {
head = str(head)
}
// Recursively generate T/F map for the rest of the
// variable array
let tail_res = l-gen-true-false-maps(vars.slice(1))
// this variable's single true/false combinations
let true_d = (:)
let false_d = (:)
true_d.insert(head, true)
false_d.insert(head, false)
let res = ()
// join with the true/false combinations from the
// other variables
for d in tail_res {
if head in d { // don't override this variable's existing values
res.push(d)
} else {
res.push((: ..true_d, ..d))
res.push((: ..false_d, ..d))
}
}
if res.len() == 0 { // base case: only one variable => T/F
(true_d, false_d)
} else {
res
}
}
// Builds a truth table automatically from a logic
// expression. Use 'repr_true' and 'repr_false' to
// control the output of True and False values.
// Any additional args are passed to the 'table' call
// (such as 'fill').
// Use 'table_func' to override the default table function.
#let truth-table(
expr,
repr_true: "T",
repr_false: "F",
table_func: table,
..table_args
) = {
assert(type(table_func) == "function", message: "'table_func' must be a function")
let vars = l-search-variables(expr)
let varnames = vars.map(c => c.name)
let mappings = l-gen-true-false-maps(varnames)
let expr_tree = l-expr-tree(expr)
.flatten()
.filter(e => "skip" not in e or not e.skip)
// initialize with the table headers
let table_children = expr_tree.map(c => c.repr)
for map in mappings {
for expr in expr_tree {
let bool_value = (expr.value)(map)
table_children.push(if bool_value {
[#repr_true]
} else {
[#repr_false]
})
}
}
table_func(
columns: (auto,) * expr_tree.len(),
rows: (auto,) * mappings.len(),
..table_children,
..table_args.named())
}