1
$\begingroup$

I am going through Kenneth Rosen's book about discrete mathematics. Chapter 1 there is a example where we build functions to check valid conclusions.

Here is the code:

validQ[A_] := Module[{premiseList, premises, i},
premiseList = A[[1 ;; -2]];
premises = Apply[And, premiseList];
TautologyQ[Implies[premises, A[[-1]]]]
]

allCompound[vars_] := Module[{p, q, tempList = vars, propList},
Do[AppendTo[tempList, ! p], {p, vars}];
propList = tempList;
Do[If[! TautologyQ[p && q] && ! TautologyQ[! (p && q)], 
AppendTo[propList, p && q]];
If[! TautologyQ[p || q] && ! TautologyQ[! (p || q)], 
AppendTo[propList, p || q]];
If[! TautologyQ[Implies[p, q]] && ! TautologyQ[! Implies[p, q]], 
AppendTo[propList, Implies[p, q]]]
, {p, tempList}, {q, tempList}];
DeleteDuplicates[propList]]

findConsequences[premises_, level_] := 
Module[{vars, P, possibleC, conclusions = {}, c, i},
vars = getVars[premises];
possibleC = vars;
For[i = 1, i <= level, i++, possibleC = allCompound[possibleC]];
Do[
If[validQ[Append[premises, c]], AppendTo[conclusions, c]], {c, 
possibleC}
];
conclusions
]

findConsequences[{Implies[p && t, r || s], Implies[q, u && t], 
Implies[u, p], ! s}, 1]

This is what I get

Do::iterb: Iterator {p$26269,getVars[{p&&t\[Implies]r||s,q\      [Implies]u&&t,u\[Implies]p,!s}]} does not have appropriate bounds. >>

Do::iterb: Iterator {p$26269,tempList$26269} does not have appropriate bounds. >>

Do::iterb: Iterator {c$26268,possibleC$26268} does not have appropriate bounds. >>

General::stop: Further output of Do::iterb will be suppressed during this calculation. >>

{}

And this is what I should get

{! s, p || ! s, p \[Implies] ! s, p || ! q, p || ! u, t || ! s, 
t \[Implies] ! s, t || ! q, r || ! s, r \[Implies] ! s, r || ! q, 
s \[Implies] p, s \[Implies] t, s \[Implies] r, s \[Implies] q, 
s \[Implies] u, s \[Implies] ! p, s \[Implies] ! t, s \[Implies] ! r,
s \[Implies] ! s, s \[Implies] ! q, s \[Implies] ! u, 
q \[Implies] p, q \[Implies] t, q \[Implies] r, q \[Implies] u, 
q || ! s, q \[Implies] ! s, u \[Implies] p, u || ! s, 
u \[Implies] ! s, 
u || ! q, ! p || ! s, ! p \[Implies] ! s, ! p \[Implies] ! q, ! 
p \[Implies] ! u, ! t || ! s, ! t \[Implies] ! s, ! t \[Implies] ! 
q, ! r || ! s, ! r \[Implies] ! s, ! r \[Implies] ! q, ! s || 
p, ! s || t, ! s || r, ! s || q, ! s || 
u, ! s || ! p, ! s || ! t, ! s || ! r, ! s && ! s, ! s || ! s, ! 
s || ! q, ! s || ! u, ! q || p, ! q || t, ! q || r, ! q || 
u, ! q || ! s, ! q \[Implies] ! s, ! u || 
p, ! u || ! s, ! u \[Implies] ! s, ! u \[Implies] ! q}

I have even tried this by copy pasting the code from the actual textbook files!

What might be the problem?

$\endgroup$
  • $\begingroup$ Did you already contact the author? $\endgroup$ – Yves Klett Mar 8 '16 at 8:51
  • $\begingroup$ No, I have not done that yet. I would like to get this fixed first. I have latest 10.4 version of Mathematica (Student version) $\endgroup$ – Zzz Mar 8 '16 at 8:55
  • 1
    $\begingroup$ Reverse-engineering lots of code is not a very attractive venture, and I would recommend you contact the author, as he might be more interested in fixing bugs than your average user here. $\endgroup$ – Yves Klett Mar 8 '16 at 9:04
3
$\begingroup$

The problem is that you haven't defined the function getVars, which is used to define possibleC, which forms the iterator on your Do list. I tracked down the definition of getVars,

getVars[p_] := Module[{L = {p}, i = 1},
  While[i <= Length[L],
    If[Head[L[[i]]] === Symbol,
     i++,
     L[[i, 0]] = List;
     L = Flatten[L]
     ]
    ]
   DeleteDuplicates[L]
  ]

But I couldn't get it to work. I'm not sure what version the book was written for, but I did figure out what it was trying to do, which is just list all the Symobol objects in the premise, so just use this

getVars[p_] := DeleteDuplicates@Cases[p, _Symbol, Infinity]

Now the code gives the answer you are looking for.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.