import java.util.*; /** * This program is designed to keep track of * truth values of propositions. It uses Conditionals (>), * Biconditionals (<->), Conjunctions (&), and Disjunctions(v). * It determines whether or not a given proposition is a tautology * and if it is logically equivalent to any other proposition. * It also tests for the validity of arguments. * * @author (Yitzchok Pinkesz & Aaron Koolyk) * @version (01.04.2010) */ public class Logic { // instance variables - replace the example below with your own private HashMap> p; private int length; private String[] keys; private ArrayList values; private boolean full; /** * The constructor uses a number specified by the * user to create a truth table with the given amount * of propositions. It creates every possible combination. * * @param numberOfPropositions */ public Logic() { } public void start(int numberOfPropositions) { full = false; length = (int)(Math.pow(2.0,(double)(numberOfPropositions))); p = new HashMap>(numberOfPropositions); for(Integer i = 0; i < numberOfPropositions; i++) { ArrayList tmp = new ArrayList(length); p.put("" + (i+1),tmp); } Boolean nextval = true; for(int i = 0; i < numberOfPropositions; i++) { for(int j = 0; j < length; j++) { ArrayList tmp = p.get("" + (i+1)); tmp.add(nextval); if((j + 1) % Math.pow(2,i) == 0) nextval = !nextval; } } } public void start(int numberOfPropositions, String[] a) { full = true; length = (int)(Math.pow(2.0,(double)(numberOfPropositions))); p = new HashMap>(numberOfPropositions); for(int i = 0; i < numberOfPropositions; i++) { ArrayList tmp = new ArrayList(length); p.put("" + (a[i]),tmp); } Boolean nextval = true; for(int i = 0; i < numberOfPropositions; i++) { for(int j = 0; j < length; j++) { ArrayList tmp = p.get("" + (a[i])); tmp.add(nextval); if((j + 1) % Math.pow(2,i) == 0) nextval = !nextval; } } } public void initializeKeys(int i) { keys = new String[i]; } public void addKeys(String a) { int i = 0; while(keys[i] != null) { i++; } if(i < keys.length) keys[i] = a; } public void initializeValues() { values = new ArrayList(length); } public void addValues(boolean q) { values.add(q); } /** * Adds a conditional to the list of propositions * * @param a,b two Strings that the user inputs to reference two propositions. */ public String conditional(String a, String b) { ArrayList q = new ArrayList(length); ArrayList tmp1 = p.get(a); ArrayList tmp2 = p.get(b); for(int i = 0; i < length; i++) { if(tmp1.get(i) && !tmp2.get(i)) q.add(false); else q.add(true); } if(!full) { p.put("(" + a + ">" + b + ")",q); return "(" + a + ">" + b + ")"; } else { p.put("if." + a + ".then." + b + ")",q); return "if." + a + ".then." + b + ")"; } } /** * Adds a conjunction to the list of propositions. * * @param a,b two Strings that the user inputs to reference two propositions. */ public String conjunction(String a, String b) { ArrayList q = new ArrayList(length); ArrayList tmp1 = p.get(a); ArrayList tmp2 = p.get(b); for(int i = 0; i < length; i++) { if(tmp1.get(i) && tmp2.get(i)) q.add(true); else q.add(false); } if(!full) { p.put("(" + a + "&" + b + ")",q); return "(" + a + "&" + b + ")"; } else { p.put(a + ".&." + b,q); return a + ".&." + b; } } /** * Adds a disjunction to the list of proposition. * * @param a,b two Strings that the user inputs to reference two propositions. */ public String disjunction(String a, String b) { ArrayList q = new ArrayList(length); ArrayList tmp1 = p.get(a); ArrayList tmp2 = p.get(b); for(int i = 0; i < length; i++) { if(tmp1.get(i) || tmp2.get(i)) q.add(true); else q.add(false); } if(!full) { p.put("(" + a + "v" + b + ")",q); return "(" + a + "v" + b + ")"; } else { p.put("either." + a + ".or." + b + ")",q); return "either." + a + ".or." + b + ")"; } } /** * Adds a biconditional to the list of propositions. * * @param a,b two Strings that the user inputs to reference two propositions. */ public String biconditional(String a, String b) { ArrayList q = new ArrayList(length); ArrayList tmp1 = p.get(a); ArrayList tmp2 = p.get(b); for(int i = 0; i < length; i++) { if(tmp1.get(i).equals(tmp2.get(i))) q.add(true); else q.add(false); } if(!full) { p.put("(" + a + "<->" + b +")",q); return "(" + a + "<->" + b +")"; } else { p.put(a + ".if.and.only.if." + b,q); return a + ".if.and.only.if." + b; } } /** * Adds a negation of a proposition to the list. * * @param a a String that the user inputs to reference the proposition to be negated. */ public String negation(String a) { ArrayList q = new ArrayList(length); ArrayList tmp1 = p.get(a); for(int i = 0; i < length; i++) q.add(!tmp1.get(i)); if(!full) { p.put("~"+ a,q); return "~"+ a; } else { p.put("it.is.not.the.case.that." + a,q); return("it.is.not.the.case.that." + a); } } /** * returns whether or not the given proposition is a tautology. */ public boolean isTautology(String a) { ArrayList tmp = p.get(a); for(int i = 0; i < tmp.size(); i++) if(!tmp.get(i)) return false; return true; } /** * returns true if the two given propositions are logicially equivalent. */ public boolean isLogicallyEquivalent(String a, String b) { ArrayList tmp1 = p.get(a); ArrayList tmp2 = p.get(b); for(int i = 0; i < tmp1.size(); i++) { if(!tmp1.get(i).equals(tmp2.get(i))) return false; } return true; } /** * returns true if the argument is valid. */ public boolean isValidArg(String[] keys,String b) { boolean checker = false; ArrayList therefore = p.get(b); HashMap> premise = new HashMap>(); for(int i = 0; i < keys.length; i++) premise.put(i,p.get(keys[i])); Set key = premise.keySet(); ArrayList> tmp = new ArrayList>(); ArrayList tmpo = new ArrayList(); for(int i = 0; i < length; i++) { for(Integer x : key) { ArrayList temp = premise.get(x); tmpo.add(temp.get(i)); } tmp.add(tmpo); tmpo = new ArrayList(); } for(int i = 0; i < therefore.size(); i++) { if(!therefore.get(i)) { for(int j = 0; j < tmp.get(i).size(); j++) { if(!tmp.get(i).get(j)) checker = true; } if(!checker) return checker; else checker = !checker; } } return true; } /***************************************************** * returns the truth table of the given proposition. */ public ArrayList getP(String key) { return p.get(key); } /** * returns the length of the rows. */ public int getLength() { return length; } /** * returns the number of propositions. */ public int getNumberOfPropositions() { return p.size(); } /** * Allows the user to add a proposition to p. */ public void addP(String key) { p.put(key,values); } /** * Allows the user to print all keys in p. */ public Set getKeys() { return p.keySet(); } public void changeNameP(String original, String changeTo) { ArrayList tmp = p.get(original); p.remove(original); p.put(changeTo,tmp); } public ArrayList findTautology() { Set tmp = p.keySet(); ArrayList ret = new ArrayList(); for(String x : tmp) { if(isTautology(x)) ret.add(x); } return ret; } public ArrayList> findLogicallyEquivalent() { Set tmp1 = p.keySet(); Set tmp2 = tmp1; HashSet> ret = new HashSet>(); for(String i : tmp1) { HashSet tmp3 = new HashSet(); tmp3.add(i); for(String j : tmp2) if(isLogicallyEquivalent(i,j)) { tmp3.add(j); } ret.add(tmp3); } ArrayList> tmp4 = hashSetToArrayList(ret); return tmp4; } private ArrayList> hashSetToArrayList(HashSet> tmp) { ArrayList> tmp1 = new ArrayList>(); for(HashSet a : tmp) { ArrayList tmp2 = new ArrayList(); for(String b : a) { tmp2.add(b); } tmp1.add(tmp2); } return tmp1; } }