gladiusmaximus

truth.py

Sep 4th, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. def tfSeq(size):
  2. ''' Generates a list of the given size containing all combinations
  3. of True and False. Starts with all Trues and ends with all
  4. Falses.
  5. '''
  6.  
  7. if size > 1:
  8. for head in tfSeq(size-1):
  9. yield head + [True]
  10. yield head + [False]
  11. else:
  12. yield [True]
  13. yield [False]
  14.  
  15. def nth_term(seq, idx):
  16. return [elem[idx] for elem in seq]
  17.  
  18. first_term = lambda seq: nth_term(seq, 0)
  19. second_term = lambda seq: nth_term(seq, 1)
  20.  
  21. def truthtable(vars, funcs, conclusion):
  22. output = '\\item\n'
  23. output += '{'
  24. output += '\n'.join(['$' + var + '$: ' + desc + '\\\\' for var, desc in vars])
  25. output += '\\(\n'
  26. output += '\n'.join([func[0] + '\\\\' for func in funcs])
  27. output += '\\line \\\\ \n'
  28. output += conclusion[0] + '\n'
  29. output += '\\)}\n\n'
  30. output += '\\item\n'
  31. output += '\\(\n'
  32.  
  33. lVars = len(vars)
  34. lFuncs = len(funcs)
  35. output += r'\begin{array}[t]{' + '|'.join(['c']*lVars) + '||' + '|'.join(['c']*lFuncs) + '||c}' + '\n'
  36.  
  37. labels = first_term(vars) + first_term(funcs) + first_term([conclusion])
  38. output += '&'.join(labels) + r'\\'
  39. output += '\n\\hline\n\\hline\n'
  40.  
  41. tfText = {True:'T', False:'F'}
  42.  
  43. for tf in tfSeq(len(vars)):
  44. values = [tfText[val] for val in tf]
  45. values += [tfText[func[1](*tf)] for func in funcs]
  46. values += [tfText[conclusion[1](*tf)]]
  47. output += '&'.join(values) + '\\\\\n'
  48.  
  49.  
  50. output += r'\end{array}'
  51. output += '\n\)\n'
  52.  
  53. return output
  54.  
  55. # Setup notes
  56. '''
  57. Install http://www.ctan.org/pkg/python
  58.  
  59. Run latex with --shell-escape 'latex -shell-escape document.tex'
  60.  
  61. The first argument in which you declare variables also determines the order that the arguments come to the lambdas in the second argument. Ex, if your first argument is [('A', ''), ('B', ''), ('C', '')], then all of the lambdas should be lambda A, B, C not lambda A, C, B.
  62.  
  63. All macros (even unary operators) must accept as arguments all of the variables. Ex, lambda A, B, C: not A is correct while lambda A: not A is incorrect.
  64.  
  65. When debugging, consult LATEX_SOURCE.py.out (which holds stdout) and LATEX_SOURCE.py.err (which holds stderr).
  66.  
  67. Be sure to define macros \lif (a -> b), and \liff (a <-> b) like so (\newcommand{\lif}{\rightarrow}). If you don't like the default line, you can always redefine them (\renewcommand{\line}{\\[-1.5ex]\rule{\linewidth}{.4pt}}).
  68.  
  69. The program expects to be run inside of an \begin{enumerate}...\end{enumerate}
  70. '''
  71.  
  72. # Sample input
  73. '''
  74. \begin{enumerate}
  75. \begin{python}[truth.py]
  76. print truthtable(
  77. [('P', 'The patient will die'), ('O', 'We will operate')],
  78. [('\lnot O \lif P', lambda P, O: not (not O) or P), ('O', lambda P, O: O)],
  79. ('\lnot P', lambda P, O: not P)
  80. )
  81. \end{python}
  82. \end{enumerate}
  83. '''
  84.  
  85. # Sample output
  86. '''
  87. \begin{enumerate}
  88. \item
  89. {
  90. $P$: The patient will die\\
  91. $O$: We will operate\\
  92. \(
  93. \lnot O \lif P \\
  94. O \\
  95. \line \\
  96. \lnot P
  97. \)
  98. }
  99.  
  100. \item
  101. \(
  102. \begin{array}[t]{c|c||c|c||cc}
  103. P&O&\lnot O \lif P&O&\lnot P\\
  104. \hline
  105. \hline
  106. T&T&T&T&F&*\\
  107. T&F&T&F&F&\\
  108. F&T&T&T&T&*\\
  109. F&F&F&F&T&\\
  110. \end{array}
  111. \)
  112. \end{enumerate}
  113. '''
Advertisement
Add Comment
Please, Sign In to add comment