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

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

r""" 

Symbolic Logic Expressions 

 

An expression is created from a string that consists of the 

operators ``!``, ``&``, ``|``, ``->``, ``<->``, which correspond to the 

logical functions not, and, or, if then, if and only if, respectively. 

Variable names must start with a letter and contain only 

alpha-numerics and the underscore character. 

 

AUTHORS: 

 

- Chris Gorecki (2007): initial version 

 

- William Stein (2007-08-31): integration into Sage 2.8.4 

 

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

""" 

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

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

# Copyright (C) 2007 William Stein <wstein@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 __future__ import print_function, division 

 

import string 

 

# constants 

tok_list = ['OPAREN', 'CPAREN', 'AND', 'OR', 'NOT', 'IFTHEN', 'IFF'] 

bin_list = ['AND', 'OR', 'IFTHEN', 'IFF'] 

operators = '()&|!<->' 

# variables 

vars = {} 

vars_order = [] 

 

class SymbolicLogic: 

""" 

EXAMPLES: 

 

This example illustrates how to create a boolean formula and print 

its table:: 

 

sage: log = SymbolicLogic() 

sage: s = log.statement("a&b|!(c|a)") 

sage: t = log.truthtable(s) 

sage: log.print_table(t) 

a | b | c | value | 

-------------------------------- 

False | False | False | True | 

False | False | True | False | 

False | True | False | True | 

False | True | True | False | 

True | False | False | False | 

True | False | True | False | 

True | True | False | True | 

True | True | True | True | 

""" 

def statement(self, s): 

r""" 

Return a token list to be used by other functions in the class 

 

INPUT: 

 

- ``s`` -- a string containing the logic expression to be manipulated 

 

- ``global vars`` -- a dictionary with variable names as keys and the 

variables' current boolean values as dictionary values 

 

- ``global vars_order`` -- a list of the variables in the order 

that they are found 

 

OUTPUT: 

 

A list of length three containing the following in this order: 

 

1. a list of tokens 

2. a dictionary of variable/value pairs 

3. a list of the variables in the order they were found 

 

EXAMPLES: 

 

This example illustrates the creation of a statement:: 

 

sage: log = SymbolicLogic() 

sage: s = log.statement("a&b|!(c|a)") 

sage: s2 = log.statement("!((!(a&b)))") 

 

It is an error to use invalid variable names:: 

 

sage: s = log.statement("3fe & @q") 

Invalid variable name: 3fe 

Invalid variable name: @q 

 

It is also an error to use invalid syntax:: 

 

sage: s = log.statement("a&&b") 

Malformed Statement 

sage: s = log.statement("a&((b)") 

Malformed Statement 

""" 

global vars, vars_order 

toks, vars, vars_order = ['OPAREN'], {}, [] 

tokenize(s, toks) 

statement = [toks, vars, vars_order] 

try: #verify the syntax 

eval(toks) 

except (KeyError, RuntimeError): 

print('Malformed Statement') 

return [] 

return statement 

 

def truthtable(self, statement, start=0, end=-1): 

r""" 

Return a truth table. 

 

INPUT: 

 

- ``statement`` -- a list; it contains the tokens and the two global 

variables vars and vars_order 

 

- ``start`` -- (default: 0) an integer; this represents the row of 

the truth table from which to start 

 

- ``end`` -- (default: -1) an integer; this represents the last row 

of the truth table to be created 

 

OUTPUT: 

 

The truth table as a 2d array with the creating formula tacked 

to the front. 

 

EXAMPLES: 

 

This example illustrates the creation of a statement:: 

 

sage: log = SymbolicLogic() 

sage: s = log.statement("a&b|!(c|a)") 

sage: t = log.truthtable(s) #creates the whole truth table 

 

We can now create truthtable of rows 1 to 5:: 

 

sage: s2 = log.truthtable(s, 1, 5); s2 

[[['OPAREN', 

'a', 

'AND', 

'b', 

'OR', 

'NOT', 

'OPAREN', 

'c', 

'OR', 

'a', 

'CPAREN', 

'CPAREN'], 

{'a': 'False', 'b': 'False', 'c': 'True'}, 

['a', 'b', 'c']], 

['False', 'False', 'True', 'False'], 

['False', 'True', 'False', 'True'], 

['False', 'True', 'True', 'True'], 

['True', 'False', 'False', 'False']] 

 

.. NOTE:: 

 

When sent with no start or end parameters this is an 

exponential time function requiring `O(2^n)` time, where 

`n` is the number of variables in the logic expression 

""" 

global vars, vars_order 

toks, vars, vars_order = statement 

if end == -1: 

end = 2 ** len(vars) 

table = [statement] 

keys = vars_order 

keys.reverse() 

for i in range(start,end): 

j = 0 

row = [] 

for key in keys: 

bit = get_bit(i, j) 

vars[key] = bit 

j += 1 

row.insert(0, bit) 

row.append(eval(toks)) 

table.append(row) 

return table 

 

def print_table(self, table): 

r""" 

Return a truthtable corresponding to the given statement. 

 

INPUT: 

 

- ``table`` -- object created by :meth:`truthtable()` method; it 

contains the variable values and the evaluation of the statement 

 

OUTPUT: 

 

A formatted version of the truth table. 

 

EXAMPLES: 

 

This example illustrates the creation of a statement and 

its truth table:: 

 

sage: log = SymbolicLogic() 

sage: s = log.statement("a&b|!(c|a)") 

sage: t = log.truthtable(s) #creates the whole truth table 

sage: log.print_table(t) 

a | b | c | value | 

-------------------------------- 

False | False | False | True | 

False | False | True | False | 

False | True | False | True | 

False | True | True | False | 

True | False | False | False | 

True | False | True | False | 

True | True | False | True | 

True | True | True | True | 

 

We can also print a shortened table:: 

 

sage: t = log.truthtable(s, 1, 5) 

sage: log.print_table(t) 

a | b | c | value | value | 

---------------------------------------- 

False | False | False | True | True | 

False | False | True | False | False | 

False | False | True | True | False | 

False | True | False | False | True | 

""" 

statement = table[0] 

del table[0] 

vars_order = statement[2] 

vars_len = [] 

line = s = "" 

vars_order.reverse() 

vars_order.append('value') 

for var in vars_order: 

vars_len.append(len(var)) 

s = var + ' ' 

while len(s) < len('False '): 

s += ' ' 

s += '| ' 

line += s 

print(line) 

print(len(line) * '-') 

for row in table: 

line = s = "" 

i = 0 

for e in row: 

if e == 'True': 

j = 2 

else: 

j = 1 

s = e + ' ' * j 

if i < len(vars_len): 

while len(s) <= vars_len[i]: 

s += ' ' 

s += '| ' 

line += s 

i += 1 

print(line) 

print("") 

 

def combine(self, statement1, statement2): 

r""" 

Return a new statement which contains the 

two statements or'd together. 

 

INPUT: 

 

- ``statement1`` -- the first statement 

- ``statement2`` -- the second statement 

 

OUTPUT: 

 

A new statement which or'd the given statements together. 

 

EXAMPLES:: 

 

sage: log = SymbolicLogic() 

sage: s1 = log.statement("(a&b)") 

sage: s2 = log.statement("b") 

sage: log.combine(s1,s2) 

[['OPAREN', 

'OPAREN', 

'OPAREN', 

'a', 

'AND', 

'b', 

'CPAREN', 

'CPAREN', 

'OR', 

'OPAREN', 

'b', 

'CPAREN', 

'CPAREN'], 

{'a': 'False', 'b': 'False'}, 

['a', 'b', 'b']]  

""" 

toks = ['OPAREN'] + statement1[0] + ['OR'] + statement2[0] + ['CPAREN'] 

variables = dict(statement1[1].items() + statement2[1].items()) 

var_order = statement1[2] + statement2[2] 

return [toks, variables, var_order] 

 

 

#TODO: implement the simplify function which calls 

#a c++ implementation of the ESPRESSO algorithm 

#to simplify the truthtable: probably Minilog 

def simplify(self, table): 

""" 

Call a C++ implementation of the ESPRESSO algorithm to simplify the 

given truth table. 

 

.. TODO:: 

 

Implement this method. 

 

EXAMPLES:: 

 

sage: log = SymbolicLogic() 

sage: s = log.statement("a&b|!(c|a)") 

sage: t = log.truthtable(s) 

sage: log.simplify(t) 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

raise NotImplementedError 

 

def prove(self, statement): 

""" 

A function to test to see if the statement is a tautology or 

contradiction by calling a C++ library. 

 

.. TODO:: 

 

Implement this method. 

 

EXAMPLES:: 

 

sage: log = SymbolicLogic() 

sage: s = log.statement("a&b|!(c|a)") 

sage: log.prove(s) 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

raise NotImplementedError 

 

def get_bit(x, c): 

r""" 

Determine if bit ``c`` of the number ``x`` is 1. 

 

INPUT: 

 

- ``x`` -- an integer; this is the number from which to take the bit 

 

- ``c`` -- an integer; this is the bit number to be taken 

 

OUTPUT: 

 

A boolean value to be determined as follows: 

 

- ``True`` if bit ``c`` of ``x`` is 1. 

 

- ``False`` if bit ``c`` of ``x`` is not 1. 

 

.. NOTE:: 

 

This function is for internal use by the :class:`SymbolicLogic` class. 

 

EXAMPLES:: 

 

sage: from sage.logic.logic import get_bit 

sage: get_bit(int(2), int(1)) 

'True' 

sage: get_bit(int(8), int(0)) 

'False' 

""" 

bits = [] 

while x > 0: 

if x % 2 == 0: 

b = 'False' 

else: 

b = 'True' 

x = x // 2 

bits.append(b) 

if c > len(bits) - 1: 

return 'False' 

else: 

return bits[c] 

 

 

def eval(toks): 

r""" 

Evaluate the expression contained in ``toks``. 

 

INPUT: 

 

- ``toks`` -- a list of tokens; this represents a boolean expression 

 

OUTPUT: 

 

A boolean value to be determined as follows: 

 

- ``True`` if expression evaluates to ``True``. 

 

- ``False`` if expression evaluates to ``False``. 

 

.. NOTE:: 

 

This function is for internal use by the :class:`SymbolicLogic` class. 

The evaluations rely on setting the values of the variables in the 

global dictionary vars. 

 

TESTS:: 

 

sage: log = SymbolicLogic() 

sage: s = log.statement("a&b|!(c|a)") 

sage: sage.logic.logic.eval(s[0]) 

'True' 

""" 

stack = [] 

for tok in toks: 

stack.append(tok) 

if tok == 'CPAREN': 

lrtoks = [] 

while tok != 'OPAREN': 

tok = stack.pop() 

lrtoks.insert(0, tok) 

stack.append(eval_ltor_toks(lrtoks[1:-1])) 

if len(stack) > 1: 

raise RuntimeError 

return stack[0] 

 

def eval_ltor_toks(lrtoks): 

r""" 

Evaluates the expression contained in ``lrtoks``. 

 

INPUT: 

 

- ``lrtoks`` -- a list of tokens; this represents a part of a boolean 

formula that contains no inner parentheses 

 

OUTPUT: 

 

A boolean value to be determined as follows: 

 

- ``True`` if expression evaluates to ``True``. 

 

- ``False`` if expression evaluates to ``False``. 

 

.. NOTE:: 

 

This function is for internal use by the :class:`SymbolicLogic` class. 

The evaluations rely on setting the values of the variables in the 

global dictionary vars. 

 

TESTS:: 

 

sage: log = SymbolicLogic() 

sage: s = log.statement("a&b|!c") 

sage: ltor = s[0][1:-1]; ltor 

['a', 'AND', 'b', 'OR', 'NOT', 'c'] 

sage: sage.logic.logic.eval_ltor_toks(ltor) 

'True' 

""" 

reduce_monos(lrtoks) # monotonic ! operators go first 

reduce_bins(lrtoks) # then the binary operators 

if len(lrtoks) > 1: 

raise RuntimeError 

return lrtoks[0] 

 

def reduce_bins(lrtoks): 

r""" 

Evaluate ``lrtoks`` to a single boolean value. 

 

INPUT: 

 

- ``lrtoks`` -- a list of tokens; this represents a part of a boolean 

formula that contains no inner parentheses or monotonic operators 

 

OUTPUT: 

 

``None``; the pointer to lrtoks is now a list containing 

``True`` or ``False``. 

 

.. NOTE:: 

 

This function is for internal use by the :class:`SymbolicLogic` class. 

 

TESTS:: 

 

sage: log = SymbolicLogic() 

sage: s = log.statement("a&b|c") 

sage: lrtoks = s[0][1:-1]; lrtoks 

['a', 'AND', 'b', 'OR', 'c'] 

sage: sage.logic.logic.reduce_bins(lrtoks); lrtoks 

['False'] 

""" 

i = 0 

while i < len(lrtoks): 

if lrtoks[i] in bin_list: 

args = [lrtoks[i - 1], lrtoks[i], lrtoks[i + 1]] 

lrtoks[i - 1] = eval_bin_op(args) 

del lrtoks[i] 

del lrtoks[i] 

reduce_bins(lrtoks) 

i += 1 

 

def reduce_monos(lrtoks): 

r""" 

Replace monotonic operator/variable pairs with a boolean value. 

 

INPUT: 

 

- ``lrtoks`` -- a list of tokens; this represents a part of a boolean 

expression that contains now inner parentheses 

 

OUTPUT: 

 

``None``; the pointer to ``lrtoks`` is now a list containing 

monotonic operators. 

 

.. NOTE:: 

 

This function is for internal use by the :class:`SymbolicLogic` class. 

 

TESTS:: 

 

sage: log = SymbolicLogic() 

sage: s = log.statement("!a&!b") 

sage: lrtoks = s[0][1:-1]; lrtoks 

['NOT', 'a', 'AND', 'NOT', 'b'] 

sage: sage.logic.logic.reduce_monos(lrtoks); lrtoks 

['True', 'AND', 'True'] 

""" 

i = 0 

while i < len(lrtoks): 

if lrtoks[i] == 'NOT': 

args = [lrtoks[i], lrtoks[i + 1]] 

lrtoks[i] = eval_mon_op(args) 

del lrtoks[i + 1] 

i += 1 

 

def eval_mon_op(args): 

r""" 

Return a boolean value based on the truth table of the operator 

in ``args``. 

 

INPUT: 

 

- ``args`` -- a list of length 2; this contains the token 'NOT' and 

then a variable name 

 

OUTPUT: 

 

A boolean value to be determined as follows: 

 

- ``True`` if the variable in ``args`` is ``False``. 

 

- ``False`` if the variable in ``args`` is ``True``. 

 

.. NOTE:: 

 

This function is for internal use by the :class:`SymbolicLogic` class. 

 

TESTS:: 

 

sage: log = SymbolicLogic() 

sage: s = log.statement("!(a&b)|!a"); s 

[['OPAREN', 'NOT', 'OPAREN', 'a', 'AND', 'b', 'CPAREN', 'OR', 'NOT', 'a', 'CPAREN'], 

{'a': 'False', 'b': 'False'}, 

['a', 'b']] 

sage: sage.logic.logic.eval_mon_op(['NOT', 'a']) 

'True' 

""" 

if args[1] != 'True' and args[1] != 'False': 

val = vars[args[1]] 

else: 

val = args[1] 

 

if val == 'True': 

return 'False' 

else: 

return 'True' 

 

def eval_bin_op(args): 

r""" 

Return a boolean value based on the truth table of the operator 

in ``args``. 

 

INPUT: 

 

- ``args`` -- a list of length 3; this contains a variable name, 

then a binary operator, and then a variable name, in that order 

 

OUTPUT: 

 

A boolean value; this is the evaluation of the operator based on the 

truth values of the variables. 

 

.. NOTE:: 

 

This function is for internal use by the :class:`SymbolicLogic` class. 

 

TESTS:: 

 

sage: log = SymbolicLogic() 

sage: s = log.statement("!(a&b)"); s 

[['OPAREN', 'NOT', 'OPAREN', 'a', 'AND', 'b', 'CPAREN', 'CPAREN'], 

{'a': 'False', 'b': 'False'}, 

['a', 'b']] 

sage: sage.logic.logic.eval_bin_op(['a', 'AND', 'b']) 

'False' 

""" 

if args[0] == 'False': 

lval = 'False' 

elif args[0] == 'True': 

lval = 'True' 

else: 

lval = vars[args[0]] 

 

if args[2] == 'False': 

rval = 'False' 

elif args[2] == 'True': 

rval = 'True' 

else: 

rval = vars[args[2]] 

 

if args[1] == 'AND': 

return eval_and_op(lval, rval) 

elif args[1] == 'OR': 

return eval_or_op(lval, rval) 

elif args[1] == 'IFTHEN': 

return eval_ifthen_op(lval, rval) 

elif args[1] == 'IFF': 

return eval_iff_op(lval, rval) 

 

def eval_and_op(lval, rval): 

r""" 

Apply the 'and' operator to ``lval`` and ``rval``. 

 

INPUT: 

 

- ``lval`` -- a string; this represents the value of the variable 

appearing to the left of the 'and' operator 

 

- ``rval`` -- a string; this represents the value of the variable 

appearing to the right of the 'and' operator 

 

OUTPUT: 

 

The result of applying 'and' to ``lval`` and ``rval`` as a string. 

 

.. NOTE:: 

 

This function is for internal use by the :class:`SymbolicLogic` class. 

 

TESTS:: 

 

sage: sage.logic.logic.eval_and_op('False', 'False') 

'False' 

sage: sage.logic.logic.eval_and_op('False', 'True') 

'False' 

sage: sage.logic.logic.eval_and_op('True', 'False') 

'False' 

sage: sage.logic.logic.eval_and_op('True', 'True') 

'True' 

""" 

if lval == 'False' and rval == 'False': 

return 'False' 

elif lval == 'False' and rval == 'True': 

return 'False' 

elif lval == 'True' and rval == 'False': 

return 'False' 

elif lval == 'True' and rval == 'True': 

return 'True' 

 

def eval_or_op(lval, rval): 

r""" 

Apply the 'or' operator to ``lval`` and ``rval``. 

 

INPUT: 

 

- ``lval`` -- a string; this represents the value of the variable 

appearing to the left of the 'or' operator 

 

- ``rval`` -- a string; this represents the value of the variable 

appearing to the right of the 'or' operator 

 

OUTPUT: 

 

A string representing the result of applying 'or' to ``lval`` and ``rval``. 

 

.. NOTE:: 

 

This function is for internal use by the :class:`SymbolicLogic` class. 

 

TESTS:: 

 

sage: sage.logic.logic.eval_or_op('False', 'False') 

'False' 

sage: sage.logic.logic.eval_or_op('False', 'True') 

'True' 

sage: sage.logic.logic.eval_or_op('True', 'False') 

'True' 

sage: sage.logic.logic.eval_or_op('True', 'True') 

'True' 

""" 

if lval == 'False' and rval == 'False': 

return 'False' 

elif lval == 'False' and rval == 'True': 

return 'True' 

elif lval == 'True' and rval == 'False': 

return 'True' 

elif lval == 'True' and rval == 'True': 

return 'True' 

 

def eval_ifthen_op(lval, rval): 

r""" 

Apply the 'if then' operator to ``lval`` and ``rval``. 

 

INPUT: 

 

- ``lval`` -- a string; this represents the value of the variable 

appearing to the left of the 'if then' operator 

 

- ``rval`` -- a string;t his represents the value of the variable 

appearing to the right of the 'if then' operator 

 

OUTPUT: 

 

A string representing the result of applying 'if then' to 

``lval`` and ``rval``. 

 

.. NOTE:: 

 

This function is for internal use by the :class:`SymbolicLogic` class. 

 

TESTS:: 

 

sage: sage.logic.logic.eval_ifthen_op('False', 'False') 

'True' 

sage: sage.logic.logic.eval_ifthen_op('False', 'True') 

'True' 

sage: sage.logic.logic.eval_ifthen_op('True', 'False') 

'False' 

sage: sage.logic.logic.eval_ifthen_op('True', 'True') 

'True' 

""" 

if lval == 'False' and rval == 'False': 

return 'True' 

elif lval == 'False' and rval == 'True': 

return 'True' 

elif lval == 'True' and rval == 'False': 

return 'False' 

elif lval == 'True' and rval == 'True': 

return 'True' 

 

def eval_iff_op(lval, rval): 

r""" 

Apply the 'if and only if' operator to ``lval`` and ``rval``. 

 

INPUT: 

 

- ``lval`` -- a string; this represents the value of the variable 

appearing to the left of the 'if and only if' operator 

 

- ``rval`` -- a string; this represents the value of the variable 

appearing to the right of the 'if and only if' operator 

 

OUTPUT: 

 

A string representing the result of applying 'if and only if' 

to ``lval`` and ``rval``. 

 

.. NOTE:: 

 

This function is for internal use by the :class:`SymbolicLogic` class. 

 

TESTS:: 

 

sage: sage.logic.logic.eval_iff_op('False', 'False') 

'True' 

sage: sage.logic.logic.eval_iff_op('False', 'True') 

'False' 

sage: sage.logic.logic.eval_iff_op('True', 'False') 

'False' 

sage: sage.logic.logic.eval_iff_op('True', 'True') 

'True' 

""" 

if lval == 'False' and rval == 'False': 

return 'True' 

elif lval == 'False' and rval == 'True': 

return 'False' 

elif lval == 'True' and rval == 'False': 

return 'False' 

elif lval == 'True' and rval == 'True': 

return 'True' 

 

def tokenize(s, toks): 

r""" 

Tokenize ``s`` and place the tokens of ``s`` in ``toks``. 

 

INPUT: 

 

- ``s`` -- a string; this contains a boolean expression 

 

- ``toks`` -- a list; this will be populated with the tokens of ``s`` 

 

OUTPUT: 

 

``None``; the tokens of ``s`` are placed in ``toks``. 

 

.. NOTE:: 

 

This function is for internal use by the :class:`SymbolicLogic` class. 

 

EXAMPLES:: 

 

sage: from sage.logic.logic import tokenize 

sage: toks = [] 

sage: tokenize("(a&b)|c", toks) 

sage: toks 

['OPAREN', 'a', 'AND', 'b', 'CPAREN', 'OR', 'c', 'CPAREN'] 

""" 

i = 0 

while i < len(s): 

tok = "" 

skip = valid = 1 

if s[i] == '(': 

tok = tok_list[0] 

elif s[i] == ')': 

tok = tok_list[1] 

elif s[i] == '&': 

tok = tok_list[2] 

elif s[i] == '|': 

tok = tok_list[3] 

elif s[i] == '!': 

tok = tok_list[4] 

elif s[i:i + 2] == '->': 

tok = tok_list[5] 

skip = 2 

elif s[i:i + 3] == '<->': 

tok = tok_list[6] 

skip = 3 

 

if len(tok) > 0: 

toks.append(tok) 

i += skip 

continue 

else: 

# token is a variable name 

if(s[i] == ' '): 

i += 1 

continue 

 

while i < len(s) and s[i] not in operators and s[i] != ' ': 

tok += s[i] 

i += 1 

 

if len(tok) > 0: 

if tok[0] not in string.letters: 

valid = 0 

for c in tok: 

if c not in string.letters and c not in string.digits and c != '_': 

valid = 0 

 

if valid == 1: 

toks.append(tok) 

vars[tok] = 'False' 

if tok not in vars_order: 

vars_order.append(tok) 

else: 

print('Invalid variable name: ', tok) 

toks = [] 

 

toks.append('CPAREN')