From bed847fab26ed2fee4a8ce646a4d54a2007c427d Mon Sep 17 00:00:00 2001 From: Bill Sommers <14631342+bsommers@users.noreply.github.com> Date: Wed, 11 Nov 2026 21:36:56 -0500 Subject: [PATCH] fix: emitter bounds, OpenAPI subclass indent, :explain core, native/Vampire bound conflicts --- CHANGELOG.md | 18 ++- docs/user-guide/axioms-and-reasoning.md | 2 +- docs/user-guide/editor-and-lsp.md | 2 +- docs/user-guide/repl.md | 1 - docs/user-guide/troubleshooting.md | 5 +- editors/vscode/README.md | 6 +- editors/vscode/scripts/build_extension.sh | 3 +- editors/vscode/snippets/ontologic.json | 67 ++++++----- ontologic.cabal | 1 + src/Emitters/OpenApi.hs | 37 +++--- src/Emitters/SMT.hs | 57 +++++++-- src/Emitters/TPTP.hs | 7 ++ src/FOLTheory.hs | 9 +- src/IR.hs | 30 ++++- src/IR/Clausify.hs | 36 +++++- src/OntoLogic.hs | 43 +++++++ test/RegressionBugsSpec.hs | 136 ++++++++++++++++++++++ 17 files changed, 391 insertions(+), 69 deletions(-) create mode 100644 test/RegressionBugsSpec.hs diff --git a/CHANGELOG.md b/CHANGELOG.md index fba6201..3b1e5a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,9 +9,25 @@ tagged after the fact, on their version-bump commits. ## [1.8.2] - Unreleased -Fixes to the v1.8.1 solver resource controls. +Fixes to the v1.8.1 solver resource controls, plus emitter, REPL and prover fixes. ### Fixed +- **Repeated bounds in generated Python and Java.** A subclass that restated an inherited + bound produced `Field(le=90, ge=0, le=130)` or two `@Max` annotations, which neither + language accepts. The IR now keeps only the tightest lower and upper bound per property. +- **Invalid `openapi.yaml` for subclasses with their own properties.** Their properties + were emitted one level too shallow under `allOf`; they are now nested correctly. +- **`:explain` / `--explain-unsat` answered SAT after `:solve` reported UNSAT.** The unsat-core + query left out the property bound assertions the consistency check makes. It now includes + them, named, so the core cites the conflicting bounds. +- **Native prover and Vampire missed plain numeric-bound conflicts** (a universal `gte 100` + with an existential `lt 50`). Both now receive background axioms for each pair of bounds + that no number satisfies together. The native prover also reads a bare property name in a + comparison as that property of the quantified entity, as the SMT and TPTP backends do, + instead of as a free variable. Conflicts that need real arithmetic (integer gaps) still need + Z3 or CVC5. +- **VS Code snippets expanded to obsolete syntax.** They now use the current grammar, and the + extension README and `build_extension.sh` report the real package version. - **`--solve-timeout` was ignored by CVC5.** `--tlimit` was passed after the `-` stdin marker, where cvc5 ignores options. It now comes before `-`. - **Timeouts behave the same on every backend.** External solvers now also run under a diff --git a/docs/user-guide/axioms-and-reasoning.md b/docs/user-guide/axioms-and-reasoning.md index cae1567..3ea74da 100644 --- a/docs/user-guide/axioms-and-reasoning.md +++ b/docs/user-guide/axioms-and-reasoning.md @@ -157,7 +157,7 @@ A model is one possible world, usually a tiny one. Concepts that are empty in th | `native` | Built into the binary (tableau and resolution). No install needed; honours `-T`; reports unknown on step or clause limits | | `auto` | Tries `z3`, `cvc5`, `vampire` in order, skipping any that is not installed; if none is installed, falls back to `native`. A solver that is installed but fails is reported, not skipped | -Practical guidance: use `z3` or `cvc5` for ontologies with numeric property bounds. On the balance example above, `z3` and `cvc5` report UNSAT while `native` and `vampire` report SAT, because they do not reason about the numeric comparisons the way an SMT solver does. The domain fixtures in `test/fixtures/domains/` are checked with Z3 (then CVC5) for the same reason. For purely taxonomic and relational reasoning, all backends agree. If two provers disagree, trust UNSAT from an SMT solver and treat the other's SAT as incomplete. +Practical guidance: use `z3` or `cvc5` for ontologies with numeric property bounds. `native` and `vampire` have no arithmetic. OntoLogic gives them one background axiom for each pair of numeric bounds in the file that no number can meet together (such as `gte 100` and `lt 50`), so they catch contradictions like the balance example above. They still miss contradictions that need real arithmetic, such as integer gaps (`gt 5` and `lt 6` on an `Integer`) or bounds derived from sums. The domain fixtures in `test/fixtures/domains/` are checked with Z3 (then CVC5) for this reason. For purely taxonomic and relational reasoning, all backends agree. If two provers disagree, trust UNSAT from an SMT solver and treat the other's SAT as incomplete. ## Time and memory limits diff --git a/docs/user-guide/editor-and-lsp.md b/docs/user-guide/editor-and-lsp.md index ce8bfee..3628d6e 100644 --- a/docs/user-guide/editor-and-lsp.md +++ b/docs/user-guide/editor-and-lsp.md @@ -40,7 +40,7 @@ The extension activates for `.logic` files (language id `ontologic`). It finds t ### Features - TextMate syntax highlighting and bracket/comment configuration. -- Snippets for `def-concept`, `def-concept-subclass`, `def-relation`, `axiom`, `axiom-forall`, and `import`. **Caution:** the concept, axiom, and subclass snippets were written for an older syntax (`(subclass-of ...)`, `(axiom ...)`, `=>`, inline property lists, `between`) and expand to forms the current parser rejects. Only `def-relation` and `import` expand to valid code; write concepts and axioms as shown in the [Language reference](language-reference.md). +- Snippets: `def-concept`, `def-concept-subclass`, `def-property`, `def-property-optional`, `def-relation`, `def-individual`, `forall-bound`, `forall-range`, `exists`, and `import`. Each expands to current syntax (see the [Language reference](language-reference.md)). - Everything in the table above via the language server. ### Commands diff --git a/docs/user-guide/repl.md b/docs/user-guide/repl.md index 699421c..46c4dd6 100644 --- a/docs/user-guide/repl.md +++ b/docs/user-guide/repl.md @@ -91,7 +91,6 @@ ontologic> :prove Vehicle Truck - There are no `-T`/`--solve-timeout` or `--solve-memory` controls; for bounded runs use the CLI. - `:solve` always prints the model on SAT; the CLI needs `--inspect-model`. - `:prove` exists only here. -- `:explain` reports "No contradictory axioms or unsat cores detected" when the solver returns an empty core, which can happen even when `:solve` has just reported UNSAT. If so, rely on the `:solve` verdict and read the lint message. - Malformed input prints a parse error with a caret and leaves the session unchanged: ```text diff --git a/docs/user-guide/troubleshooting.md b/docs/user-guide/troubleshooting.md index 6e0afc1..082092a 100644 --- a/docs/user-guide/troubleshooting.md +++ b/docs/user-guide/troubleshooting.md @@ -53,7 +53,7 @@ Contradictions can be reported twice (once per operand order); fix the first and ### SAT from one prover, UNSAT from another -The native prover and Vampire do not interpret numeric comparisons the way Z3 and CVC5 do. An ontology with contradictory numeric bounds that the linter did not catch (for example a universal lower bound and an existential witness below it) is UNSAT under `-p z3` or `-p cvc5` and can look SAT under `-p native` or `-p vampire`. Prefer Z3 or CVC5 whenever properties have numeric bounds. +The native prover and Vampire have no arithmetic. They are told which pairs of numeric bounds in the file exclude each other, so a plain conflict (a universal `gte 100` and an existential witness with `lt 50`) is UNSAT on every backend. A contradiction that needs real arithmetic, such as an integer gap (`gt 5` and `lt 6` on an `Integer`), is UNSAT under `-p z3` or `-p cvc5` and can look SAT under `-p native` or `-p vampire`. Prefer Z3 or CVC5 whenever properties have numeric bounds. ### Auto picked the native prover @@ -71,14 +71,11 @@ The native prover and Vampire do not interpret numeric comparisons the way Z3 an | No warning for `owl`, `shacl`, `smt`, `neo4j`, ... | They do not warn by default; add `--warn-unmapped` | | Output file overwritten | File names are fixed per target; use a separate `-o` directory per ontology | | `mmdc ... not found or failed` / `d2 CLI not found or failed` | Install the renderer or drop `--render`; see [Visualization](visualization.md) | -| Generated Python or Java has the same bound repeated | Known limitation (as of 1.8.2): when a subclass restates a bound on a property it inherits, for example `Field(le=90, ge=0, le=130)`, the emitter lists both the subclass and inherited bounds. Python rejects a repeated keyword and Java rejects a repeated annotation. Put each bound on one concept only | -| `openapi.yaml` fails to parse when a subclass has its own properties | Known limitation (as of 1.8.2): the properties of such a subclass are emitted at the wrong indentation. Check generated YAML before use, or use `jsonschema` | ## REPL and editor | Symptom | Fix | |---------|-----| -| REPL `:explain` says "No contradictory axioms" right after `:solve` reported UNSAT | The solver returned an empty core; rely on `:solve` and the lint message | | REPL form spans several lines and fails | Each REPL line is parsed on its own; put the whole form on one line | | Editor shows no diagnostics | Check the "OntoLogic" output channel; run "OntoLogic: Restart Language Server"; make sure `ontologic-lsp` is found (set `ontologic.lsp.path`) | | Editor reports undeclared library concepts | `ONTOLOGIC_PATH` must be set in the environment the editor was launched from | diff --git a/editors/vscode/README.md b/editors/vscode/README.md index 9cb4447..529baf9 100644 --- a/editors/vscode/README.md +++ b/editors/vscode/README.md @@ -12,16 +12,16 @@ Official Visual Studio Code extension for **OntoLogic** — First-Order Logic Do - **Context-Aware Autocompletion**: Keywords, primitive types, concept classes, and properties. - **Type Definition & Hover**: Hover over concept names to inspect class inheritance and typed bounds. - **Jump to Definition**: Jump directly to concept declarations across files. -- 🧩 **Code Snippets**: Quick templates for `def-concept`, `subclass-of`, `def-relation`, `axiom`, and `import`. +- 🧩 **Code Snippets**: Quick templates for `def-concept` (with or without a parent), `def-property`, `def-relation`, `def-individual`, `forall` bound/range axioms, `exists` axioms, and `import`. - 🔍 **Interactive SMT Solver Integration**: Trigger Z3 and CVC5 satisfiability checks and concept emptiness tests directly from the editor command palette. --- ## Installation & Setup -1. Install the `ontologic-1.0.0.vsix` extension package: +1. Install the `ontologic-1.3.0.vsix` extension package: ```bash - code --install-extension ontologic-1.0.0.vsix + code --install-extension ontologic-1.3.0.vsix ``` 2. Ensure `ontologic-lsp` is in your `PATH` (e.g., `~/.local/bin/ontologic-lsp`) or configure `ontologic.lsp.path` in Settings. diff --git a/editors/vscode/scripts/build_extension.sh b/editors/vscode/scripts/build_extension.sh index 30e2e15..e2e97e5 100755 --- a/editors/vscode/scripts/build_extension.sh +++ b/editors/vscode/scripts/build_extension.sh @@ -15,4 +15,5 @@ npm run compile echo "==> Packaging VSIX extension..." npx @vscode/vsce package --no-git-tag-version -echo "==> VSIX Package created at $EXT_DIR/ontologic-1.0.0.vsix" +VERSION="$(node -p "require('./package.json').version")" +echo "==> VSIX Package created at $EXT_DIR/ontologic-$VERSION.vsix" diff --git a/editors/vscode/snippets/ontologic.json b/editors/vscode/snippets/ontologic.json index d526e86..8120210 100644 --- a/editors/vscode/snippets/ontologic.json +++ b/editors/vscode/snippets/ontologic.json @@ -1,48 +1,63 @@ { "Define Concept": { "prefix": "def-concept", - "body": [ - "(def-concept ${1:ConceptName}", - " (${2:propertyName} ${3|String,Integer,Float,Double,Boolean,DateTime,Decimal,URI|})$0)" - ], - "description": "Define a new ontology concept with properties" + "body": ["(def-concept ${1:ConceptName})$0"], + "description": "Define a new ontology concept" }, - "Define Concept with Subclass": { + "Define Concept with Parent": { "prefix": "def-concept-subclass", - "body": [ - "(def-concept ${1:SubclassName} (subclass-of ${2:ParentName})", - " (${3:propertyName} ${4|String,Integer,Float,Double,Boolean,DateTime,Decimal,URI|})$0)" - ], - "description": "Define a new ontology subclass inheriting from a parent concept" + "body": ["(def-concept ${1:SubclassName} ${2:ParentName})$0"], + "description": "Define a concept that specialises (is subsumed by) a parent concept" + }, + "Define Property": { + "prefix": "def-property", + "body": ["(def-property ${1:propertyName} ${2:ConceptName} ${3|Integer,Float,Double,String,Boolean|})$0"], + "description": "Define a typed data property on a concept" + }, + "Define Optional Property": { + "prefix": "def-property-optional", + "body": ["(def-property ${1:propertyName} ${2:ConceptName} ${3|Integer,Float,Double,String,Boolean|} :cardinality ${4|0..1,1..1,0..*,1..*|})$0"], + "description": "Define a data property with an explicit cardinality" }, "Define Relation": { "prefix": "def-relation", + "body": ["(def-relation ${1:relationName} ${2:DomainConcept} ${3:RangeConcept})$0"], + "description": "Define a binary relational association between concepts" + }, + "Define Individual": { + "prefix": "def-individual", + "body": ["(def-individual ${1:individualName} ${2:ConceptName})$0"], + "description": "Declare a named individual of a concept" + }, + "Universal Bound Axiom": { + "prefix": "forall-bound", "body": [ - "(def-relation ${1:relationName} ${2:DomainConcept} ${3:RangeConcept})" + "(forall (${1:x})", + " (implies (${2:ConceptName} ${1:x})", + " (${3|gte,gt,lte,lt,eq,ne|} ${4:propertyName} ${5:0})))$0" ], - "description": "Define a binary relational association between concepts" + "description": "Every instance of a concept satisfies a bound on a property" }, - "Axiom Constraint": { - "prefix": "axiom", + "Universal Range Axiom": { + "prefix": "forall-range", "body": [ - "(axiom (${1|gt,gte,lt,lte,eq,neq,between|} (${2:propertyName} ${3:x}) ${4:bound}))" + "(forall (${1:x})", + " (implies (${2:ConceptName} ${1:x})", + " (and (gte ${3:propertyName} ${4:0}) (lte ${3:propertyName} ${5:100}))))$0" ], - "description": "Define an axiomatic constraint on a concept property" + "description": "Every instance of a concept keeps a property within [min, max]" }, - "Axiom Universal Quantifier": { - "prefix": "axiom-forall", + "Existential Axiom": { + "prefix": "exists", "body": [ - "(axiom (forall (${1:x})", - " (=> (${2:ConceptName} ${1:x})", - " (and (${3|gt,gte,lt,lte,eq,neq,between|} (${4:propertyName} ${1:x}) ${5:bound})))))" + "(exists (${1:x})", + " (and (${2:ConceptName} ${1:x}) (${3|gte,gt,lte,lt,eq,ne|} ${4:propertyName} ${5:0})))$0" ], - "description": "Define a quantified first-order logic axiom" + "description": "Assert that some instance of a concept exists with a property condition" }, "Import File": { "prefix": "import", - "body": [ - "(import \"${1:ontologic-lib/bfo_2020.logic}\")" - ], + "body": ["(import \"${1:ontologic-lib/bfo_2020.logic}\")$0"], "description": "Import an external ontology definition file" } } diff --git a/ontologic.cabal b/ontologic.cabal index 9334651..0dcad90 100644 --- a/ontologic.cabal +++ b/ontologic.cabal @@ -212,6 +212,7 @@ test-suite ontologic-test ProtobufSpec PythonSpec RegistrySpec + RegressionBugsSpec RelationSpec REPLSpec ResolutionSpec diff --git a/src/Emitters/OpenApi.hs b/src/Emitters/OpenApi.hs index 0cf5c51..7a73a69 100644 --- a/src/Emitters/OpenApi.hs +++ b/src/Emitters/OpenApi.hs @@ -52,10 +52,12 @@ emitOpenApiSchema cls = propsBlock :: String -> [CompiledProperty] -> String propsBlock indent ps | null ps = indent ++ "properties: {}\n" - | otherwise = indent ++ "properties:\n" ++ concatMap emitOpenApiField ps + | otherwise = indent ++ "properties:\n" ++ concatMap (emitOpenApiField (indent ++ " ")) ps -emitOpenApiField :: CompiledProperty -> String -emitOpenApiField p = +-- | Emit one property entry; @ind@ is the indentation of the property key, which must sit one +-- level under its @properties:@ key (deeper for subclasses, whose properties live in @allOf@). +emitOpenApiField :: String -> CompiledProperty -> String +emitOpenApiField ind p = let fName = sanitizeIdent (compPropName p) pType = compPropType p constraints = compConstraints p @@ -66,24 +68,25 @@ emitOpenApiField p = PTCustom c -> indent ++ "$ref: '#/components/schemas/" ++ sanitizeIdent c ++ "'\n" _ -> indent ++ "type: " ++ openApiTypeName pType ++ "\n" ++ concatMap (emitOpenApiConstraint indent) constraints + ind2 = ind ++ " " in case compCardinality p of CardZeroOrMore -> - " " ++ fName ++ ":\n" ++ - " type: array\n" ++ - defLine " " ++ - " items:\n" ++ - typeLines " " + ind ++ fName ++ ":\n" ++ + ind2 ++ "type: array\n" ++ + defLine ind2 ++ + ind2 ++ "items:\n" ++ + typeLines (ind2 ++ " ") CardOneOrMore -> - " " ++ fName ++ ":\n" ++ - " type: array\n" ++ - " minItems: 1\n" ++ - defLine " " ++ - " items:\n" ++ - typeLines " " + ind ++ fName ++ ":\n" ++ + ind2 ++ "type: array\n" ++ + ind2 ++ "minItems: 1\n" ++ + defLine ind2 ++ + ind2 ++ "items:\n" ++ + typeLines (ind2 ++ " ") _ -> - " " ++ fName ++ ":\n" ++ - defLine " " ++ - typeLines " " + ind ++ fName ++ ":\n" ++ + defLine ind2 ++ + typeLines ind2 formatOpenApiDefault :: PropertyType -> String -> String formatOpenApiDefault PTString s = show s diff --git a/src/Emitters/SMT.hs b/src/Emitters/SMT.hs index d06e46c..3713c32 100644 --- a/src/Emitters/SMT.hs +++ b/src/Emitters/SMT.hs @@ -1,6 +1,7 @@ module Emitters.SMT ( emitSMT , emitSMTDeclarations + , emitSMTDeclarationsNamed , emitPropertyFunction , smtNumeral , mapSmtType @@ -61,16 +62,56 @@ emitPropertyFunction cName p = in decl ++ concatMap (emitPropertyAssertion cName fName pType) (compConstraints p) emitPropertyAssertion :: String -> String -> PropertyType -> BoundConstraint -> String -emitPropertyAssertion cName fName pType (BoundConstraint op val) = +emitPropertyAssertion cName fName pType c = + "(assert " ++ propertyAssertionBody cName fName pType c ++ ")\n" + +propertyAssertionBody :: String -> String -> PropertyType -> BoundConstraint -> String +propertyAssertionBody cName fName pType (BoundConstraint op val) = let app = "(" ++ fName ++ " " ++ cName ++ ")" lit = smtNumeral pType val - in case op of - OpGt -> "(assert (> " ++ app ++ " " ++ lit ++ "))\n" - OpGte -> "(assert (>= " ++ app ++ " " ++ lit ++ "))\n" - OpLt -> "(assert (< " ++ app ++ " " ++ lit ++ "))\n" - OpLte -> "(assert (<= " ++ app ++ " " ++ lit ++ "))\n" - OpEq -> "(assert (= " ++ app ++ " " ++ lit ++ "))\n" - OpNe -> "(assert (distinct " ++ app ++ " " ++ lit ++ "))\n" + rel = case op of + OpGt -> ">" + OpGte -> ">=" + OpLt -> "<" + OpLte -> "<=" + OpEq -> "=" + OpNe -> "distinct" + in "(" ++ rel ++ " " ++ app ++ " " ++ lit ++ ")" + +-- | 'emitSMTDeclarations' with every property bound assertion named, so an unsat core can +-- cite it. Returns the SMT text plus (name, description) pairs for each named bound. +-- The consistency check asserts these bounds, so the unsat-core theory must assert them too, +-- or ':explain' answers SAT for a contradiction ':solve' reported. +emitSMTDeclarationsNamed :: Registry -> (String, [(String, String)]) +emitSMTDeclarationsNamed reg = + let modelIR = compileRegistry reg + classes = irClasses modelIR + bounds = [ (cls, p, c) | cls <- classes, p <- compProperties cls, c <- compConstraints p ] + named = [ ( nm + , "(assert (! " ++ propertyAssertionBody cName fName (compPropType p) c ++ " :named " ++ nm ++ "))\n" + , "Property bound: " ++ compClassName cls ++ "." ++ compPropName p ++ " " + ++ compOpToString (constraintOp c) ++ " " ++ valueToString (constraintValue c) + ) + | (idx, (cls, p, c)) <- zip ([1..] :: [Int]) bounds + , let cName = sanitizeIdent (compClassName cls) + fName = cName ++ "_" ++ sanitizeIdent (compPropName p) + nm = "bound_" ++ show idx + ] + funDecls = concat [ "(declare-fun " ++ sanitizeIdent (compClassName cls) ++ "_" ++ sanitizeIdent (compPropName p) + ++ " (Entity) " ++ mapSmtType (compPropType p) ++ ")\n" + | cls <- classes, p <- compProperties cls ] + txt = "; Sort declarations\n" ++ + "(declare-sort Entity 0)\n\n" ++ + "; Entity Concept Constants\n" ++ + concatMap emitConceptConst classes ++ "\n" ++ + "; Subclass Hierarchy\n" ++ + "(declare-fun subClassOf (Entity Entity) Bool)\n" ++ + concatMap emitHierarchy classes ++ "\n" ++ + "; Property Functions and Bound Assertions\n" ++ + funDecls ++ concat [ a | (_, a, _) <- named ] ++ "\n" ++ + "; Relational Declarations\n" ++ + emitRelations (irRelations modelIR) ++ "\n" + in (txt, [ (n, d) | (n, _, d) <- named ]) -- | SMT-LIB2 numeral: plain decimal notation, reals keep a fraction, negatives as (- n). smtNumeral :: PropertyType -> Value -> String diff --git a/src/Emitters/TPTP.hs b/src/Emitters/TPTP.hs index 092c4e4..f4dbab0 100644 --- a/src/Emitters/TPTP.hs +++ b/src/Emitters/TPTP.hs @@ -27,6 +27,12 @@ emitTPTP reg = | (idx, ax) <- zip ([1..] :: [Int]) (axioms reg) , Right s <- [folToTPTP env [] ax] ] + -- FOF has no arithmetic; state which numeric bounds exclude each other. + arithAxioms = concat + [ "fof(arith_" ++ show i ++ ", axiom, (! [T] : ~(cmp_" ++ op1 ++ "(T, " ++ tptpConst a ++ ") & cmp_" + ++ op2 ++ "(T, " ++ tptpConst b ++ ")))).\n" + | (i, ((op1, a), (op2, b))) <- zip ([1..] :: [Int]) (disjointBoundPairs (axioms reg)) + ] header = "% TPTP First-Order Form (FOF) generated by OntoLogic\n\n" sections = filter (not . null) [ subclasses @@ -34,6 +40,7 @@ emitTPTP reg = , propSigs , indSigs , concat axEntries + , arithAxioms ] in header ++ intercalate "\n" sections diff --git a/src/FOLTheory.hs b/src/FOLTheory.hs index 93973d4..219268c 100644 --- a/src/FOLTheory.hs +++ b/src/FOLTheory.hs @@ -38,7 +38,7 @@ import qualified Data.Map as Map import qualified Data.Set as Set import Data.Containers.ListUtils (nubOrd) import OntoLogic -import Emitters.SMT (smtNumeral, mapSmtType) +import Emitters.SMT (smtNumeral, mapSmtType, emitSMTDeclarationsNamed) data TheoryOptions = TheoryOptions { optFreeConstants :: Bool @@ -215,8 +215,11 @@ buildNamedTheoryWithOptions opts reg = decls = "(set-option :produce-unsat-cores true)\n" ++ "(set-logic ALL)\n" ++ - "(declare-sort Entity 0)\n" ++ + boundDecls ++ commonDecls env funs + -- Same declarations and property bounds the consistency check asserts (see + -- Solver.emitSMTForConsistency), with the bounds named so a core can cite them. + (boundDecls, boundEntries) = emitSMTDeclarationsNamed reg hierPairs = [ (c, p) | (c, Just p) <- nubOrd (concepts reg), p `elem` conceptNames ] hierEntries = [ ( "hier_" ++ sanitizeIdent c ++ "_" ++ sanitizeIdent p , "(assert (! (forall ((x Entity)) (=> (" ++ conceptPred c ++ " x) (" ++ conceptPred p ++ " x))) :named hier_" ++ sanitizeIdent c ++ "_" ++ sanitizeIdent p ++ "))\n" @@ -238,7 +241,7 @@ buildNamedTheoryWithOptions opts reg = | (idx, (ax, Right s)) <- zip ([1..] :: [Int]) axResults ] namedAsserts = concat [ stmt | (_, stmt, _) <- hierEntries ++ indEntries ++ axEntries ] - nameMap = Map.fromList [ (name, desc) | (name, _, desc) <- hierEntries ++ indEntries ++ axEntries ] + nameMap = Map.fromList ([ (name, desc) | (name, _, desc) <- hierEntries ++ indEntries ++ axEntries ] ++ boundEntries) smt = decls ++ namedAsserts ++ "(check-sat)\n(get-unsat-core)\n" in (smt, nameMap) diff --git a/src/IR.hs b/src/IR.hs index 95c1c80..e51cff0 100644 --- a/src/IR.hs +++ b/src/IR.hs @@ -5,6 +5,8 @@ import GHC.Generics import Data.Graph (graphFromEdges, topSort) import qualified Data.Map as Map import qualified Data.Set as Set +import Data.List (maximumBy, minimumBy, nub) +import Data.Ord (comparing) import OntoLogic -- | Compiled representation of a single property attached to a class @@ -147,7 +149,7 @@ resolveProperty name conceptsList localMap mergedMap p -- | Group raw (prop, op, val) string triples into typed BoundConstraint records preserving order groupConstraints :: [(Name, String, String)] -> Map.Map Name [BoundConstraint] groupConstraints consts = - Map.fromListWith (\new old -> old ++ new) + Map.map tightenBounds $ Map.fromListWith (\new old -> old ++ new) [ (prop, [BoundConstraint op val]) | (prop, opStr, valStr) <- consts , Just op <- [parseCompOp opStr] @@ -205,3 +207,29 @@ formatUnmappedWarning style ir xmlSafe ('-' : '-' : rest) = "- " ++ xmlSafe ('-' : rest) xmlSafe (c : rest) = c : xmlSafe rest xmlSafe [] = [] + +-- | A subclass that restates an inherited bound (or a concept that states two bounds of the +-- same direction) yields several lower or upper bounds for one property. Emitters map each +-- bound to a keyword or annotation that may appear only once (pydantic kwargs, Java @Min, +-- OpenAPI keys), so keep only the tightest bound in each direction. Ties go to the strict +-- operator. Equality and disequality constraints are kept, deduplicated. First-appearance +-- order is preserved. +tightenBounds :: [BoundConstraint] -> [BoundConstraint] +tightenBounds cs = nub (go False False cs) + where + lowers = filter (isLower . constraintOp) cs + uppers = filter (isUpper . constraintOp) cs + bestLower = maximumBy (comparing (\c -> (numVal c, strictness c))) lowers + bestUpper = minimumBy (comparing (\c -> (numVal c, negate (strictness c)))) uppers + go _ _ [] = [] + go seenL seenU (c:rest) + | isLower (constraintOp c) = if seenL then go seenL seenU rest else bestLower : go True seenU rest + | isUpper (constraintOp c) = if seenU then go seenL seenU rest else bestUpper : go seenL True rest + | otherwise = c : go seenL seenU rest + isLower op = op `elem` [OpGt, OpGte] + isUpper op = op `elem` [OpLt, OpLte] + strictness c = if constraintOp c `elem` [OpGt, OpLt] then 1 else (0 :: Int) + numVal c = case constraintValue c of + VInt n -> fromIntegral n + VDouble d -> d + _ -> 0 :: Double diff --git a/src/IR/Clausify.hs b/src/IR/Clausify.hs index b9deead..7d672b2 100644 --- a/src/IR/Clausify.hs +++ b/src/IR/Clausify.hs @@ -9,7 +9,7 @@ module IR.Clausify import qualified Data.Map as Map import qualified Data.Set as Set -import OntoLogic (Name, FOL(..), Term(..), Registry(..), RelationDef(..)) +import OntoLogic (Name, FOL(..), Term(..), Registry(..), RelationDef(..), PropertyDef(..), disjointBoundPairs) import IR.CNF -- | State tracking variable renaming and Skolem constant/function generation @@ -229,8 +229,40 @@ clausifyTheory reg = runClausifyM $ do ] -- 3. Explicit logical axioms from registry - let allAxioms = taxonomyAxioms ++ relationDomainAxioms ++ relationRangeAxioms ++ axioms reg + let propNames = map propName (properties reg) + regAxioms = map (bindPropertyTerms propNames []) (axioms reg) + -- 4. Arithmetic background: comparison predicates are uninterpreted here, so state which + -- numeric bounds exclude each other (see 'disjointBoundPairs'). + let arithAxioms = + [ ForAll "t" (Not (And (Predicate op1 [Var "t", Constant a]) (Predicate op2 [Var "t", Constant b]))) + | ((op1, a), (op2, b)) <- disjointBoundPairs (axioms reg) + ] + let allAxioms = taxonomyAxioms ++ relationDomainAxioms ++ relationRangeAxioms ++ regAxioms ++ arithAxioms clauseLists <- mapM clausifyFOL allAxioms let flatClauses = concat clauseLists uniqueNonTautologyClauses = Set.fromList (map canonicalizeClause (filter (not . isTautology) flatClauses)) return (CNFTheory uniqueNonTautologyClauses) + +-- | In @(gte pressure 100)@ the property name is written bare. Read it as the property of the +-- innermost quantified variable, @prop_pressure(p)@, as the SMT and TPTP backends do. Left +-- alone it would be closed as a fresh universal variable, making every entity share one value. +bindPropertyTerms :: [Name] -> [Name] -> FOL -> FOL +bindPropertyTerms props bound f = case f of + Predicate op [Var p, Constant c] | isBound op p -> Predicate op [propTerm p, Constant c] + Predicate op [Constant c, Var p] | isBound op p -> Predicate op [Constant c, propTerm p] + And a b -> And (rec a) (rec b) + Or a b -> Or (rec a) (rec b) + Implies a b -> Implies (rec a) (rec b) + Iff a b -> Iff (rec a) (rec b) + Not a -> Not (rec a) + ForAll v b -> ForAll v (bindPropertyTerms props (v : bound) b) + Exists v b -> Exists v (bindPropertyTerms props (v : bound) b) + TypedForAll v s b -> TypedForAll v s (bindPropertyTerms props (v : bound) b) + TypedExists v s b -> TypedExists v s (bindPropertyTerms props (v : bound) b) + other -> other + where + rec = bindPropertyTerms props bound + isBound op p = op `elem` ["gt", "gte", "lt", "lte"] && p `elem` props && p `notElem` bound + propTerm p = case bound of + (subject : _) -> Apply ("prop_" ++ p) [Var subject] + [] -> Apply ("prop_" ++ p) [] diff --git a/src/OntoLogic.hs b/src/OntoLogic.hs index acd9179..11e96c1 100644 --- a/src/OntoLogic.hs +++ b/src/OntoLogic.hs @@ -460,3 +460,46 @@ formatConditional p c = "if " ++ formatFOL p ++ " then " ++ formatFOL c + +-- | Numeric bound literals @op(property, constant)@ in the axioms, normalised so the property +-- is the left operand. Only the four ordering operators with numeric constants are collected. +numericBoundLiterals :: [FOL] -> [(String, String)] +numericBoundLiterals = nubOrd . concatMap go + where + ordOps = ["gt", "gte", "lt", "lte"] + go f = case f of + Predicate op [Var _, Constant c] | op `elem` ordOps, numeric c -> [(op, c)] + Predicate op [Constant c, Var _] | op `elem` ordOps, numeric c -> [(flipOp op, c)] + And a b -> go a ++ go b + Or a b -> go a ++ go b + Implies a b -> go a ++ go b + Iff a b -> go a ++ go b + Not a -> go a + ForAll _ b -> go b + Exists _ b -> go b + TypedForAll _ _ b -> go b + TypedExists _ _ b -> go b + _ -> [] + numeric c = (readMaybe c :: Maybe Double) /= Nothing + flipOp "gt" = "lt" + flipOp "gte" = "lte" + flipOp "lt" = "gt" + flipOp "lte" = "gte" + flipOp o = o + +-- | Pairs of numeric bound literals that no real number satisfies together (a lower bound above +-- an upper bound, or meeting it with a strict side). Provers without arithmetic (the native +-- prover, Vampire on FOF) treat comparison predicates as uninterpreted, so callers assert +-- @forall t. not (op1(t, a) and op2(t, b))@ for each pair. These are valid over the reals and +-- hence over the integers too, so they never make a satisfiable theory unsatisfiable; they are +-- incomplete (integer gaps such as gt 5 and lt 6 are not detected). +disjointBoundPairs :: [FOL] -> [((String, String), (String, String))] +disjointBoundPairs axs = + let lits = numericBoundLiterals axs + num c = maybe 0 id (readMaybe c :: Maybe Double) + lowers = [ l | l@(op, _) <- lits, op `elem` ["gt", "gte"] ] + uppers = [ u | u@(op, _) <- lits, op `elem` ["lt", "lte"] ] + in [ (l, u) + | l@(lop, a) <- lowers, u@(uop, b) <- uppers + , num a > num b || (num a == num b && (lop == "gt" || uop == "lt")) + ] diff --git a/test/RegressionBugsSpec.hs b/test/RegressionBugsSpec.hs new file mode 100644 index 0000000..a2e795a --- /dev/null +++ b/test/RegressionBugsSpec.hs @@ -0,0 +1,136 @@ +-- | Regression tests for defects found while writing the 1.8.2 user guide: +-- emitter output for subclasses, the REPL/CLI unsat-core explanation, and numeric-bound +-- contradictions on provers without arithmetic. +module RegressionBugsSpec (spec) where + +import Test.Hspec +import Data.List (isInfixOf, isPrefixOf) +import Data.Text (pack) +import System.Directory (findExecutable) +import Text.Megaparsec (parse, errorBundlePretty) +import OntoLogic +import Parser (parseRegistry) +import IR +import Solver (checkConsistencyWith, explainUnsatWith, Prover(..), UnsatExplanation(..)) +import Emitters.Java (emitJavaWithPackage) +import Emitters.OpenApi (emitOpenApi) +import Emitters.Python (emitPython) +import Emitters.TPTP (emitTPTP) + +load :: String -> Registry +load src = either (error . errorBundlePretty) id (parse parseRegistry "t" (pack src)) + +-- | A subclass that restates an inherited bound and adds a property of its own. +truckReg :: Registry +truckReg = load $ unlines + [ "(def-concept Vehicle)" + , "(def-concept Truck Vehicle)" + , "(def-property age Vehicle Integer)" + , "(def-property max_load Truck Double)" + , "(forall (v) (implies (Vehicle v) (and (gte age 0) (lte age 130))))" + , "(forall (t) (implies (Truck t) (lte age 90)))" + , "(forall (t) (implies (Truck t) (gt max_load 0.5)))" + ] + +-- | A universal lower bound contradicted by an existential witness. +pumpConflictReg :: Registry +pumpConflictReg = load $ unlines + [ "(def-concept Pump)" + , "(def-property pressure Pump Integer)" + , "(forall (p) (implies (Pump p) (gte pressure 100)))" + , "(exists (p) (and (Pump p) (lt pressure 50)))" + ] + +-- | Disjoint bounds on different concepts' instances: satisfiable, must stay SAT. +pumpValveReg :: Registry +pumpValveReg = load $ unlines + [ "(def-concept Pump)" + , "(def-concept Valve)" + , "(def-property pressure Pump Integer)" + , "(def-individual p1 Pump)" + , "(def-individual v1 Valve)" + , "(forall (p) (implies (Pump p) (gte pressure 100)))" + , "(forall (v) (implies (Valve v) (lt pressure 50)))" + ] + +sensorReg :: Registry +sensorReg = load $ unlines + [ "(def-concept Device)" + , "(def-concept SmartSensor Device)" + , "(def-property batteryLevel SmartSensor Integer)" + , "(forall (s) (implies (SmartSensor s) (and (gte batteryLevel 0) (lte batteryLevel 100))))" + , "(forall (s) (implies (SmartSensor s) (gt batteryLevel 100)))" + ] + +constraintsOf :: Name -> Name -> Registry -> [BoundConstraint] +constraintsOf cls prop reg = + concat [ compConstraints p + | c <- irClasses (compileRegistry reg), compClassName c == cls + , p <- compProperties c, compPropName p == prop ] + +count :: String -> String -> Int +count needle hay = length [ () | t <- tailsOf hay, needle `isPrefixOf` t ] + where tailsOf s = case s of { [] -> [[]]; (_:r) -> s : tailsOf r } + +withProver :: String -> IO () -> IO () +withProver exe act = findExecutable exe >>= maybe (pendingWith (exe ++ " not installed")) (const act) + +isUnsat, isSat :: SolverResult -> Bool +isUnsat Unsat = True +isUnsat _ = False +isSat (Sat _) = True +isSat _ = False + +spec :: Spec +spec = do + describe "restated inherited bounds" $ do + it "keeps only the tightest bound in each direction" $ + constraintsOf "Truck" "age" truckReg `shouldBe` + [BoundConstraint OpLte (VInt 90), BoundConstraint OpGte (VInt 0)] + it "prefers the strict operator on a tie" $ + tightenBounds [BoundConstraint OpGte (VInt 5), BoundConstraint OpGt (VInt 5)] + `shouldBe` [BoundConstraint OpGt (VInt 5)] + it "keeps a contradiction it cannot resolve (lower above upper)" $ + constraintsOf "SmartSensor" "batteryLevel" sensorReg `shouldBe` + [BoundConstraint OpGt (VInt 100), BoundConstraint OpLte (VInt 100)] + it "emits each pydantic keyword once" $ do + let py = emitPython truckReg + py `shouldContain` "age: int = Field(le=90, ge=0)" + py `shouldNotContain` "ge=0, le=130)\n max_load" + it "emits a single @Max on the Java subclass" $ + case lookup "Truck" (emitJavaWithPackage "p" truckReg) of + Nothing -> expectationFailure "no Truck class emitted" + Just j -> do + count "@Max(" j `shouldBe` 1 + count "@Min(" j `shouldBe` 1 + + describe "OpenAPI subclass properties" $ do + let yaml = lines (emitOpenApi truckReg) + it "nests each property one level under its allOf properties key" $ do + yaml `shouldContain` [" properties:"] + yaml `shouldContain` [" max_load:"] + yaml `shouldContain` [" type: number"] + it "leaves no subclass property at the base-schema indent" $ + filter (" max_load:" ==) yaml `shouldBe` [] + + describe "unsat-core explanation agrees with the consistency check" $ do + it "cites the conflicting property bounds (Z3)" $ withProver "z3" $ do + checkConsistencyWith Z3 sensorReg >>= (`shouldSatisfy` isUnsat) + res <- explainUnsatWith Z3 sensorReg + case res of + Left err -> expectationFailure err + Right e -> do + unsatCoreNames e `shouldSatisfy` (not . null) + unwords (unsatCoreDetails e) `shouldSatisfy` ("SmartSensor.batteryLevel" `isInfixOf`) + + describe "numeric-bound contradictions without arithmetic" $ do + it "native prover finds the forall/exists bound conflict" $ + checkConsistencyWith NativeProver pumpConflictReg >>= (`shouldSatisfy` isUnsat) + it "native prover keeps disjoint bounds on different entities satisfiable" $ + checkConsistencyWith NativeProver pumpValveReg >>= (`shouldSatisfy` isSat) + it "TPTP states that the two bounds exclude each other" $ + emitTPTP pumpConflictReg `shouldSatisfy` ("~(cmp_gte(T, c_100) & cmp_lt(T, c_50))" `isInfixOf`) + it "vampire finds the conflict" $ withProver "vampire" $ + checkConsistencyWith Vampire pumpConflictReg >>= (`shouldSatisfy` isUnsat) + it "vampire keeps disjoint bounds on different entities satisfiable" $ withProver "vampire" $ + checkConsistencyWith Vampire pumpValveReg >>= (`shouldSatisfy` isSat)