/* A -> B ~A ~B (conclusion) */ func SatInvalid(t *testing.T) { na := literal.Literal{"A", true} b := literal.Literal{"B", false} one := clause.Clause{} one.Append(na) two := clause.Clause{} two.Append(b) three := clause.Clause{} three.Append(na) three.Append(b) CS := clauseset.ClauseSet{} CS.Append(one) CS.Append(two) CS.Append(three) CS.Indent = 0 sat, _ := Satisfiable(CS) if !sat { t.Errorf("ClauseSet %q was not Satisfiable, so it is a valid argument (it should be invalid)", CS) } }
//Satisfiable implements above function func Satisfiable(CS clauseset.ClauseSet) (bool, Tree) { tree := Tree{Name: CS.String(), Split: ""} fmt.Printf("\n%sSatisfiable(%s)\n", strings.Repeat(" ", CS.Indent), CS) CS.Indent += 2 //if CS = {} : return true if CS.Len() == 0 { fmt.Printf("%sEmpty ClauseSet, returning true\n", strings.Repeat(" ", CS.Indent)) openTree := Tree{Name: "O", Split: ""} tree.Children = append(tree.Children, openTree) return true, tree } //if {} in CS : return false firstElement, err := CS.FirstElement() if err != nil { log.Fatal(err) } if clause.Len(firstElement) == 0 { fmt.Printf("%s{} found in ClauseSet, returning false\n", strings.Repeat(" ", CS.Indent)) closedTree := Tree{Name: "X", Split: ""} tree.Children = append(tree.Children, closedTree) return false, tree } //select L in lit(CS): return Satisfiable(CS_L) || Satisfiable(CS_L') nextLiteral, err2 := CS.NextLiteral() if err2 != nil { log.Fatal(err2) } fmt.Printf("%sSpliting on %s\n", strings.Repeat(" ", CS.Indent), nextLiteral) CSL := CS.Reduce(nextLiteral) CSL.Indent = CS.Indent CSR := CS.Reduce(nextLiteral.Negation()) CSR.Indent = CS.Indent left, tree_l := Satisfiable(CSL) right, tree_r := Satisfiable(CSR) tree_l.Split = nextLiteral.String() tree_r.Split = nextLiteral.Negation().String() tree.Children = append(tree.Children, tree_l) tree.Children = append(tree.Children, tree_r) return left || right, tree }
//FindValidity finds the validity of the argument (given as a ClauseSet) func FindValidity(CS clauseset.ClauseSet, filename string) { fmt.Printf("Starting ClauseSet: %s\n", CS) CS.Indent = 0 sat, tree := Satisfiable(CS) treeJson, _ := json.Marshal(tree) f, _ := os.Create(filename) f.WriteString(string(treeJson)) fmt.Print("Conclusion: ") if sat { fmt.Printf("Satisfiable(%s) == %t; INVALID\n", CS, sat) } else { fmt.Printf("Satisfiable(%s) == %t; VALID\n", CS, sat) } }