Author SHA1 Message Date
Roman Volosovskyi 8cb4beca6b async flow last updates 2016-04-04 13:36:54 +03:00
Roman Volosovskyi b2826dd3ad GI prototype and memory abstraction updates 2016-04-01 18:35:15 +03:00
Roman Volosovskyi d747fb479b system's state 2016-04-01 14:41:12 +03:00
Roman Volosovskyi de1aeace55 fix ::third-fn 2016-04-01 12:40:27 +03:00
Roman Volosovskyi 5ff521769a test flow generator 2016-04-01 12:36:57 +03:00
Roman Volosovskyi 5cb2ce8048 Merge branch 'redis' into async_workflow 2016-04-01 11:35:55 +03:00
Roman Volosovskyi 0cee460fb6 redis mem tests 2016-04-01 11:29:29 +03:00
Roman Volosovskyi b16b68b845 panicking sliding buffer 2016-04-01 10:13:00 +03:00
Roman Volosovskyi 1e274ec874 flow & general inference prototype 2016-03-31 19:20:36 +03:00
Jarrad Hope 85b4de8666 test push 2016-03-29 17:35:07 +02:00
Jarrad Hope af66fdf663 basic travis.yml 2016-03-29 17:30:27 +02:00
Roman Volosovskyi 06a14467de fix typos 2016-03-29 18:07:12 +03:00
Roman Volosovskyi e3988d8454 Memory protocol & first redis implementation 2016-03-29 17:56:54 +03:00
Roman Volosovskyi 191885f13f remove core.match from deriver 2016-03-29 17:35:56 +03:00
Roman Volosovskyi a896281eb2 Merge branch 'deriver' of https://github.com/rasom/opennars2 into deriver 2016-03-18 18:41:53 +02:00
Roman Volosovskyi 2ddbadc029 multiple defrules 2016-03-18 18:40:58 +02:00
Jarrad f867bec1e2 Merge pull request #2 from jarradh/deriver
Update Deriver
2016-03-16 09:18:31 +01:00
Roman Volosovskyi 1da3805c07 small performance improvement 2016-03-15 15:11:43 +02:00
Roman Volosovskyi db7bf9c807 :shift-occurrence-backward/:shift-occurrence-forward 2016-03-14 17:16:32 +02:00
Roman Volosovskyi 769efb0bcb tests fix 2016-03-14 12:19:35 +02:00
Roman Volosovskyi 2e1db6d379 unification optimization & other 2016-03-14 12:09:35 +02:00
Roman Volosovskyi 3175fc57c6 :shift-occurrence-forward/backward first attempt 2016-03-12 12:00:13 +02:00
Roman Volosovskyi bbd331073e :concurrent & other 2016-03-11 15:26:30 +02:00
Roman Volosovskyi 3926a91fb2 :measure-time 2016-03-10 18:15:45 +02:00
Roman Volosovskyi 5ea93b79d1 test fix 2016-03-10 15:43:51 +02:00
Roman Volosovskyi 16ab0408d5 :d/ values & other 2016-03-10 15:01:57 +02:00
Roman Volosovskyi 5161b1d91d :no-common-subterm & :no-set 2016-03-09 21:36:05 +02:00
Roman Volosovskyi 4d7398dd50 conclusions format & :p/judgement 2016-03-09 19:01:24 +02:00
Roman Volosovskyi 6ca166a74c :not-implication-or-equivalence 2016-03-09 15:18:00 +02:00
Roman Volosovskyi c31ca8e3c9 ext-inter reduction fix 2016-03-09 14:13:37 +02:00
Roman Volosovskyi caed8a203c conclusions reduction 2016-03-09 12:42:41 +02:00
Roman Volosovskyi c51668962b reduce functions 2016-03-09 11:46:50 +02:00
Roman Volosovskyi db72eabc8e commutative 2016-03-07 15:10:07 +02:00
Roman Volosovskyi 4934ceff07 deriver test fix 2016-03-06 20:06:49 +02:00
Roman Volosovskyi 0accb32d5b backward inference related updates 2016-03-06 14:30:29 +02:00
Roman Volosovskyi a263820b83 list expansion tests 2016-03-04 16:50:03 +02:00
Roman Volosovskyi 504f10f4f6 rules with Ai to _ substitution 2016-03-04 15:23:32 +02:00
Roman Volosovskyi b245fd7165 :list/B and more comments 2016-03-04 13:41:54 +02:00
Roman Volosovskyi 17fb6a4970 substitution tests & comments 2016-03-04 09:54:27 +02:00
Roman Volosovskyi 9e14ae40a2 preconditions & other 2016-03-03 18:54:36 +02:00
Roman Volosovskyi d5db8f8a78 key-path test fix 2016-03-03 15:19:34 +02:00
Roman Volosovskyi f1cab507b8 :substitute-if-unifies first attempt 2016-03-03 15:10:28 +02:00
Roman Volosovskyi ad6d4fa04e :substitute precondition 2016-03-02 17:30:54 +02:00
Roman Volosovskyi 31fbaef862 :intersection precondition 2016-03-02 14:40:38 +02:00
Roman Volosovskyi 16b7d1b8e1 fix all-paths test 2016-03-02 14:13:23 +02:00
Roman Volosovskyi 2b68e63fd2 union/difference first attempt 2016-03-02 14:06:56 +02:00
Roman Volosovskyi af50e35255 remove check? functions 2016-03-01 18:35:45 +02:00
Roman Volosovskyi 854bc3651a tests for #R reader 2016-03-01 17:35:45 +02:00
Roman Volosovskyi 587887b0a0 remove :require core.logic 2016-03-01 16:00:11 +02:00
Roman Volosovskyi 39c4c66f08 remove logic engine 2016-03-01 15:56:24 +02:00
Roman Volosovskyi adaec85c18 more tests 2016-03-01 14:23:14 +02:00
Roman Volosovskyi 4ba9c1766d nonlvaro fix 2016-03-01 00:01:16 +02:00
Roman Volosovskyi 82ae4b0b2b fix kibit's errors 2016-02-29 23:53:00 +02:00
Roman Volosovskyi 5e4d496d91 fix eastwood errors 2016-02-29 23:01:29 +02:00
Roman Volosovskyi 3fb9978a3b kibit & eastwood 2016-02-29 22:34:11 +02:00
Roman Volosovskyi be335c9b50 truth-value functions tests 2016-02-29 22:11:30 +02:00
Roman Volosovskyi d53838c5fb tests fix 2016-02-28 22:56:12 +02:00
Roman Volosovskyi a084fe32b5 some modules, not final 2016-02-28 22:41:34 +02:00
Roman Volosovskyi 0a6a0b5443 premises swapping 2016-02-28 21:11:13 +02:00
Roman Volosovskyi c2a2c9bd56 truth values 2016-02-27 11:31:10 +02:00
Roman Volosovskyi 2e5d0f618c reserved-operators declaration 2016-02-24 12:23:09 +02:00
Roman Volosovskyi fcb4c55123 matching 2016-02-24 00:02:25 +02:00
Roman Volosovskyi ad295fd197 kill experiments 2016-02-21 16:05:04 +02:00
Roman Volosovskyi f30952226e a lot of things for deriver 2016-02-21 16:01:12 +02:00
Roman Volosovskyi f7a44fa871 rest of rules 2016-02-16 23:34:19 +02:00
Roman Volosovskyi d7f908c1fe reader macros & more rules 2016-02-15 22:33:56 +02:00
Roman Volosovskyi 703a6ea489 more rules 2016-02-14 22:51:01 +02:00
Roman Volosovskyi 3fb237ffec experiments with core.match 2016-02-14 13:12:06 +02:00
Jarrad Hope ab89bd3e94 partial conversion of rules 2016-02-14 11:03:46 +01:00
Roman Volosovskyi 96302b1293 sugar for ext/int sets 2016-02-13 21:54:36 +02:00
Roman Volosovskyi 3b89349637 rules map 2016-02-13 12:30:58 +02:00
Roman Volosovskyi c8788cd683 first attempt of handling of the questions 2016-02-07 18:48:13 +02:00
Roman Volosovskyi 938a6e014d defaults & fix typos 2016-02-07 18:48:13 +02:00
Jarrad Hope e98b2e4d5f i swear i fixed this typo already 2016-02-01 18:08:25 +01:00
Roman Volosovskyi 7cc1dc912a fix tests & fix parser bug with product 2016-01-30 12:41:52 +02:00
Jarrad Hope e47df6bdc9 At Circle CI Badge to README 2016-01-30 10:49:41 +01:00
Roman Volosovskyi ac638ab461 local and forward inference first attempt 2016-01-29 20:44:22 +02:00
Roman Volosovskyi 87e90b0900 Merge branch 'master' of https://github.com/jarradh/narjure into inference_cycle 2016-01-28 17:57:49 +02:00
Jarrad Hope bb268d8101 minor comment 2016-01-27 13:30:36 -05:00
Roman Volosovskyi 9933445026 the first steps of implementation of the control cycle 2016-01-26 19:11:10 +02:00
Roman Volosovskyi 40ff775112 the first steps of implementation of the control cycle 2016-01-25 00:02:42 +02:00
Roman Volosovskyi 86b440335d repl refactoring 2016-01-23 21:37:54 +02:00
Roman Volosovskyi ec304fee41 Merge branch 'bag' 2016-01-23 21:36:24 +02:00
Roman Volosovskyi 5360cf76da repl refactoring 2016-01-21 21:32:01 +02:00
Roman Volosovskyi f4bf98b8ff tests of default bag 2016-01-21 11:42:45 +02:00
Roman Volosovskyi 5da98999e9 very first version of bag 2016-01-21 11:24:20 +02:00
Jarrad Hope 6a3e8795cf typo 2016-01-18 10:21:56 -05:00
Jarrad Hope 01ab080327 all variables now have mandatory name 2016-01-18 10:19:34 -05:00
Roman Volosovskyi 76b0255638 fix parsing of int/ext-set 2016-01-18 10:45:57 +02:00
Jarrad Hope 89bf8e685c remove unused compound-terms 2016-01-17 21:10:37 -05:00
Roman Volosovskyi 0daeca97c3 fix parser for ext/int-image and negation 2016-01-17 22:10:43 +02:00
Roman Volosovskyi 4e967f39bc Merge branch 'cider_repl' 2016-01-17 11:42:57 +02:00
Jarrad Hope aaf8bf3e24 update comment, cleanup of bnf 2016-01-16 18:39:04 -05:00
71 changed files with 5558 additions and 2024 deletions
+2 -1
View File
@@ -13,4 +13,5 @@ pom.xml.asc
*.iml
*~
\#*\#
.\#*
.\#*
/src/nal/experiments.clj
+8
View File
@@ -0,0 +1,8 @@
language: clojure
# Notify #nars
notifications:
slack:
secure: eI5hj2PABtAUkpeYwiAHcFMa5pNvHFuU87uqe4qBtv0upPtu22/dLpw8l6EhhcMZlW6C8D2irrj6W5C1lujCy7u3Vpu1FeBPZbqzapyU1Hpe8YgSRdCY1dbygawZp/Av/HWzpi0aTket83F5/0dvTZK0fQTBRmAfyx17DEJscYEBH61dKVqdwBy5OVYvhQ1+QtEnt1TRJcT0AA8von9lanzx5/mWdQ6O+3BrKURWG6vvQUYEMZH43NU6lNGNV+PBGV1PSqZqbzCh/6C6/d6HauQdewr4Oubez90OquyCDpCZDoVG8eCAsZtZt+tzfJ5gjtz8+HA6GQeQB6QMLAp89m1bPwSZCqdnmQp9S8TNgm8Io3jREBh+5JIdpDXwukKT1kMPFrdDiPTEhHNJojyK3/BERGyrhf8azow+brPq0EIM9Fi/SkGa0gb9mUXY/BZ1MF7ulxxbzpLxHJYlT9QVlV9Q09/uhH4tIh0kPQhe+ntkMT1uLTfDur7CZakB5270ibreRSeK4RKdpy5SjaIN70hSwyrWoZHiED6aKEJJSO1t9Ve1jTDyWbcUZYewtBi+APcqAraja9NIlFApjBRO1jUneS4/BzNqxWZAFS7MOwMrk1xFft47hMIHoNYd7IgyB7TWSxhloNTwMyOWkdOhnPIqqCvds6M0yQCDKNBiVeM=
services:
- redis-server
+2
View File
@@ -1,3 +1,5 @@
[![Circle CI](https://circleci.com/gh/jarradh/narjure/tree/master.svg?style=svg)](https://circleci.com/gh/jarradh/narjure/tree/master)
# Narjure
A Clojure implementation of the [Non-Axiomatic Reasoning System](https://github.com/opennars/opennars) proposed by Pei Wang.
+13 -6
View File
@@ -3,16 +3,23 @@
:url "https://github.com/jarradh/narjure"
:license {:name "GNU General Public License 2.0"
:url "http://www.gnu.org/licenses/old-licenses/gpl-2.0.html"}
:dependencies [[org.clojure/clojure "1.7.0"]
[org.clojure/core.logic "0.8.10"]
:dependencies [[org.clojure/clojure "1.8.0"]
[instaparse "1.4.1"]
[com.rpl/specter "0.9.1"]
[org.clojure/tools.nrepl "0.2.12"]]
[org.clojure/tools.nrepl "0.2.12"]
[org.clojure/data.priority-map "0.0.7"]
[org.clojure/core.match "0.3.0-alpha4"]
[org.clojure/core.unify "0.5.5"]
[org.clojure/core.async "0.2.374"]
[com.taoensso/carmine "2.12.2"]
[mount "0.1.10"]]
:main ^:skip-aot narjure.core
:plugins [[lein-cloverage "1.0.6"]
[jonase/eastwood "0.2.3"]
[lein-kibit "0.1.2"]
[cider/cider-nrepl "0.11.0-SNAPSHOT"]]
:eastwood {:exclude-namespaces [nal.rules]}
:target-path "target/%s"
:repl-options {:init-ns narjure.repl
:nrepl-middleware
[narjure.repl/narsese-handler]}
:repl-options {:init-ns narjure.repl
:nrepl-middleware [narjure.repl/narsese-handler]}
:profiles {:uberjar {:aot :all}})
+528
View File
@@ -0,0 +1,528 @@
; Pei Wang's "Non-Axiomatic Logic" specified with a math. notation inspired DSL with given intiutive explainations:
; The rules of NAL can be interpreted by considering the intiution behind the following two relations:
; Statement: (A --> B): A can stand for B
; Statement about Statement: (A ==> B): If A is true so is/will be B
; --> is a relation in meaning of terms while ==> is a relation of truth between statements.
; Revision
; When a given belief is challenged by new experience a new belief2 with same content (and disjoint evidental base)
; a new revised task which sums up the evidence of both belief and belief2 is derived:
; A A |- A [:t/revision] (Commented out because it is already handled by belief management in java)
; Similarity to Inheritance
(S --> P) (S <-> P) |- (S --> P) :pre [:question?] :post [:t/structural-intersection :p/judgment]
; Inheritance to Similarity
(S <-> P) (S --> P) |- (S <-> P) :pre [:question?] :post [:t/structural-abduction :p/judgment]
; Set Definition Similarity to Inheritance
(S <-> {P}) S |- (S --> {P}) :post [:t/identity :d/identity :allow-backward]
(S <-> {P}) {P} |- (S --> {P}) :post [:t/identity :d/identity :allow-backward]
([S] <-> P) [S] |- ([S] --> P) :post [:t/identity :d/identity :allow-backward]
([S] <-> P) P |- ([S] --> P) :post [:t/identity :d/identity :allow-backward]
({S} <-> {P}) {S} |- ({P} --> {S}) :post [:t/identity :d/identity :allow-backward]
({S} <-> {P}) {P} |- ({P} --> {S}) :post [:t/identity :d/identity :allow-backward]
([S] <-> [P]) [S] |- ([P] --> [S]) :post [:t/identity :d/identity :allow-backward]
([S] <-> [P]) [P] |- ([P] --> [S]) :post [:t/identity :d/identity :allow-backward]
; Set Definition Unwrap
({S} <-> {P}) {S} |- (S <-> P) :post [:t/identity :d/identity :allow-backward]
({S} <-> {P}) {P} |- (S <-> P) :post [:t/identity :d/identity :allow-backward]
([S] <-> [P]) [S] |- (S <-> P) :post [:t/identity :d/identity :allow-backward]
([S] <-> [P]) [P] |- (S <-> P) :post [:t/identity :d/identity :allow-backward]
; Nothing is more specific than a instance so it's similar
(S --> {P}) S |- (S <-> {P}) :post [:t/identity :d/identity :allow-backward]
(S --> {P}) {P} |- (S <-> {P}) :post [:t/identity :d/identity :allow-backward]
; nothing is more general than a property so it's similar
([S] --> P) [S] |- ([S] <-> P) :post [:t/identity :d/identity :allow-backward]
([S] --> P) P |- ([S] <-> P) :post [:t/identity :d/identity :allow-backward]
; Truth-value functions: see TruthFunctions.java
; Immediate Inference
; If S can stand for P P can to a certain low degree also represent the class S
; If after S usually P happens then it might be a good guess that usually before P happens S happens.
(P --> S) (S --> P) |- (P --> S) :pre [:question?] :post [:t/conversion :p/judgment]
(P --> S) (S --> P) |- (P --> S) :pre [:question?] :post [:t/conversion :p/judgment]
(P ==> S) (S ==> P) |- (P ==> S) :pre [:question?] :post [:t/conversion :p/judgment]
(P =|> S) (S =|> P) |- (P =|> S) :pre [:question?] :post [:t/conversion :p/judgment]
(P =\> S) (S =/> P) |- (P =\> S) :pre [:question?] :post [:t/conversion :p/judgment]
(P =/> S) (S =\> P) |- (P =/> S) :pre [:question?] :post [:t/conversion :p/judgment]
; "If not smoking lets you be healthy being not healthy may be the result of smoking"
( --S ==> P) P |- ( --P ==> S) :post [:t/contraposition :allow-backward]
( --S ==> P) --S |- ( --P ==> S) :post [:t/contraposition :allow-backward]
( --S =|> P) P |- ( --P =|> S) :post [:t/contraposition :allow-backward]
( --S =|> P) --S |- ( --P =|> S) :post [:t/contraposition :allow-backward]
( --S =/> P) P |- ( --P =\> S) :post [:t/contraposition :allow-backward]
( --S =/> P) --S |- ( --P =\> S) :post [:t/contraposition :allow-backward]
( --S =\> P) P |- ( --P =/> S) :post [:t/contraposition :allow-backward]
( --S =\> P) --S |- ( --P =/> S) :post [:t/contraposition :allow-backward]
; A belief b <f c> is equal to --b <1-f c> which is the negation rule:
(A --> B) A |- --(A --> B) :post [:t/negation :d/negation :allow-backward]
(A --> B) B |- --(A --> B) :post [:t/negation :d/negation :allow-backward]
--(A --> B) A |- (A --> B) :post [:t/negation :d/negation :allow-backward]
--(A --> B) B |- (A --> B) :post [:t/negation :d/negation :allow-backward]
(A <-> B) A |- --(A <-> B) :post [:t/negation :d/negation :allow-backward]
(A <-> B) B |- --(A <-> B) :post [:t/negation :d/negation :allow-backward]
--(A <-> B) A |- (A <-> B) :post [:t/negation :d/negation :allow-backward]
--(A <-> B) B |- (A <-> B) :post [:t/negation :d/negation :allow-backward]
(A ==> B) A |- --(A ==> B) :post [:t/negation :d/negation :allow-backward :order-for-all-same]
(A ==> B) B |- --(A ==> B) :post [:t/negation :d/negation :allow-backward :order-for-all-same]
--(A ==> B) A |- (A ==> B) :post [:t/negation :d/negation :allow-backward :order-for-all-same]
--(A ==> B) B |- (A ==> B) :post [:t/negation :d/negation :allow-backward :order-for-all-same]
(A <=> B) A |- --(A <=> B) :post [:t/negation :d/negation :allow-backward :order-for-all-same]
(A <=> B) B |- --(A <=> B) :post [:t/negation :d/negation :allow-backward :order-for-all-same]
--(A <=> B) A |- (A <=> B) :post [:t/negation :d/negation :allow-backward :order-for-all-same]
--(A <=> B) B |- (A <=> B) :post [:t/negation :d/negation :allow-backward :order-for-all-same]
; TODO: probably make simpler by just allowing it for all tasks in general
; inheritance-based syllogism
; (A --> B) ------- (B --> C)
; \ /
; \ /
; \ /
; \ /
; (A --> C)
; If A is a special case of B and B is a special case of C so is A a special case of C (strong) the other variations are hypotheses (weak)
(A --> B) (B --> C) |- (A --> C) :pre [#(not= A C)] :post [:t/deduction :d/strong :allow-backward]
(A --> B) (A --> C) |- (C --> B) :pre [#(not= B C)] :post [:t/abduction :d/weak :allow-backward]
(A --> C) (B --> C) |- (B --> A) :pre [#(not= A B)] :post [:t/induction :d/weak :allow-backward]
(A --> B) (B --> C) |- (C --> A) :pre [#(not= C A)] :post [:t/exemplification :d/weak :allow-backward]
; similarity from inheritance
; If S is a special case of P and P is a special case of S then S and P are similar
(S --> P) (P --> S) |- (S <-> P) :post [:t/intersection :d/strong :allow-backward]
; inheritance from similarty <- TODO check why this one was missing
(S <-> P) (P --> S) |- (S --> P) :post [:t/reduce-conjunction :d/strong :allow-backward]
; similarity-based syllogism
; If P and S are a special case of M then they might be similar (weak)
; also if P and S are a general case of M
(P --> M) (S --> M) |- (S <-> P) :pre [#(not= S P)] :post [:t/comparison :d/weak :allow-backward]
(M --> P) (M --> S) |- (S <-> P) :pre [#(not= S P)] :post [:t/comparison :d/weak :allow-backward]
; If M is a special case of P and S and M are similar then S is also a special case of P (strong)
(M --> P) (S <-> M) |- (S --> P) :pre [#(not= S P)] :post [:t/analogy :d/strong :allow-backward]
(P --> M) (S <-> M) |- (P --> S) :pre [#(not= S P)] :post [:t/analogy :d/strong :allow-backward]
(M <-> P) (S <-> M) |- (S <-> P) :pre [#(not= S P)] :post [:t/resemblance :d/strong :allow-backward]
; inheritance-based composition
; If P and S are in the intension/extension of M then union/difference and intersection can be built:
(P --> M) (S --> M) |- ((S | P) --> M) :pre [(not_set S) (not_set P) #(not= S P) (no_common_subterm S P)] :post [:t/intersection]
((S & P) --> M) :post [:t/union]
((P -i S) --> M) :post [:t/difference]
(M --> P) (M --> S) |- (M --> (P & S)) :pre [(not_set S) (not_set P) #(not= S P) (no_common_subterm S P)] :post [:t/intersection]
(M --> (P | S)) :post [:t/union]
(M --> (P -e S)) :post [:t/difference]
; inheritance-based decomposition
; if (S --> M) is the case and ((| S :list/A) --> M) is not the case then ((| :list/A) --> M) is not the case hence :t/decompose-positive-negative-negative
(S --> M) ((| S :list/A) --> M) |- ((| :list/A) --> M) :post [:t/decompose-positive-negative-negative]
(S --> M) ((& S :list/A) --> M) |- ((& :list/A) --> M) :post [:t/decompose-negative-positive-positive]
(S --> M) ((S -i P) --> M) |- (P --> M) :post [:t/decompose-positive-negative-positive]
(S --> M) ((P -i S) --> M) |- (P --> M) :post [:t/decompose-negative-negative-negative]
(M --> S) (M --> (& S :list/A)) |- (M --> (& :list/A)) :post [:t/decompose-positive-negative-negative]
(M --> S) (M --> (| S :list/A)) |- (M --> (| :list/A)) :post [:t/decompose-negative-positive-positive]
(M --> S) (M --> (S -e P)) |- (M --> P) :post [:t/decompose-positive-negative-positive]
(M --> S) (M --> (P -e S)) |- (M --> P) :post [:t/decompose-negative-negative-negative]
; Set comprehension:
(C --> A) (C --> B) |- (C --> R) :pre [(set-ext? A) (union A B R)] :post [:t/union]
(C --> A) (C --> B) |- (C --> R) :pre [(set-int? A) (union A B R)] :post [:t/intersection]
(A --> C) (B --> C) |- (R --> C) :pre [(set-ext? A) (union A B R)] :post [:t/intersection]
(A --> C) (B --> C) |- (R --> C) :pre [(set-int? A) (union A B R)] :post [:t/union]
(C --> A) (C --> B) |- (C --> R) :pre [(set-ext? A) (intersection A B R)] :post [:t/intersection]
(C --> A) (C --> B) |- (C --> R) :pre [(set-int? A) (intersection A B R)] :post [:t/union]
(A --> C) (B --> C) |- (R --> C) :pre [(set-ext? A) (intersection A B R)] :post [:t/union]
(A --> C) (B --> C) |- (R --> C) :pre [(set-int? A) (intersection A B R)] :post [:t/intersection]
(C --> A) (C --> B) |- (C --> R) :pre [(difference A B R)] :post [:t/difference]
(A --> C) (B --> C) |- (R --> C) :pre [(difference A B R)] :post [:t/difference]
; Set element takeout:
(C --> {:list/A}) C |- (C --> {:from/A}) :post [:t/structural-deduction]
(C --> [:list/A]) C |- (C --> [:from/A]) :post [:t/structural-deduction]
({:list/A} --> C) C |- ({:from/A} --> C) :post [:t/structural-deduction]
([:list/A] --> C) C |- ([:from/A] --> C) :post [:t/structural-deduction]
; NAL3 single premise inference:
((| :list/A) --> M) M |- (:from/A --> M) :post [:t/structural-deduction]
(M --> (& :list/A)) M |- (M --> :from/A) :post [:t/structural-deduction]
((B -i G) --> S) S |- (B --> S) :post [:t/structural-deduction]
(R --> (B -e S)) R |- (R --> B) :post [:t/structural-deduction]
; NAL4 - Transformations between products and images:
; Relations and transforming them into different representations so that arguments and the relation it'self can become the subject or predicate
((:list/A) --> M) A_i |- (A_i --> (/ M A_1..A_i.(substitute _)..A_n )) :post [:t/identity :d/identity]
(M --> (:list/A)) A_i |- ((\ M A_1..A_i.(substitute _)..A_n ) --> A_i) :post [:t/identity :d/identity]
(A_i --> (/ M A_1..A_i.(substitute _)..A_n )) M |- ((:list/A) --> M) :post [:t/identity :d/identity]
((\ M A_1..A_i.(substitute _)..A_n ) --> A_i) M |- (M --> (:list/A)) :post [:t/identity :d/identity]
; implication-based syllogism
; (A ==> B) ------- (B ==> C)
; \ /
; \ /
; \ /
; \ /
; (A ==> C)
; If after S M happens and after M P happens so P happens after S
(M ==> P) (S ==> M) |- (S ==> P) :pre [#(not= S P)] :post [:t/deduction :order-for-all-same :allow-backward]
(P ==> M) (S ==> M) |- (S ==> P) :pre [#(not= S P)] :post [:t/induction :allow-backward]
(P =|> M) (S =|> M) |- (S =|> P) :pre [#(not= S P)] :post [:t/induction :allow-backward]
(P =/> M) (S =/> M) |- (S =|> P) :pre [#(not= S P)] :post [:t/induction :allow-backward]
(P =\> M) (S =\> M) |- (S =|> P) :pre [#(not= S P)] :post [:t/induction :allow-backward]
(M ==> P) (M ==> S) |- (S ==> P) :pre [#(not= S P)] :post [:t/abduction :allow-backward]
(M =/> P) (M =/> S) |- (S =|> P) :pre [#(not= S P)] :post [:t/abduction :allow-backward]
(M =|> P) (M =|> S) |- (S =|> P) :pre [#(not= S P)] :post [:t/abduction :allow-backward]
(M =\> P) (M =\> S) |- (S =|> P) :pre [#(not= S P)] :post [:t/abduction :allow-backward]
(P ==> M) (M ==> S) |- (S ==> P) :pre [#(not= S P)] :post [:t/exemplification :allow-backward]
(P =/> M) (M =/> S) |- (S =\> P) :pre [#(not= S P)] :post [:t/exemplification :allow-backward]
(P =\> M) (M =\> S) |- (S =/> P) :pre [#(not= S P)] :post [:t/exemplification :allow-backward]
(P =|> M) (M =|> S) |- (S =|> P) :pre [#(not= S P)] :post [:t/exemplification :allow-backward]
; implication to equivalence
; If when S happens P happens and before P happens S has happened then they are truth-related equivalent
(S ==> P) (P ==> S) |- (S <=> P) :pre [#(not= S P)] :post [:t/intersection :allow-backward]
(S =|> P) (P =|> S) |- (S <|> P) :pre [#(not= S P)] :post [:t/intersection :allow-backward]
(S =/> P) (P =\> S) |- (S </> P) :pre [#(not= S P)] :post [:t/intersection :allow-backward]
(S =\> P) (P =/> S) |- (P </> S) :pre [#(not= S P)] :post [:t/intersection :allow-backward]
; equivalence-based syllogism
; Same as for inheritance again
(P ==> M) (S ==> M) |- (S <=> P) :pre [#(not= S P)] :post [:t/comparison :allow-backward]
(P =/> M) (S =/> M) |- (S <|> P) :pre [#(not= S P)] :post [:t/comparison :allow-backward]
(S </> P) :post [:t/comparison :allow-backward]
(P </> S) :post [:t/comparison :allow-backward]
(P =|> M) (S =|> M) |- (S <|> P) :pre [#(not= S P)] :post [:t/comparison :allow-backward]
(P =\> M) (S =\> M) |- (S <|> P) :pre [#(not= S P)] :post [:t/comparison :allow-backward]
(S </> P) :post [:t/comparison :allow-backward]
(P </> S) :post [:t/comparison :allow-backward]
(M ==> P) (M ==> S) |- (S <=> P) :pre [#(not= S P)] :post [:t/comparison :allow-backward]
(M =/> P) (M =/> S) |- (S <|> P) :pre [#(not= S P)] :post [:t/comparison :allow-backward]
(S </> P) :post [:t/comparison :allow-backward]
(P </> S) :post [:t/comparison :allow-backward]
(M =|> P) (M =|> S) |- (S <|> P) :pre [#(not= S P)] :post [:t/comparison :allow-backward]
; Same as for inheritance again
(M ==> P) (S <=> M) |- (S ==> P) :pre [#(not= S P)] :post [:t/analogy :allow-backward]
(M =/> P) (S </> M) |- (S =/> P) :pre [#(not= S P)] :post [:t/analogy :allow-backward]
(M =/> P) (S <|> M) |- (S =/> P) :pre [#(not= S P)] :post [:t/analogy :allow-backward]
(M =|> P) (S <|> M) |- (S =|> P) :pre [#(not= S P)] :post [:t/analogy :allow-backward]
(M =\> P) (M </> S) |- (S =\> P) :pre [#(not= S P)] :post [:t/analogy :allow-backward]
(M =\> P) (S <|> M) |- (S =\> P) :pre [#(not= S P)] :post [:t/analogy :allow-backward]
(P ==> M) (S <=> M) |- (P ==> S) :pre [#(not= S P)] :post [:t/analogy :allow-backward]
(P =/> M) (S <|> M) |- (P =/> S) :pre [#(not= S P)] :post [:t/analogy :allow-backward]
(P =|> M) (S <|> M) |- (P =|> S) :pre [#(not= S P)] :post [:t/analogy :allow-backward]
(P =\> M) (S </> M) |- (P =\> S) :pre [#(not= S P)] :post [:t/analogy :allow-backward]
(P =\> M) (S <|> M) |- (P =\> S) :pre [#(not= S P)] :post [:t/analogy :allow-backward]
(M <=> P) (S <=> M) |- (S <=> P) :pre [#(not= S P)] :post [:t/resemblance :order-for-all-same :allow-backward]
(M </> P) (S <|> M) |- (S </> P) :pre [#(not= S P)] :post [:t/resemblance :allow-backward]
(M <|> P) (S </> M) |- (S </> P) :pre [#(not= S P)] :post [:t/resemblance :allow-backward]
; implication-based composition
; Same as for inheritance again
(P ==> M) (S ==> M) |- ((P || S) ==> M) :pre [#(not= S P)] :post [:t/intersection]
((P && S) ==> M) :post [:t/union]
(P =|> M) (S =|> M) |- ((P || S) =|> M) :pre [#(not= S P)] :post [:t/intersection]
((P &| S) =|> M) :post [:t/union]
(P =/> M) (S =/> M) |- ((P || S) =/> M) :pre [#(not= S P)] :post [:t/intersection]
((P &| S) =/> M) :post [:t/union]
(P =\> M) (S =\> M) |- ((P || S) =\> M) :pre [#(not= S P)] :post [:t/intersection]
((P &| S) =\> M) :post [:t/union]
(M ==> P) (M ==> S) |- (M ==> (P && S)) :pre [#(not= S P)] :post [:t/intersection]
(M ==> (P || S)) :post [:t/union]
(M =/> P) (M =/> S) |- (M =/> (P &| S)) :pre [#(not= S P)] :post [:t/intersection]
(M =/> (P || S)) :post [:t/union]
(M =|> P) (M =|> S) |- (M =|> (P &| S)) :pre [#(not= S P)] :post [:t/intersection]
(M =|> (P || S)) :post [:t/union]
(M =\> P) (M =\> S) |- (M =\> (P &| S)) :pre [#(not= S P)] :post [:t/intersection]
(M =\> (P || S)) :post [:t/union]
(D =/> R) (D =\> K) |- (K =/> R) :pre [#(not= R K)] :post [:t/abduction]
(R =\> K) :post [:t/induction]
(K </> R) :post [:t/comparison]
; implication-based decomposition
; Same as for inheritance again
(S ==> M) ((|| S :list/A) ==> M) |- ((|| :list/A) ==> M) :post [:t/decompose-positive-negative-negative :order-for-all-same]
(S ==> M) ((&& S :list/A) ==> M) |- ((&& :list/A) ==> M) :post [:t/decompose-negative-positive-positive :order-for-all-same :seq-interval-from-premises]
(M ==> S) (M ==> (&& S :list/A)) |- (M ==> (&& :list/A)) :post [:t/decompose-positive-negative-negative :order-for-all-same :seq-interval-from-premises]
(M ==> S) (M ==> (|| S :list/A)) |- (M ==> (|| :list/A)) :post [:t/decompose-negative-positive-positive :order-for-all-same]
; conditional syllogism
; If after M P usually happens and M happens it means P is expected to happen
M (M ==> P) |- P :post [:t/deduction :d/induction :order-for-all-same] :pre [(shift-occurrence-forward unused "==>")]
M (P ==> M) |- P :post [:t/abduction :d/deduction :order-for-all-same] :pre [(shift-occurrence-backward unused "==>")]
M (S <=> M) |- S :post [:t/analogy :d/strong :order-for-all-same] :pre [(shift-occurrence-backward unused "<=>")]
M (M <=> S) |- S :post [:t/analogy :d/strong :order-for-all-same] :pre [(shift-occurrence-forward unused "==>")]
; conditional composition:
; They are let out for AGI purpose don't let the system generate conjunctions or useless <=> and ==> statements
; For this there needs to be a semantic dependence between both either by the predicate or by the subject
; or a temporal dependence which acts as special case of semantic dependence
; These cases are handled by "Variable Introduction" and "Temporal Induction"
; P S |- (S ==> P) :pre [(no_common_subterm S P)] :post [:t/induction]
; P S |- (S <=> P) :pre [(no_common_subterm S P)] :post [:t/comparison]
; P S |- (P && S) :pre [(no_common_subterm S P)] :post [:t/intersection]
; P S |- (P || S) :pre [(no_common_subterm S P)] :post [:t/union]
; conjunction decompose
(&& :list/A) A_1 |- A_1 :post [:t/structural-deduction :d/structural-strong]
(&/ :list/A) A_1 |- A_1 :post [:t/structural-deduction :d/structural-strong]
(&| :list/A) A_1 |- A_1 :post [:t/structural-deduction :d/structural-strong]
(&/ B :list/A) B |- (&/ :list/A) :pre [(task "!")] :post [:t/deduction :d/strong :seq-interval-from-premises]
; propositional decomposition
; If S is the case and (&& S :list/A) is not the case it can't be that (&& :list/A) is the case
S (&/ S :list/A) |- (&/ :list/A) :post [:t/decompose-positive-negative-negative :seq-interval-from-premises]
S (&| S :list/A) |- (&| :list/A) :post [:t/decompose-positive-negative-negative]
S (&& S :list/A) |- (&& :list/A) :post [:t/decompose-positive-negative-negative]
S (|| S :list/A) |- (|| :list/A) :post [:t/decompose-negative-positive-positive]
; Additional for negation: https://groups.google.com/forum/#!topic/open-nars/g-7r0jjq2Vc
S (&/ (-- S) :list/A) |- (&/ :list/A) :post [:t/decompose-negative-negative-negative :seq-interval-from-premises]
S (&| (-- S) :list/A) |- (&| :list/A) :post [:t/decompose-negative-negative-negative]
S (&& (-- S) :list/A) |- (&& :list/A) :post [:t/decompose-negative-negative-negative]
S (|| (-- S) :list/A) |- (|| :list/A) :post [:t/decompose-positive-positive-positive]
; multi-conditional syllogism
; Inference about the pre/postconditions
Y ((&& X :list/A) ==> B) |- ((&& :list/A) ==> B) :pre [(substitute-if-unifies "$" X Y)] :post [:t/deduction :order-for-all-same :seq-interval-from-premises]
((&& M :list/A) ==> C) ((&& :list/A) ==> C) |- M :post [:t/abduction :order-for-all-same]
; Can be derived by NAL7 rules so this won't be necessary there (:order-for-all-same left out here)
; the first rule does not have :order-for-all-same because it would be invalid see: https://groups.google.com/forum/#!topic/open-nars/r5UJo64Qhrk
((&& :list/A) ==> C) M |- ((&& M :list/A) ==> C) :pre [(not-implication-or-equivalence M)] :post [:t/induction]
((&& :list/A) =|> C) M |- ((&& M :list/A) =|> C) :pre [(not-implication-or-equivalence M)] :post [:t/induction]
((&& :list/A) =/> C) M |- ((&& M :list/A) =/> C) :pre [(not-implication-or-equivalence M)] :post [:t/induction]
((&& :list/A) =\> C) M |- ((&& M :list/A) =\> C) :pre [(not-implication-or-equivalence M)] :post [:t/induction]
(A ==> M) ((&& M :list/A) ==> C) |- ((&& A :list/A) ==> C) :post [:t/deduction :order-for-all-same :seq-interval-from-premises]
((&& M :list/A) ==> C) ((&& A :list/A) ==> C) |- (A ==> M) :post [:t/induction :order-for-all-same]
(A ==> M) ((&& A :list/A) ==> C) |- ((&& M :list/A) ==> C) :post [:t/abduction :order-for-all-same :seq-interval-from-premises]
; variable introduction
; Introduce variables by common subject or predicate
(S --> M) (P --> M) |- ((P --> $X) ==> (S --> $X)) :pre [#(not= S P)] :post [:t/abduction]
((S --> $X) ==> (P --> $X)) :post [:t/induction]
((P --> $X) <=> (S --> $X)) :post [:t/comparison]
(&& (S --> #Y) (P --> #Y)) :post [:t/intersection]
(S --> M) (P --> M) |- ((&/ (P --> $X) I) =/> (S --> $X)) :pre [#(not= S P) (measure-time I)] :post [:t/induction :linkage-temporal]
((S --> $X) =\> (&/ (P --> $X) I)) :post [:t/abduction :linkage-temporal]
((&/ (P --> $X) I) </> (S --> $X)) :post [:t/comparison :linkage-temporal]
(&/ (P --> #Y) I (S --> #Y)) :post [:t/intersection :linkage-temporal]
(S --> M) (P --> M) |- ((P --> $X) =|> (S --> $X)) :pre [#(not= S P) (concurrent Task Belief)] :post [:t/abduction :linkage-temporal]
((S --> $X) =|> (P --> $X)) :post [:t/induction :linkage-temporal]
((P --> $X) <|> (S --> $X)) :post [:t/comparison :linkage-temporal]
(&| (P --> #Y) (S --> #Y)) :post [:t/intersection :linkage-temporal]
(M --> S) (M --> P) |- (($X --> S) ==> ($X --> P)) :pre [#(not= S P)] :post [:t/induction]
(($X --> P) ==> ($X --> S)) :post [:t/abduction]
(($X --> S) <=> ($X --> P)) :post [:t/comparison]
(&& (#Y --> S) (#Y --> P)) :post [:t/intersection]
(M --> S) (M --> P) |- ((&/ ($X --> P) I) =/> ($X --> S)) :pre [#(not= S P) (measure-time I)] :post [:t/induction :linkage-temporal]
(($X --> S) =\> (&/ ($X --> P) I)) :post [:t/abduction :linkage-temporal]
((&/ ($X --> P) I) </> ($X --> S)) :post [:t/comparison :linkage-temporal]
(&/ (#Y --> P) I (#Y --> S)) :post [:t/intersection :linkage-temporal]
(M --> S) (M --> P) |- (($X --> S) =|> ($X --> P)) :pre [#(not= S P) (concurrent (M --> P) (M --> S))] :post [:t/induction :linkage-temporal]
(($X --> P) =|> ($X --> S)) :post [:t/abduction :linkage-temporal]
(($X --> S) <|> ($X --> P)) :post [:t/comparison :linkage-temporal]
(&| (#Y --> S) (#Y --> P)) :post [:t/intersection :linkage-temporal]
; 2nd variable introduction
(A ==> (M --> P)) (M --> S) |- ((&& A ($X --> S)) ==> ($X --> P)) :pre [#(not= A (M --> S))] :post [:t/induction]
(&& (A ==> (#Y --> P)) (#Y --> S)) :post [:t/intersection]
(&& (M --> P) :list/A) (M --> S) |- (($Y --> S) ==> (&& ($Y --> P) :list/A)) :pre [#(not= S P)] :post [:t/induction]
(&& (#Y --> S) (#Y --> P) :list/A) :post [:t/intersection]
(A ==> (P --> M)) (S --> M) |- ((&& A (P --> $X)) ==> (S --> $X)) :pre [#(not= S P) #(not= A (S --> M))] :post [:t/abduction]
(&& (A ==> (P --> #Y)) (S --> #Y)) :post [:t/intersection]
(&& (P --> M) :list/A) (S --> M) |- ((S --> $Y) ==> (&& (P --> $Y) :list/A)) :pre [#(not= S P)] :post [:t/abduction]
(&& (S --> #Y) (P --> #Y) :list/A) :post [:t/intersection]
(A --> L) ((A --> S) ==> R) |- ((&& (#X --> L) (#X --> S)) ==> R) :post [:t/induction]
(A --> L) ((&& (A --> S) :list/A) ==> R) |- ((&& (#X --> L) (#X --> S) :list/A) ==> R) :pre [(substitute A #X)] :post [:t/induction]
; dependent variable elimination
; Decomposition with elimination of a variable
B (&& A :list/A) |- (&& :list/A) :pre [(task ".") (substitute-if-unifies "#" A B)] :post [:t/anonymous-analogy :d/strong :order-for-all-same :seq-interval-from-premises]
; conditional abduction by dependent variable
((A --> R) ==> Z) ((&& (#Y --> B) (#Y --> R) :list/A) ==> Z) |- (A --> B) :post [:t/abduction]
((A --> R) ==> Z) ((&& (#Y --> B) (#Y --> R)) ==> Z) |- (A --> B) :post [:t/abduction]
; conditional deduction "An inverse inference has been implemented as a form of deduction" https://code.google.com/p/open-nars/issues/detail?id=40&can=1
(U --> L) ((&& (#X --> L) (#X --> R)) ==> Z) |- ((U --> R) ==> Z) :post [:t/deduction]
(U --> L) ((&& (#X --> L) (#X --> R) :list/A) ==> Z) |- ((&& (U --> R) :list/A) ==> Z) :pre [(substitute #X U)] :post [:t/deduction]
; independent variable elimination
B (A ==> C) |- C [:t/deduction :order-for-all-same] :pre [(substitute-if-unifies "$" A B) (shift-occurrence-forward unused "==>")]
B (C ==> A) |- C [:t/abduction :order-for-all-same] :pre [(substitute-if-unifies "$" A B) (shift-occurrence-backward unused "==>")]
B (A <=> C) |- C [:t/deduction :order-for-all-same] :pre [(substitute-if-unifies "$" A B) (shift-occurrence-backward unused "<=>")]
B (C <=> A) |- C [:t/deduction :order-for-all-same] :pre [(substitute-if-unifies "$" A B) (shift-occurrence-forward unused "<=>")]
; second level variable handling rules
; second level variable elimination (termlink level2 growth needed in order for these rules to work)
(A --> K) (&& (#X --> L) (($Y --> K) ==> (&& :list/A))) |- (&& (#X --> L) :list/A) :pre [(substitute $Y A)] :post [:t/deduction]
(A --> K) (($X --> L) ==> (&& (#Y --> K) :list/A)) |- (($X --> L) ==> (&& :list/A)) :pre [(substitute #Y A)] :post [:t/anonymous-analogy]
; precondition combiner inference rule (variable_unification6):
((&& C :list/A) ==> Z) ((&& C :list/B) ==> Z) |- ((&& :list/A) ==> (&& :list/B)) :post [:t/induction]
((&& C :list/A) ==> Z) ((&& C :list/B) ==> Z) |- ((&& :list/B) ==> (&& :list/A)) :post [:t/induction]
(Z ==> (&& C :list/A)) (Z ==> (&& C :list/B)) |- ((&& :list/A) ==> (&& :list/B)) :post [:t/abduction]
(Z ==> (&& C :list/A)) (Z ==> (&& C :list/B)) |- ((&& :list/B) ==> (&& :list/A)) :post [:t/abduction]
; NAL7 specific inference
; Reasoning about temporal statements. those are using the ==> relation because relation in time is a relation of the truth between statements.
X (XI ==> B) |- B [:t/deduction :d/induction :order-for-all-same] :pre [(substitute-if-unifies "$" XI (&/ X /0)) (shift-occurrence-forward XI "==>")]
X (BI ==> Y) |- BI [:t/abduction :d/deduction :order-for-all-same] :pre [(substitute-if-unifies "$" Y X) (shift-occurrence-backward BI "==>")]
; Temporal induction:
; When P and then S happened according to an observation by induction (weak) it may be that alyways after P usually S happens.
P S |- ((&/ S I) =/> P) :pre [(measure-time I) (not-implication-or-equivalence P) (not-implication-or-equivalence S)] :post [:t/induction :linkage-temporal]
(P =\> (&/ S I)) :post [:t/abduction :linkage-temporal]
((&/ S I) </> P) :post [:t/comparison :linkage-temporal]
(&/ S I P) :post [:t/intersection :linkage-temporal]
P S |- (S =|> P) :pre [(concurrent Task Belief) (not-implication-or-equivalence P) (not-implication-or-equivalence S)] :post [:t/induction :linkage-temporal]
(P =|> S) :post [:t/induction :linkage-temporal]
(S <|> P) :post [:t/comparison :linkage-temporal]
(&| S P) :post [:t/intersection :linkage-temporal]
; backward inference is mostly handled by the rule transformation:
; T B |- C [post] =>
; C B |- T [post] :pre [:question?]
; C T |- B [post] :pre [:question?]
; here now are the backward inference rules which should really only work on backward inference:
(A --> S) (B --> S) |- (A --> B) :pre [:question?] :post [:p/question]
(B --> A) :post [:p/question]
(A <-> B) :post [:p/question]
; and the backward inference driven forward inference:
; NAL2:
([A] <-> [B]) (A <-> B) |- ([A] <-> [B]) :pre [:question?] :post [:t/belief-identity :p/judgment]
({A} <-> {B}) (A <-> B) |- ({A} <-> {B}) :pre [:question?] :post [:t/belief-identity :p/judgment]
([A] --> [B]) (A <-> B) |- ([A] --> [B]) :pre [:question?] :post [:t/belief-identity :p/judgment]
({A} --> {B}) (A <-> B) |- ({A} --> {B}) :pre [:question?] :post [:t/belief-identity :p/judgment]
; NAL3:
; composition on both sides of a statement:
((& B :list/A) --> (& A :list/A)) (B --> A) |- ((& B :list/A) --> (& A :list/A)) :pre [:question?] :post [:t/belief-structural-deduction :p/judgment]
((| B :list/A) --> (| A :list/A)) (B --> A) |- ((| B :list/A) --> (| A :list/A)) :pre [:question?] :post [:t/belief-structural-deduction :p/judgment]
((- S A) --> (- S B)) (B --> A) |- ((- S A) --> (- S B)) :pre [:question?] :post [:t/belief-structural-deduction :p/judgment]
((~ S A) --> (~ S B)) (B --> A) |- ((~ S A) --> (~ S B)) :pre [:question?] :post [:t/belief-structural-deduction :p/judgment]
; composition on one side of a statement:
(W --> (| B :list/A)) (W --> B) |- (W --> (| B :list/A)) :pre [:question?] :post [:t/belief-structural-deduction :p/judgment]
((& B :list/A) --> W) (B --> W) |- ((& B :list/A) --> W) :pre [:question?] :post [:t/belief-structural-deduction :p/judgment]
(W --> (- S B)) (W --> B) |- (W --> (- S B)) :pre [:question?] :post [:t/beliefStructuralDifference :p/judgment]
((~ S B) --> W) (B --> W) |- ((~ S B) --> W) :pre [:question?] :post [:t/beliefStructuralDifference :p/judgment]
; NAL4:
; composition on both sides of a statement:
((B P) --> Z) (B --> A) |- ((B P) --> (A P)) :pre [:question?] :post [:t/belief-structural-deduction :p/judgment]
((P B) --> Z) (B --> A) |- ((P B) --> (P A)) :pre [:question?] :post [:t/belief-structural-deduction :p/judgment]
((B P) <-> Z) (B <-> A) |- ((B P) <-> (A P)) :pre [:question?] :post [:t/belief-structural-deduction :p/judgment]
((P B) <-> Z) (B <-> A) |- ((P B) <-> (P A)) :pre [:question?] :post [:t/belief-structural-deduction :p/judgment]
((\ N A _) --> Z) (N --> R) |- ((\ N A _) --> (\ R A _)) :pre [:question?] :post [:t/belief-structural-deduction :p/judgment]
((/ N _ B) --> Z) (S --> B) |- ((/ N _ B) --> (/ N _ S)) :pre [:question?] :post [:t/belief-structural-deduction :p/judgment]
; NAL5:
--A A |- --A :pre [:question?] :post [:t/belief-negation :p/judgment]
A --A |- A :pre [:question?] :post [:t/belief-negation :p/judgment]
; compound composition one premise
(|| B :list/A) B |- (|| B :list/A) :pre [:question?] :post [:t/belief-structural-deduction :p/judgment]
+526
View File
@@ -0,0 +1,526 @@
// Pei Wang's "Non-Axiomatic Logic" specified with a math. notation inspired DSL with given intiutive explainations:
//The rules of NAL, can be interpreted by considering the intiution behind the following two relations:
// Statement: (A --> B): A can stand for B
// Statement about Statement: (A ==> B): If A is true, so is/will be B
// --> is a relation in meaning of terms, while ==> is a relation of truth between statements.
//// Revision ////////////////////////////////////////////////////////////////////////////////////
// When a given belief is challenged by new experience, a new belief2 with same content (and disjoint evidental base),
// a new revised task, which sums up the evidence of both belief and belief2 is derived:
// A, A |- A, (Truth:Revision) (Commented out because it is already handled by belief management in java)
//Similarity to Inheritance
(S --> P), (S <-> P), task("?") |- (S --> P), (Truth:StructuralIntersection, Punctuation:Judgment)
//Inheritance to Similarity
(S <-> P), (S --> P), task("?") |- (S <-> P), (Truth:StructuralAbduction, Punctuation:Judgment)
//Set Definition Similarity to Inheritance
(S <-> {P}), S |- (S --> {P}), (Truth:Identity, Desire:Identity, Derive:AllowBackward)
(S <-> {P}), {P} |- (S --> {P}), (Truth:Identity, Desire:Identity, Derive:AllowBackward)
([S] <-> P), [S] |- ([S] --> P), (Truth:Identity, Desire:Identity, Derive:AllowBackward)
([S] <-> P), P |- ([S] --> P), (Truth:Identity, Desire:Identity, Derive:AllowBackward)
({S} <-> {P}), {S} |- ({P} --> {S}), (Truth:Identity, Desire:Identity, Derive:AllowBackward)
({S} <-> {P}), {P} |- ({P} --> {S}), (Truth:Identity, Desire:Identity, Derive:AllowBackward)
([S] <-> [P]), [S] |- ([P] --> [S]), (Truth:Identity, Desire:Identity, Derive:AllowBackward)
([S] <-> [P]), [P] |- ([P] --> [S]), (Truth:Identity, Desire:Identity, Derive:AllowBackward)
//Set Definition Unwrap
({S} <-> {P}), {S} |- (S <-> P), (Truth:Identity, Desire:Identity, Derive:AllowBackward)
({S} <-> {P}), {P} |- (S <-> P), (Truth:Identity, Desire:Identity, Derive:AllowBackward)
([S] <-> [P]), [S] |- (S <-> P), (Truth:Identity, Desire:Identity, Derive:AllowBackward)
([S] <-> [P]), [P] |- (S <-> P), (Truth:Identity, Desire:Identity, Derive:AllowBackward)
//Nothing is more specific than a instance, so its similar
(S --> {P}), S |- (S <-> {P}), (Truth:Identity, Desire:Identity, Derive:AllowBackward)
(S --> {P}), {P} |- (S <-> {P}), (Truth:Identity, Desire:Identity, Derive:AllowBackward)
//nothing is more general than a property, so its similar
([S] --> P), [S] |- ([S] <-> P), (Truth:Identity, Desire:Identity, Derive:AllowBackward)
([S] --> P), P |- ([S] <-> P), (Truth:Identity, Desire:Identity, Derive:AllowBackward)
////// Truth-value functions: see TruthFunctions.java
//// Immediate Inference ////////////////////////////////////////////////////////////////////////////////////
//If S can stand for P, P can to a certain low degree also represent the class S
//If after S usually P happens, then it might be a good guess that usually before P happens, S happens.
(P --> S), (S --> P), task("?") |- (P --> S), (Truth:Conversion, Punctuation:Judgment)
(P --> S), (S --> P), task("?") |- (P --> S), (Truth:Conversion, Punctuation:Judgment)
(P ==> S), (S ==> P), task("?") |- (P ==> S), (Truth:Conversion, Punctuation:Judgment)
(P =|> S), (S =|> P), task("?") |- (P =|> S), (Truth:Conversion, Punctuation:Judgment)
(P =\> S), (S =/> P), task("?") |- (P =\> S), (Truth:Conversion, Punctuation:Judgment)
(P =/> S), (S =\> P), task("?") |- (P =/> S), (Truth:Conversion, Punctuation:Judgment)
// "If not smoking lets you be healthy, being not healthy may be the result of smoking"
( --S ==> P), P |- ( --P ==> S), (Truth:Contraposition, Derive:AllowBackward)
( --S ==> P), --S |- ( --P ==> S), (Truth:Contraposition, Derive:AllowBackward)
( --S =|> P), P |- ( --P =|> S), (Truth:Contraposition, Derive:AllowBackward)
( --S =|> P), --S |- ( --P =|> S), (Truth:Contraposition, Derive:AllowBackward)
( --S =/> P), P |- ( --P =\> S), (Truth:Contraposition, Derive:AllowBackward)
( --S =/> P), --S |- ( --P =\> S), (Truth:Contraposition, Derive:AllowBackward)
( --S =\> P), P |- ( --P =/> S), (Truth:Contraposition, Derive:AllowBackward)
( --S =\> P), --S |- ( --P =/> S), (Truth:Contraposition, Derive:AllowBackward)
//A belief b <f,c> is equal to --b <1-f,c>, which is the negation rule:
(A --> B), A |- --(A --> B), (Truth:Negation, Desire:Negation, Derive:AllowBackward)
(A --> B), B |- --(A --> B), (Truth:Negation, Desire:Negation, Derive:AllowBackward)
--(A --> B), A |- (A --> B), (Truth:Negation, Desire:Negation, Derive:AllowBackward)
--(A --> B), B |- (A --> B), (Truth:Negation, Desire:Negation, Derive:AllowBackward)
(A <-> B), A |- --(A <-> B), (Truth:Negation, Desire:Negation, Derive:AllowBackward)
(A <-> B), B |- --(A <-> B), (Truth:Negation, Desire:Negation, Derive:AllowBackward)
--(A <-> B), A |- (A <-> B), (Truth:Negation, Desire:Negation, Derive:AllowBackward)
--(A <-> B), B |- (A <-> B), (Truth:Negation, Desire:Negation, Derive:AllowBackward)
(A ==> B), A |- --(A ==> B), (Truth:Negation, Desire:Negation, Derive:AllowBackward, Order:ForAllSame)
(A ==> B), B |- --(A ==> B), (Truth:Negation, Desire:Negation, Derive:AllowBackward, Order:ForAllSame)
--(A ==> B), A |- (A ==> B), (Truth:Negation, Desire:Negation, Derive:AllowBackward, Order:ForAllSame)
--(A ==> B), B |- (A ==> B), (Truth:Negation, Desire:Negation, Derive:AllowBackward, Order:ForAllSame)
(A <=> B), A |- --(A <=> B), (Truth:Negation, Desire:Negation, Derive:AllowBackward, Order:ForAllSame)
(A <=> B), B |- --(A <=> B), (Truth:Negation, Desire:Negation, Derive:AllowBackward, Order:ForAllSame)
--(A <=> B), A |- (A <=> B), (Truth:Negation, Desire:Negation, Derive:AllowBackward, Order:ForAllSame)
--(A <=> B), B |- (A <=> B), (Truth:Negation, Desire:Negation, Derive:AllowBackward, Order:ForAllSame)
//TODO: probably make simpler by just allowing it for all tasks in general
//// inheritance-based syllogism ////////////////////////////////////////////////////////////////////////////////////
// (A --> B) ------- (B --> C)
// \ /
// \ /
// \ /
// \ /
// (A --> C)
//If A is a special case of B, and B is a special case of C, so is A a special case of C (strong), the other variations are hypotheses (weak)
(A --> B), (B --> C), not_equal(A,C) |- (A --> C), (Truth:Deduction, Desire:Strong, Derive:AllowBackward)
(A --> B), (A --> C), not_equal(B,C) |- (C --> B), (Truth:Abduction, Desire:Weak, Derive:AllowBackward)
(A --> C), (B --> C), not_equal(A,B) |- (B --> A), (Truth:Induction, Desire:Weak, Derive:AllowBackward)
(A --> B), (B --> C), not_equal(C,A) |- (C --> A), (Truth:Exemplification, Desire:Weak, Derive:AllowBackward)
//// similarity from inheritance ////////////////////////////////////////////////////////////////////////////////////
//If S is a special case of P, and P is a special case of S, then S and P are similar
(S --> P), (P --> S) |- (S <-> P), (Truth:Intersection, Desire:Strong, Derive:AllowBackward)
//// inheritance from similarty <- TODO check why this one was missing ////////////////////////////////////////////////////////////////////////////////////
(S <-> P), (P --> S) |- (S --> P), (Truth:ReduceConjunction, Desire:Strong, Derive:AllowBackward)
//// similarity-based syllogism ////////////////////////////////////////////////////////////////////////////////////
//If P and S are a special case of M, then they might be similar (weak),
//also if P and S are a general case of M
(P --> M), (S --> M), not_equal(S,P) |- (S <-> P), (Truth:Comparison, Desire:Weak, Derive:AllowBackward)
(M --> P), (M --> S), not_equal(S,P) |- (S <-> P), (Truth:Comparison, Desire:Weak, Derive:AllowBackward)
//If M is a special case of P and S and M are similar, then S is also a special case of P (strong)
(M --> P), (S <-> M), not_equal(S,P) |- (S --> P), (Truth:Analogy, Desire:Strong, Derive:AllowBackward)
(P --> M), (S <-> M), not_equal(S,P) |- (P --> S), (Truth:Analogy, Desire:Strong, Derive:AllowBackward)
(M <-> P), (S <-> M), not_equal(S,P) |- (S <-> P), (Truth:Resemblance, Desire:Strong, Derive:AllowBackward)
//// inheritance-based composition ////////////////////////////////////////////////////////////////////////////////////
//If P and S are in the intension/extension of M, then union/difference and intersection can be built:
(P --> M), (S --> M), not_set(S), not_set(P), not_equal(S,P), no_common_subterm(S,P) |- ((S | P) --> M), (Truth:Intersection),
((S & P) --> M), (Truth:Union),
((P ~ S) --> M), (Truth:Difference)
(M --> P), (M --> S), not_set(S), not_set(P), not_equal(S,P), no_common_subterm(S,P) |- (M --> (P & S)), (Truth:Intersection),
(M --> (P | S)), (Truth:Union),
(M --> (P - S)), (Truth:Difference)
//// inheritance-based decomposition ////////////////////////////////////////////////////////////////////////////////////
//if (S --> M) is the case, and ((|,S,A_1..n) --> M) is not the case, then ((|,A_1..n) --> M) is not the case, hence Truth:DecomposePositiveNegativeNegative
(S --> M), ((|,S,A_1..n) --> M) |- ((|,A_1..n) --> M), (Truth:DecomposePositiveNegativeNegative)
(S --> M), ((&,S,A_1..n) --> M) |- ((&,A_1..n) --> M), (Truth:DecomposeNegativePositivePositive)
(S --> M), ((S ~ P) --> M) |- (P --> M), (Truth:DecomposePositiveNegativePositive)
(S --> M), ((P ~ S) --> M) |- (P --> M), (Truth:DecomposeNegativeNegativeNegative)
(M --> S), (M --> (&,S,A_1..n)) |- (M --> (&,A_1..n)), (Truth:DecomposePositiveNegativeNegative)
(M --> S), (M --> (|,S,A_1..n)) |- (M --> (|,A_1..n)), (Truth:DecomposeNegativePositivePositive)
(M --> S), (M --> (S - P)) |- (M --> P), (Truth:DecomposePositiveNegativePositive)
(M --> S), (M --> (P - S)) |- (M --> P), (Truth:DecomposeNegativeNegativeNegative)
//Set comprehension:
(C --> A), (C --> B), set_ext(A), union(A,B,R) |- (C --> R), (Truth:Union)
(C --> A), (C --> B), set_int(A), union(A,B,R) |- (C --> R), (Truth:Intersection)
(A --> C), (B --> C), set_ext(A), union(A,B,R) |- (R --> C), (Truth:Intersection)
(A --> C), (B --> C), set_int(A), union(A,B,R) |- (R --> C), (Truth:Union)
(C --> A), (C --> B), set_ext(A), intersection(A,B,R) |- (C --> R), (Truth:Intersection)
(C --> A), (C --> B), set_int(A), intersection(A,B,R) |- (C --> R), (Truth:Union)
(A --> C), (B --> C), set_ext(A), intersection(A,B,R) |- (R --> C), (Truth:Union)
(A --> C), (B --> C), set_int(A), intersection(A,B,R) |- (R --> C), (Truth:Intersection)
(C --> A), (C --> B), difference(A,B,R) |- (C --> R), (Truth:Difference)
(A --> C), (B --> C), difference(A,B,R) |- (R --> C), (Truth:Difference)
//Set element takeout:
(C --> {A_1..n}), C |- (C --> {A_i}), (Truth:StructuralDeduction)
(C --> [A_1..n]), C |- (C --> [A_i]), (Truth:StructuralDeduction)
({A_1..n} --> C), C |- ({A_i} --> C), (Truth:StructuralDeduction)
([A_1..n] --> C), C |- ([A_i] --> C), (Truth:StructuralDeduction)
//NAL3 single premise inference:
((|,A_1..n) --> M), M |- (A_i --> M), (Truth:StructuralDeduction)
(M --> (&,A_1..n)), M |- (M --> A_i), (Truth:StructuralDeduction)
((B ~ G) --> S), S |- (B --> S), (Truth:StructuralDeduction)
(R --> (B - S)), R |- (R --> B), (Truth:StructuralDeduction)
////// NAL4 - Transformations between products and images: ////////////////////////////////////////////////////////////////////////////////////
//Relations and transforming them into different representations so that arguments and the relation itself can become the subject or predicate
((A_1..n) --> M), A_i |- (A_i --> (/,M, A_1..A_i.substitute(_)..A_n )), (Truth:Identity, Desire:Identity)
(M --> (A_1..n)), A_i |- ((\,M, A_1..A_i.substitute(_)..A_n ) --> A_i), (Truth:Identity, Desire:Identity)
(A_i --> (/,M,A_1..A_i.substitute(_)..A_n )), M |- ((A_1..n) --> M), (Truth:Identity, Desire:Identity)
((\,M, A_1..A_i.substitute(_)..A_n ) --> A_i), M |- (M --> (A_1..n)), (Truth:Identity, Desire:Identity)
//// implication-based syllogism ////////////////////////////////////////////////////////////////////////////////////
// (A ==> B) ------- (B ==> C)
// \ /
// \ /
// \ /
// \ /
// (A ==> C)
//If after S M happens, and after M P happens, so P happens after S
(M ==> P), (S ==> M), not_equal(S,P) |- (S ==> P), (Truth:Deduction, Order:ForAllSame, Derive:AllowBackward)
(P ==> M), (S ==> M), not_equal(S,P) |- (S ==> P), (Truth:Induction, Derive:AllowBackward)
(P =|> M), (S =|> M), not_equal(S,P) |- (S =|> P), (Truth:Induction, Derive:AllowBackward)
(P =/> M), (S =/> M), not_equal(S,P) |- (S =|> P), (Truth:Induction, Derive:AllowBackward)
(P =\> M), (S =\> M), not_equal(S,P) |- (S =|> P), (Truth:Induction, Derive:AllowBackward)
(M ==> P), (M ==> S), not_equal(S,P) |- (S ==> P), (Truth:Abduction, Derive:AllowBackward)
(M =/> P), (M =/> S), not_equal(S,P) |- (S =|> P), (Truth:Abduction, Derive:AllowBackward)
(M =|> P), (M =|> S), not_equal(S,P) |- (S =|> P), (Truth:Abduction, Derive:AllowBackward)
(M =\> P), (M =\> S), not_equal(S,P) |- (S =|> P), (Truth:Abduction, Derive:AllowBackward)
(P ==> M), (M ==> S), not_equal(S,P) |- (S ==> P), (Truth:Exemplification, Derive:AllowBackward)
(P =/> M), (M =/> S), not_equal(S,P) |- (S =\> P), (Truth:Exemplification, Derive:AllowBackward)
(P =\> M), (M =\> S), not_equal(S,P) |- (S =/> P), (Truth:Exemplification, Derive:AllowBackward)
(P =|> M), (M =|> S), not_equal(S,P) |- (S =|> P), (Truth:Exemplification, Derive:AllowBackward)
//// implication to equivalence ////////////////////////////////////////////////////////////////////////////////////
//If when S happens, P happens, and before P happens, S has happened, then they are truth-related equivalent
(S ==> P), (P ==> S), not_equal(S,P) |- (S <=> P), (Truth:Intersection, Derive:AllowBackward)
(S =|> P), (P =|> S), not_equal(S,P) |- (S <|> P), (Truth:Intersection, Derive:AllowBackward)
(S =/> P), (P =\> S), not_equal(S,P) |- (S </> P), (Truth:Intersection, Derive:AllowBackward)
(S =\> P), (P =/> S), not_equal(S,P) |- (P </> S), (Truth:Intersection, Derive:AllowBackward)
//// equivalence-based syllogism ////////////////////////////////////////////////////////////////////////////////////
//Same as for inheritance again
(P ==> M), (S ==> M), not_equal(S,P) |- (S <=> P), (Truth:Comparison, Derive:AllowBackward)
(P =/> M), (S =/> M), not_equal(S,P) |- (S <|> P), (Truth:Comparison, Derive:AllowBackward),
(S </> P), (Truth:Comparison, Derive:AllowBackward),
(P </> S), (Truth:Comparison, Derive:AllowBackward)
(P =|> M), (S =|> M), not_equal(S,P) |- (S <|> P), (Truth:Comparison, Derive:AllowBackward)
(P =\> M), (S =\> M), not_equal(S,P) |- (S <|> P), (Truth:Comparison, Derive:AllowBackward),
(S </> P), (Truth:Comparison, Derive:AllowBackward),
(P </> S), (Truth:Comparison, Derive:AllowBackward)
(M ==> P), (M ==> S), not_equal(S,P) |- (S <=> P), (Truth:Comparison, Derive:AllowBackward)
(M =/> P), (M =/> S), not_equal(S,P) |- (S <|> P), (Truth:Comparison, Derive:AllowBackward),
(S </> P), (Truth:Comparison, Derive:AllowBackward),
(P </> S), (Truth:Comparison, Derive:AllowBackward)
(M =|> P), (M =|> S), not_equal(S,P) |- (S <|> P), (Truth:Comparison, Derive:AllowBackward)
//Same as for inheritance again
(M ==> P), (S <=> M), not_equal(S,P) |- (S ==> P), (Truth:Analogy, Derive:AllowBackward)
(M =/> P), (S </> M), not_equal(S,P) |- (S =/> P), (Truth:Analogy, Derive:AllowBackward)
(M =/> P), (S <|> M), not_equal(S,P) |- (S =/> P), (Truth:Analogy, Derive:AllowBackward)
(M =|> P), (S <|> M), not_equal(S,P) |- (S =|> P), (Truth:Analogy, Derive:AllowBackward)
(M =\> P), (M </> S), not_equal(S,P) |- (S =\> P), (Truth:Analogy, Derive:AllowBackward)
(M =\> P), (S <|> M), not_equal(S,P) |- (S =\> P), (Truth:Analogy, Derive:AllowBackward)
(P ==> M), (S <=> M), not_equal(S,P) |- (P ==> S), (Truth:Analogy, Derive:AllowBackward)
(P =/> M), (S <|> M), not_equal(S,P) |- (P =/> S), (Truth:Analogy, Derive:AllowBackward)
(P =|> M), (S <|> M), not_equal(S,P) |- (P =|> S), (Truth:Analogy, Derive:AllowBackward)
(P =\> M), (S </> M), not_equal(S,P) |- (P =\> S), (Truth:Analogy, Derive:AllowBackward)
(P =\> M), (S <|> M), not_equal(S,P) |- (P =\> S), (Truth:Analogy, Derive:AllowBackward)
(M <=> P), (S <=> M), not_equal(S,P) |- (S <=> P), (Truth:Resemblance, Order:ForAllSame, Derive:AllowBackward)
(M </> P), (S <|> M), not_equal(S,P) |- (S </> P), (Truth:Resemblance, Derive:AllowBackward)
(M <|> P), (S </> M), not_equal(S,P) |- (S </> P), (Truth:Resemblance, Derive:AllowBackward)
//// implication-based composition ////////////////////////////////////////////////////////////////////////////////////
//Same as for inheritance again
(P ==> M), (S ==> M), not_equal(S,P) |- ((P || S) ==> M), (Truth:Intersection),
((P && S) ==> M), (Truth:Union)
(P =|> M), (S =|> M), not_equal(S,P) |- ((P || S) =|> M), (Truth:Intersection),
((P &| S) =|> M), (Truth:Union)
(P =/> M), (S =/> M), not_equal(S,P) |- ((P || S) =/> M), (Truth:Intersection),
((P &| S) =/> M), (Truth:Union)
(P =\> M), (S =\> M), not_equal(S,P) |- ((P || S) =\> M), (Truth:Intersection),
((P &| S) =\> M), (Truth:Union)
(M ==> P), (M ==> S), not_equal(S,P) |- (M ==> (P && S)), (Truth:Intersection),
(M ==> (P || S)), (Truth:Union)
(M =/> P), (M =/> S), not_equal(S,P) |- (M =/> (P &| S)), (Truth:Intersection),
(M =/> (P || S)), (Truth:Union)
(M =|> P), (M =|> S), not_equal(S,P) |- (M =|> (P &| S)), (Truth:Intersection),
(M =|> (P || S)), (Truth:Union)
(M =\> P), (M =\> S), not_equal(S,P) |- (M =\> (P &| S)), (Truth:Intersection),
(M =\> (P || S)), (Truth:Union)
(D =/> R), (D =\> K), not_equal(R,K) |- (K =/> R), (Truth:Abduction),
(R =\> K), (Truth:Induction),
(K </> R), (Truth:Comparison)
//// implication-based decomposition ////////////////////////////////////////////////////////////////////////////////////
//Same as for inheritance again
(S ==> M), ((||,S,A_1..n) ==> M) |- ((||,A_1..n) ==> M), (Truth:DecomposePositiveNegativeNegative, Order:ForAllSame)
(S ==> M), ((&&,S,A_1..n) ==> M) |- ((&&,A_1..n) ==> M), (Truth:DecomposeNegativePositivePositive, Order:ForAllSame, SequenceIntervals:FromPremises)
(M ==> S), (M ==> (&&,S,A_1..n)) |- (M ==> (&&,A_1..n)), (Truth:DecomposePositiveNegativeNegative, Order:ForAllSame, SequenceIntervals:FromPremises)
(M ==> S), (M ==> (||,S,A_1..n)) |- (M ==> (||,A_1..n)), (Truth:DecomposeNegativePositivePositive, Order:ForAllSame)
//// conditional syllogism ////////////////////////////////////////////////////////////////////////////////////
//If after M, P usually happens, and M happens, it means P is expected to happen
M, (M ==> P), shift_occurrence_forward(unused,"==>") |- P, (Truth:Deduction, Desire:Induction, Order:ForAllSame)
M, (P ==> M), shift_occurrence_backward(unused,"==>") |- P, (Truth:Abduction, Desire:Deduction, Order:ForAllSame)
M, (S <=> M), shift_occurrence_backward(unused,"<=>") |- S, (Truth:Analogy, Desire:Strong, Order:ForAllSame)
M, (M <=> S), shift_occurrence_forward(unused,"==>") |- S, (Truth:Analogy, Desire:Strong, Order:ForAllSame)
//// conditional composition: ////////////////////////////////////////////////////////////////////////////////////
//They are let out for AGI purpose, don't let the system generate conjunctions or useless <=> and ==> statements
//For this there needs to be a semantic dependence between both, either by the predicate or by the subject,
//or a temporal dependence which acts as special case of semantic dependence
//These cases are handled by "Variable Introduction" and "Temporal Induction"
// P, S, no_common_subterm(S,P) |- (S ==> P), (Truth:Induction)
// P, S, no_common_subterm(S,P) |- (S <=> P), (Truth:Comparison)
// P, S, no_common_subterm(S,P) |- (P && S), (Truth:Intersection)
// P, S, no_common_subterm(S,P) |- (P || S), (Truth:Union)
//// conjunction decompose
(&&,A_1..n), A_1 |- A_1, (Truth:StructuralDeduction, Desire:StructuralStrong)
(&/,A_1..n), A_1 |- A_1, (Truth:StructuralDeduction, Desire:StructuralStrong)
(&|,A_1..n), A_1 |- A_1, (Truth:StructuralDeduction, Desire:StructuralStrong)
(&/,B,A_1..n), B, task("!") |- (&/,A_1..n), (Truth:Deduction, Desire:Strong, SequenceIntervals:FromPremises)
//// propositional decomposition ////////////////////////////////////////////////////////////////////////////////////
//If S is the case, and (&&,S,A_1..n) is not the case, it can't be that (&&,A_1..n) is the case
S, (&/,S,A_1..n) |- (&/,A_1..n), (Truth:DecomposePositiveNegativeNegative, SequenceIntervals:FromPremises)
S, (&|,S,A_1..n) |- (&|,A_1..n), (Truth:DecomposePositiveNegativeNegative)
S, (&&,S,A_1..n) |- (&&,A_1..n), (Truth:DecomposePositiveNegativeNegative)
S, (||,S,A_1..n) |- (||,A_1..n), (Truth:DecomposeNegativePositivePositive)
//Additional for negation: https://groups.google.com/forum/#!topic/open-nars/g-7r0jjq2Vc
S, (&/,(--,S),A_1..n) |- (&/,A_1..n), (Truth:DecomposeNegativeNegativeNegative, SequenceIntervals:FromPremises)
S, (&|,(--,S),A_1..n) |- (&|,A_1..n), (Truth:DecomposeNegativeNegativeNegative)
S, (&&,(--,S),A_1..n) |- (&&,A_1..n), (Truth:DecomposeNegativeNegativeNegative)
S, (||,(--,S),A_1..n) |- (||,A_1..n), (Truth:DecomposePositivePositivePositive)
//// multi-conditional syllogism ////////////////////////////////////////////////////////////////////////////////////
//Inference about the pre/postconditions
Y, ((&&,X,A_1..n) ==> B), substitute_if_unifies("$",X,Y) |- ((&&,A_1..n) ==> B), (Truth:Deduction, Order:ForAllSame, SequenceIntervals:FromPremises)
((&&,M,A_1..n) ==> C), ((&&,A_1..n) ==> C) |- M, (Truth:Abduction, Order:ForAllSame)
//Can be derived by NAL7 rules so this won't be necessary there (Order:ForAllSame left out here)
//the first rule does not have Order:ForAllSame because it would be invalid, see: https://groups.google.com/forum/#!topic/open-nars/r5UJo64Qhrk
((&&,A_1..n) ==> C), M, not_implication_or_equivalence(M) |- ((&&,M,A_1..n) ==> C), (Truth:Induction)
((&&,A_1..n) =|> C), M, not_implication_or_equivalence(M) |- ((&&,M,A_1..n) =|> C), (Truth:Induction)
((&&,A_1..n) =/> C), M, not_implication_or_equivalence(M) |- ((&&,M,A_1..n) =/> C), (Truth:Induction)
((&&,A_1..n) =\> C), M, not_implication_or_equivalence(M) |- ((&&,M,A_1..n) =\> C), (Truth:Induction)
(A ==> M), ((&&,M,A_1..n) ==> C) |- ((&&,A,A_1..n) ==> C), (Truth:Deduction, Order:ForAllSame, SequenceIntervals:FromPremises)
((&&,M,A_1..n) ==> C), ((&&,A,A_1..n) ==> C) |- (A ==> M), (Truth:Induction, Order:ForAllSame)
(A ==> M), ((&&,A,A_1..n) ==> C) |- ((&&,M,A_1..n) ==> C), (Truth:Abduction, Order:ForAllSame, SequenceIntervals:FromPremises)
//// variable introduction ////////////////////////////////////////////////////////////////////////////////////
//Introduce variables by common subject or predicate
(S --> M), (P --> M), not_equal(S,P) |- ((P --> $X) ==> (S --> $X)), (Truth:Abduction),
((S --> $X) ==> (P --> $X)), (Truth:Induction),
((P --> $X) <=> (S --> $X)), (Truth:Comparison),
(&&,(S --> #Y),(P --> #Y)), (Truth:Intersection)
(S --> M), (P --> M), not_equal(S,P), measure_time(I) |- ((&/,(P --> $X),I) =/> (S --> $X)), (Truth:Induction, Linkage:Temporal),
((S --> $X) =\> (&/,(P --> $X),I)), (Truth:Abduction, Linkage:Temporal),
((&/,(P --> $X),I) </> (S --> $X)), (Truth:Comparison, Linkage:Temporal),
(&/,(P --> #Y), I, (S --> #Y)), (Truth:Intersection, Linkage:Temporal)
(S --> M), (P --> M), not_equal(S,P), concurrent(Task,Belief) |- ((P --> $X) =|> (S --> $X)), (Truth:Abduction, Linkage:Temporal),
((S --> $X) =|> (P --> $X)), (Truth:Induction, Linkage:Temporal),
((P --> $X) <|> (S --> $X)), (Truth:Comparison, Linkage:Temporal),
(&|,(P --> #Y),(S --> #Y)), (Truth:Intersection, Linkage:Temporal)
(M --> S), (M --> P), not_equal(S,P) |- (($X --> S) ==> ($X --> P)), (Truth:Induction),
(($X --> P) ==> ($X --> S)), (Truth:Abduction),
(($X --> S) <=> ($X --> P)), (Truth:Comparison),
(&&,(#Y --> S),(#Y --> P)), (Truth:Intersection)
(M --> S), (M --> P), not_equal(S,P), measure_time(I) |- ((&/,($X --> P),I) =/> ($X --> S)), (Truth:Induction, Linkage:Temporal),
(($X --> S) =\> (&/,($X --> P),I)), (Truth:Abduction, Linkage:Temporal),
((&/,($X --> P),I) </> ($X --> S)), (Truth:Comparison, Linkage:Temporal),
(&/,(#Y --> P), I ,(#Y --> S)), (Truth:Intersection, Linkage:Temporal)
(M --> S), (M --> P), not_equal(S,P), concurrent((M --> P),(M --> S)) |- (($X --> S) =|> ($X --> P)), (Truth:Induction, Linkage:Temporal),
(($X --> P) =|> ($X --> S)), (Truth:Abduction, Linkage:Temporal),
(($X --> S) <|> ($X --> P)), (Truth:Comparison, Linkage:Temporal),
(&|,(#Y --> S),(#Y --> P)), (Truth:Intersection, Linkage:Temporal)
//// 2nd variable introduction ////////////////////////////////////////////////////////////////////////////////////
(A ==> (M --> P)), (M --> S), not_equal(A, (M --> S)) |- ((&&,A,($X --> S)) ==> ($X --> P)), (Truth:Induction),
(&&,(A ==> (#Y --> P)), (#Y --> S)), (Truth:Intersection)
(&&,(M --> P), A_1..n), (M --> S), not_equal(S,P) |- (($Y --> S) ==> (&&,($Y --> P), A_1..n)), (Truth:Induction),
(&&,(#Y --> S), (#Y --> P), A_1..n), (Truth:Intersection)
(A ==> (P --> M)), (S --> M), not_equal(S,P), not_equal(A, (S --> M)) |- ((&&,A,(P --> $X)) ==> (S --> $X)), (Truth:Abduction),
(&&,(A ==> (P --> #Y)), (S --> #Y)), (Truth:Intersection)
(&&,(P --> M), A_1..n), (S --> M), not_equal(S,P) |- ((S --> $Y) ==> (&&,(P --> $Y), A_1..n)), (Truth:Abduction),
(&&, (S --> #Y), (P --> #Y), A_1..n), (Truth:Intersection)
(A --> L), ((A --> S) ==> R) |- ((&&,(#X --> L),(#X --> S)) ==> R), (Truth:Induction)
(A --> L), ((&&,(A --> S),A_1..n) ==> R), substitute(A,#X) |- ((&&,(#X --> L),(#X --> S),A_1..n) ==> R), (Truth:Induction)
//// dependent variable elimination ////////////////////////////////////////////////////////////////////////////////////
//Decomposition with elimination of a variable
B, (&&,A, A_1..n), task("."), substitute_if_unifies("#",A,B) |- (&&,A_1..n), (Truth:AnonymousAnalogy, Desire:Strong, Order:ForAllSame, SequenceIntervals:FromPremises)
//conditional abduction by dependent variable
((A --> R) ==> Z), ((&&,(#Y --> B),(#Y --> R),A_1..n) ==> Z) |- (A --> B), (Truth:Abduction)
((A --> R) ==> Z), ((&&,(#Y --> B),(#Y --> R)) ==> Z) |- (A --> B), (Truth:Abduction)
// conditional deduction "An inverse inference has been implemented as a form of deduction" https://code.google.com/p/open-nars/issues/detail?id=40&can=1
(U --> L), ((&&,(#X --> L),(#X --> R)) ==> Z) |- ((U --> R) ==> Z), (Truth:Deduction)
(U --> L), ((&&,(#X --> L),(#X --> R),A_1..n) ==> Z), substitute(#X,U) |- ((&&,(U --> R),A_1..n) ==> Z), (Truth:Deduction)
//// independent variable elimination ////////////////////////////////////////////////////////////////////////////////////
B, (A ==> C), substitute_if_unifies("$",A,B), shift_occurrence_forward(unused,"==>") |- C, (Truth:Deduction, Order:ForAllSame)
B, (C ==> A), substitute_if_unifies("$",A,B), shift_occurrence_backward(unused,"==>") |- C, (Truth:Abduction, Order:ForAllSame)
B, (A <=> C), substitute_if_unifies("$",A,B), shift_occurrence_backward(unused,"<=>") |- C, (Truth:Deduction, Order:ForAllSame)
B, (C <=> A), substitute_if_unifies("$",A,B), shift_occurrence_forward(unused,"<=>") |- C, (Truth:Deduction, Order:ForAllSame)
//// second level variable handling rules ////////////////////////////////////////////////////////////////////////////////////
//second level variable elimination (termlink level2 growth needed in order for these rules to work)
(A --> K), (&&,(#X --> L),(($Y --> K) ==> (&&,A_1..n))), substitute($Y,A) |- (&&,(#X --> L),A_1..n), (Truth:Deduction)
(A --> K), (($X --> L) ==> (&&,(#Y --> K),A_1..n)), substitute(#Y,A) |- (($X --> L) ==> (&&,A_1..n)), (Truth:AnonymousAnalogy)
//precondition combiner inference rule (variable_unification6):
((&&,C,A_1..n) ==> Z), ((&&,C,B_1..m) ==> Z) |- ((&&,A_1..n) ==> (&&,B_1..m)), (Truth:Induction)
((&&,C,A_1..n) ==> Z), ((&&,C,B_1..m) ==> Z) |- ((&&,B_1..m) ==> (&&,A_1..n)), (Truth:Induction)
(Z ==> (&&,C,A_1..n)), (Z ==> (&&,C,B_1..m)) |- ((&&,A_1..n) ==> (&&,B_1..m)), (Truth:Abduction)
(Z ==> (&&,C,A_1..n)), (Z ==> (&&,C,B_1..m)) |- ((&&,B_1..m) ==> (&&,A_1..n)), (Truth:Abduction)
////NAL7 specific inference ////////////////////////////////////////////////////////////////////////////////////
//Reasoning about temporal statements. those are using the ==> relation because relation in time is a relation of the truth between statements.
X, (XI ==> B), substitute_if_unifies("$",XI,(&/,X,/0)), shift_occurrence_forward(XI,"==>") |- B, (Truth:Deduction, Desire:Induction, Order:ForAllSame)
X, (BI ==> Y), substitute_if_unifies("$",Y,X), shift_occurrence_backward(BI,"==>") |- BI, (Truth:Abduction, Desire:Deduction, Order:ForAllSame)
////Temporal induction: ////////////////////////////////////////////////////////////////////////////////////
//When P and then S happened according to an observation, by induction (weak) it may be that alyways after P, usually S happens.
P, S, measure_time(I), not_implication_or_equivalence(P), not_implication_or_equivalence(S) |- ((&/,S,I) =/> P), (Truth:Induction, Linkage:Temporal),
(P =\> (&/,S,I)), (Truth:Abduction, Linkage:Temporal),
((&/,S,I) </> P), (Truth:Comparison, Linkage:Temporal),
(&/,S,I,P), (Truth:Intersection, Linkage:Temporal)
P, S, concurrent(Task,Belief), not_implication_or_equivalence(P), not_implication_or_equivalence(S) |- (S =|> P), (Truth:Induction, Linkage:Temporal),
(P =|> S), (Truth:Induction, Linkage:Temporal),
(S <|> P), (Truth:Comparison, Linkage:Temporal),
(&|,S,P), (Truth:Intersection, Linkage:Temporal)
////backward inference is mostly handled by the rule transformation:
// T, B |- C, [post] =>
// C, B, task("?") |- T, [post]
// C, T, task("?") |- B, [post]
//here now are the backward inference rules which should really only work on backward inference:
(A --> S), (B --> S), task("?") |- (A --> B), (Punctuation:Question),
(B --> A), (Punctuation:Question),
(A <-> B), (Punctuation:Question)
//and the backward inference driven forward inference:
//NAL2:
([A] <-> [B]), (A <-> B), task("?") |- ([A] <-> [B]), (Truth:BeliefIdentity, Punctuation:Judgment)
({A} <-> {B}), (A <-> B), task("?") |- ({A} <-> {B}), (Truth:BeliefIdentity, Punctuation:Judgment)
([A] --> [B]), (A <-> B), task("?") |- ([A] --> [B]), (Truth:BeliefIdentity, Punctuation:Judgment)
({A} --> {B}), (A <-> B), task("?") |- ({A} --> {B}), (Truth:BeliefIdentity, Punctuation:Judgment)
//NAL3:
////composition on both sides of a statement:
((&,B,A_1..n) --> (&,A,A_1..n)), (B --> A), task("?") |- ((&,B,A_1..n) --> (&,A,A_1..n)), (Truth:BeliefStructuralDeduction, Punctuation:Judgment)
((|,B,A_1..n) --> (|,A,A_1..n)), (B --> A), task("?") |- ((|,B,A_1..n) --> (|,A,A_1..n)), (Truth:BeliefStructuralDeduction, Punctuation:Judgment)
((-,S,A) --> (-,S,B)), (B --> A), task("?") |- ((-,S,A) --> (-,S,B)), (Truth:BeliefStructuralDeduction, Punctuation:Judgment)
((~,S,A) --> (~,S,B)), (B --> A), task("?") |- ((~,S,A) --> (~,S,B)), (Truth:BeliefStructuralDeduction, Punctuation:Judgment)
////composition on one side of a statement:
(W --> (|,B,A_1..n)), (W --> B), task("?") |- (W --> (|,B,A_1..n)), (Truth:BeliefStructuralDeduction, Punctuation:Judgment)
((&,B,A_1..n) --> W), (B --> W), task("?") |- ((&,B,A_1..n) --> W), (Truth:BeliefStructuralDeduction, Punctuation:Judgment)
(W --> (-,S,B)), (W --> B), task("?") |- (W --> (-,S,B)), (Truth:BeliefStructuralDifference, Punctuation:Judgment)
((~,S,B) --> W), (B --> W), task("?") |- ((~,S,B) --> W), (Truth:BeliefStructuralDifference, Punctuation:Judgment)
//NAL4:
////composition on both sides of a statement:
((B,P) --> Z) ,(B --> A), task("?") |- ((B,P) --> (A,P)), (Truth:BeliefStructuralDeduction, Punctuation:Judgment)
((P,B) --> Z) ,(B --> A), task("?") |- ((P,B) --> (P,A)), (Truth:BeliefStructuralDeduction, Punctuation:Judgment)
(B,P) <-> Z) ,(B <-> A), task("?") |- ((B,P) <-> (A,P)), (Truth:BeliefStructuralDeduction, Punctuation:Judgment)
((P,B) <-> Z) ,(B <-> A), task("?") |- ((P,B) <-> (P,A)), (Truth:BeliefStructuralDeduction, Punctuation:Judgment)
((\,N,A,_) --> Z), (N --> R), task("?") |- ((\,N,A,_) --> (\,R,A,_)), (Truth:BeliefStructuralDeduction, Punctuation:Judgment)
((/,N,_,B) --> Z), (S --> B), task("?") |- ((/,N,_,B) --> (/,N,_,S)), (Truth:BeliefStructuralDeduction, Punctuation:Judgment)
//NAL5:
--A, A, task("?") |- --A, (Truth:BeliefNegation, Punctuation:Judgment)
A, --A, task("?") |- A, (Truth:BeliefNegation, Punctuation:Judgment)
//compound composition one premise
(||,B,A_1..n), B, task("?") |- (||,B,A_1..n), (Truth:BeliefStructuralDeduction, Punctuation:Judgment)
+69 -67
View File
@@ -1,80 +1,82 @@
(* Narsese Grammar - https://github.com/opennars/opennars/wiki/Input-Output-Format *)
task ::= [budget] sentence (* task to be processed *)
task ::= [budget] sentence (* task to be processed *)
sentence ::= statement"." [tense] [truth] (* judgement to be remembered *)
| statement"?" [tense] [truth] (* question to be answered, tense added in OpenNARS 1.7 *)
| statement"@" [tense] [truth] (* question on desire value to be answered, tense added in OpenNARS 1.7 *)
| statement"!" [tense] [truth] (* goal to be realized, tense added in OpenNARS 1.7 *)
sentence ::= statement"." [tense] [truth] (* judgement to be remembered *)
| statement"?" [tense] [truth] (* question to be answered, tense added in OpenNARS 1.7 *)
| statement"@" [tense] [truth] (* question on desire value to be answered, tense added in OpenNARS 1.7 *)
| statement"!" [tense] [truth] (* goal to be realized, tense added in OpenNARS 1.7 *)
statement ::= <"<">term copula term<">"> (* two terms related to each other *)
| <"(">term copula term<")"> (* two terms related to each other, new notation *)
| term (* a term can name a statement *)
| "(^"word {","term} ")" (* an operation to be executed *)
| word"("term {","term} ")" (* an operation to be executed, new notation *)
statement ::= <"<">term copula term<">"> (* two terms related to each other *)
| <"(">term copula term<")"> (* two terms related to each other, new notation *)
| term (* a term can name a statement *)
| "(^"word {","term} ")" (* an operation to be executed *)
| word"("term {","term} ")" (* an operation to be executed, new notation *)
copula ::= "-->" (* inheritance *)
| "<->" (* similarity *)
| "{--" (* instance *)
| "--]" (* property *)
| "{-]" (* instance-property *)
| "==>" (* implication *)
| "=/>" (* predictive implication *)
| "=|>" (* concurrent implication *)
| "=\\>" (* =\> retrospective implication *)
| "<=>" (* equivalence *)
| "</>" (* predictive equivalence *)
| "<|>" (* concurrent equivalence *)
copula ::= "-->" (* inheritance *)
| "<->" (* similarity *)
| "{--" (* instance *)
| "--]" (* property *)
| "{-]" (* instance-property *)
| "==>" (* implication *)
| "=/>" (* predictive implication *)
| "=|>" (* concurrent implication *)
| "=\\>" (* =\> retrospective implication *)
| "<=>" (* equivalence *)
| "</>" (* predictive equivalence *)
| "<|>" (* concurrent equivalence *)
term ::= word (* an atomic constant term *)
| variable (* an atomic variable term *)
| compound-term (* a term with internal structure *)
| statement (* a statement can serve as a term *)
| interval (* time measure between events *)
term ::= word (* an atomic constant term *)
| variable (* an atomic variable term *)
| compound-term (* a term with internal structure *)
| statement (* a statement can serve as a term *)
| interval (* time measure between events *)
compound-term ::= "{" term {","term} "}" (* extensional set *)
| "[" term {","term} "]" (* intensional set *)
| "("op-multi","term{","term} ")" (* compound term with infix operator *)
| "("op-single","term"," term ")" (* compound term with infix operator *)
| "(" term {","term} ")" (* product, new notation *)
| "(/," term {","term} ")" (* extensional image *)
| "(\\," term {","term} ")" (* \ intensional image *)
| "(--," term ")" (* negation *)
| "--"term (* negation, new notation *)
compound-term ::= op-ext-set term {"," term} "}" (* extensional set *)
| op-int-set term {"," term} "]" (* intensional set *)
| "("op-multi"," term {"," term} ")" (* with prefix operator *)
| "("op-single"," term "," term ")" (* with prefix operator *)
| "(" term {op-multi term} ")" (* with infix operator *)
| "(" term op-single term ")" (* with infix operator *)
| op-product term {","term} ")" (* product, new notation *)
| "(" op-ext-image "," term {"," term} ")"(* special case, extensional image *)
| "(" op-int-image "," term {"," term} ")"(* special case, \ intensional image *)
| "(" op-negation "," term ")" (* negation *)
| op-negation term (* negation, new notation *)
(* new compound-term notation *)
op-product::= "(" (* product *)
op-int-set::= "[" (* intensional set *)
op-ext-set::= "{" (* extensional set *)
op-negation::= "--" (* negation *)
op-int-image::= "\\" (* \ intensional image *)
op-ext-image::= "/" (* extensional image *)
op-multi ::= "&&" (* conjunction *)
| "*" (* product *)
| "||" (* disjunction *)
| "&|" (* parallel conjunction (of events) *)
| "&/" (* sequential conjunction (of events) *)
| "|" (* intensional intersection *)
| "&" (* extensional intersection *)
op-single ::= "-" (* extensional difference *)
| "~" (* intensional difference *)
| "(" term {op-multi term} ")" (* compound term with infix operator *)
| "(" term op-single term ")" (* compound term with infix operator *)
variable ::= "$"word (* independent variable *)
| "#"word (* dependent variable *)
| "?"word (* query variable in question *)
op-multi ::= "&&" (* conjunction *)
| "*" (* product *)
| "||" (* disjunction *)
| "&|" (* parallel events *)
| "&/" (* sequential events *)
| "|" (* intensional intersection *)
| "&" (* extensional intersection *)
tense ::= ":/:" (* future event *)
| ":|:" (* present event *)
| ":\\:" (* :\: past event *)
| <":">#"\d+"<":"> (* defined event, output only *)
op-single ::= "-" (* extensional difference *)
| "~" (* intensional difference *)
interval ::= <"/">#"\d+" (* integer *)
variable ::= "$"word (* independent variable *)
| "#"[word] (* dependent variable *)
| "?"[word] (* query variable in question *)
truth ::= <"%">frequency[<";">confidence]<"%"> (* two numbers in [0,1]x(0,1) *)
budget ::= <"$">priority[<";">durability][<";">quality]<"$"> (* three numbers in [0,1]x(0,1)x[0,1] *)
tense ::= ":/:" (* future event *)
| ":|:" (* present event *)
| ":\\:" (* :\: past event *)
| <":">#"\d+"<":"> (* defined event, output only *)
interval ::= <"/">#"\d+" (* integer *)
truth ::= <"%">frequency[<";">confidence]<"%"> (* two numbers in [0,1]x(0,1) *)
budget ::= <"$">priority[<";">durability][<";">quality]<"$"> (* three numbers in [0,1]x(0,1)x[0,1] *)
word : #"\w+" (* unicode string *)
priority : #"([0]?\.[0-9]+|1|0)" (* 0 <= x <= 1 *)
durability : #"[0]?\.[0]*[1-9]{1}[0-9]*" (* 0 < x < 1 *)
quality : #"([0]?\.[0-9]+|1|0)" (* 0 <= x <= 1 *)
frequency : #"([0]?\.[0-9]+|1|0)" (* 0 <= x <= 1 *)
confidence : #"[0]?\.[0]*[1-9]{1}[0-9]*" (* 0 < x < 1 *)
word : #"\w+" (* unicode string *)
priority : #"([0]?\.[0-9]+|1\.[0]*|1|0)" (* 0 <= x <= 1 *)
durability : #"[0]?\.[0]*[1-9]{1}[0-9]*" (* 0 < x < 1 *)
quality : #"([0]?\.[0-9]+|1\.[0]*|1|0)" (* 0 <= x <= 1 *)
frequency : #"([0]?\.[0-9]+|1\.[0]*|1|0)" (* 0 <= x <= 1 *)
confidence : #"[0]?\.[0]*[1-9]{1}[0-9]*" (* 0 < x < 1 *)
-65
View File
@@ -1,65 +0,0 @@
(ns nal.args-processing
(:refer-clojure :exclude [==])
(:require [clojure.core.logic :refer
[run* project fresh conde conso run membero defne fne !=
nonlvaro == emptyo succeed fail conda s# u# onceo all defna]
:as l]
[nal.utils :refer [subtracto]]
[clojure.core.logic.pldb :refer [db-rel db with-db]]))
(declare equ-producto sameo include1o include not-member dependento)
(defna dependento [A1 A2 A3]
([['var V L] Y ['var V [Y . L]]])
([[H . T] Y [H1 . T1]] (dependento H Y H1) (dependento T Y T1))
([['inheritance S P] Y ['inheritance S1 P1]]
(dependento S Y S1) (dependento P Y P1))
([['ext-image R A] Y ['ext-image R A1]] (dependento A Y A1))
([['int-image R A] Y ['int-image R A1]] (dependento A Y A1))
([X _ X]))
(defna equ-producto [A1 A2 A3]
([[] [] []])
([[T . Ls] [T . Lp] L]
(equ-producto Ls Lp L))
([[S . Ls] [P . Lp] [['inheritance S P] . L]]
(equ-producto Ls Lp L)))
(defna sameo [A1 A2]
([[] []])
([L [H . T]]
(nonlvaro L)
(fresh [L1] (membero H L) (subtracto L [H] L1) (sameo L1 T))))
(defn same-seto [X Y]
(fresh [x]
(!= X []) (!= X [x]) (sameo X Y) (project [X Y] (!= X Y))))
(defna include1o [A1 A2]
([[] _])
([[H . T1] [H . T2]] (include1o T1 T2))
([[H1 . T1] [H2 . T2]] (!= H2 H1) (include1o A1 T2)))
(defn includeo [L1 L2]
(all (nonlvaro L2) (include1o L1 L2) (!= L1 []) (!= L1 L2)))
;never used in project
(defn not-member [E C]
(fresh [X]
(conde [(emptyo C)]
[(conso E X C) fail]
;[(fresh [S T S1] (== E [S T]) (conso [S1 T] X C) (equivalence S S1) fail)]
[(fresh [L] (conso X L C) (not-member L C))])))
(defn replaceo
([A1 A2 A3]
((fne [A1 A2 A3]
([[T . L] T [nil . L]])
([[H . L] T [H . L1]] (replaceo L T L1)))
A1 A2 A3))
([A1 A2 A3 A4]
((fne [A1 A2 A3 A4]
([[H1 . T] H1 [H2 . T] H2])
([[H . T1] H1 [H . T2] H2] (replaceo T1 H1 T2 H2)))
A1 A2 A3 A4)))
+12 -510
View File
@@ -1,516 +1,18 @@
(ns nal.core
(:refer-clojure :exclude [!= == >= <= > < =])
(:require [clojure.core.logic :refer
[project fresh conde conda defna conso membero nonlvaro == !=
defne appendo all or* u# s# onceo lvar fne]
:as l]
[clojure.core.logic.arithmetic :refer :all]
[nal.utils :refer :all]
[nal.truth-value :refer :all]
[nal.args-processing :refer :all]
[clojure.core.logic.pldb :refer [db-rel db with-db]]))
(:require [nal.deriver.truth :as t]
[nal.deriver :refer [generate-conclusions]]
[nal.rules :as r]))
(declare similarity inheritance ext-intersection int-intersection
instance property ext-set int-set product negation
inst-prop ext-difference revision inference equivalence
inference2 inference3 equivalence-list reduceo replace-var
replace-all implication)
;===============================================================================
;revision
(defna revision [R1 R2 R]
([[S T1] [S T2] [S T]] (f-rev T1 T2 T)))
;===============================================================================
;choice
(defna choice [A1 A2 A3]
([[S [F1 C1]] [S [_F2 C2]] [S [F1 C1]]] (>= C1 C2))
([[S [_F1 C1]] [S [F2 C2]] [S [F2 C2]]] (< C1 C2))
([[S1 T1] [S2 T2] [S1 T1]]
(fresh [E1 E2]
(!= S1 S2) (f-exp T1 E1) (f-exp T2 E2) (>= E1 E2)))
([[S1 T1] [S2 T2] [S2 T2]]
(fresh [E1 E2]
(!= S1 S2) (f-exp T1 E1) (f-exp T2 E2) (< E1 E2))))
;===============================================================================
;simplified version
(defne infer3 [T1 T2 T3]
([['inheritance W1 ['ext-image ['ext-image 'represent [nil ['inheritance ['product [X T2]] R]]] [nil W2 W3]]]
['inheritance W1 ['ext-image 'represent [nil X]]]
[['inheritance ['ext-image 'represent [nil Y]] ['ext-image ['ext-image 'represent [nil ['inheritance ['product [Y T2]] R]]] [nil W2 W3]]] V]]
(f-ind [10.9] [1 0.9] V))
([['inheritance W3 ['ext-image ['ext-image 'represent [nil ['inheritance ['product [T1 X]] R]]] [W1 W2 nil]]]
['inheritance W3 ['ext-image 'represent [nil X]]]
[['inheritance ['ext-image 'represent [nil Y]] ['ext-image ['ext-image 'represent [nil ['inheritance ['product [T1 Y]] R]]] [W1 W2 nil]]] V]]
(f-ind [10.9] [1 0.9] V))
([T1 T2 T] (inference [T1 [1 0.9]] [T2 [1 0.9]] T)))
(defn infer
([T1 T2] (inference [T1 [1 0.9]] T2))
([T1 T2 T3] (infer3 T1 T2 T3)))
;===============================================================================
;inference
(defn- call [vec]
(all
(nonlvaro vec)
(project [vec]
(let [[predicat & args] vec
vr (ns-resolve 'nal.core predicat)]
(if vr (apply vr args) u#)))))
(defne inference2 [A1 A2]
;immediate inference
([[['inheritance S P] T1] [['inheritance P S] T]] (f-cnv T1 T))
([[['implication S P] T1] [['implication P S] T]] (f-cnv T1 T))
([[['implication ['negation S] P] T1] [['implication ['negation P] S] T]]
(f-cnt T1 T))
([[['negation S] T1] [S T]] (f-neg T1 T))
([[S [F1 C1]] [['negation S] T]] (< F1 0.5) (f-neg [F1 C1] T))
;structural inference
([[S1 T] [S T]]
(conda [(all (nonlvaro S) (reduceo S1 S) (!= S1 S))]
[(or* [(equivalence S1 S) (equivalence S S1)])]))
([P C] (fresh [S] (inference3 P [S [1 1]] C) (call S)))
([P C] (fresh [S] (inference3 [S [1 1]] P C) (call S))))
(defne inference3 [A1 A2 A3]
;inheritance-based syllogism
([[['inheritance M P] T1] [['inheritance S M] T2] [['inheritance S P] T]]
(noto= S P) (f-ded T1 T2 T))
([[['inheritance P M] T1] [['inheritance S M] T2] [['inheritance S P] T]]
(noto= S P) (f-abd T1 T2 T))
([[['inheritance M P] T1] [['inheritance M S] T2] [['inheritance S P] T]]
(noto= S P) (f-ind T1 T2 T))
([[['inheritance P M] T1] [['inheritance M S] T2] [['inheritance S P] T]]
(noto= S P) (f-exe T1 T2 T))
; similarity from inheritance
([[['inheritance S P] T1] [['inheritance P S] T2] [['similarity S P] T]]
(f-int T1 T2 T))
; similarity-based syllogism
([[['inheritance P M] T1] [['inheritance S M] T2] [['similarity S P] T]]
(noto= S P) (f-com T1 T2 T))
([[['inheritance M P] T1] [['inheritance M S] T2] [['similarity S P] T]]
(noto= S P) (f-com T1 T2 T))
([[['inheritance M P] T1] [['similarity S M] T2] [['inheritance S P] T]]
(noto= S P) (f-ana T1 T2 T))
([[['inheritance P M] T1] [['similarity S M] T2] [['inheritance P S] T]]
(noto= S P) (f-ana T1 T2 T))
([[['similarity M P] T1] [['similarity S M] T2] [['similarity S P] T]]
(noto= S P) (f-res T1 T2 T))
; inheritance-based composition
([[['inheritance P M] T1] [['inheritance S M] T2] [['inheritance N M] T]]
(noto= S P) (reduceo ['int-intersection [P S]] N) (f-int T1 T2 T))
([[['inheritance P M] T1] [['inheritance S M] T2] [['inheritance N M] T]]
(noto= S P) (reduceo ['ext-intersection [P S]] N) (f-uni T1 T2 T))
([[['inheritance P M] T1] [['inheritance S M] T2] [['inheritance N M] T]]
(noto= S P) (reduceo ['int-difference P S] N) (f-dif T1 T2 T))
([[['inheritance M P] T1] [['inheritance M S] T2] [['inheritance M N] T]]
(noto= S P) (reduceo ['ext-intersection [P S]] N) (f-int T1 T2 T))
([[['inheritance M P] T1] [['inheritance M S] T2] [['inheritance M N] T]]
(noto= S P) (reduceo ['int-intersection [P S]] N) (f-uni T1 T2 T))
([[['inheritance M P] T1] [['inheritance M S] T2] [['inheritance M N] T]]
(noto= S P) (reduceo ['ext-difference P S] N) (f-dif T1 T2 T))
; inheirance-based decomposition
([[['inheritance S M] T1] [['inheritance ['int-intersection L] M] T2] [['inheritance P M] T]]
(nonlvaro S) (nonlvaro L) (membero S L)
(fresh [N]
(subtracto L [S] N) (reduceo ['int-intersection N] P) (f-pnn T1 T2 T)))
([[['inheritance S M] T1] [['inheritance ['ext-intersection L] M] T2] [['inheritance P M] T]]
(nonlvaro S) (nonlvaro L) (membero S L)
(fresh [N]
(subtracto L [S] N) (reduceo ['ext-intersection N] P) (f-npp T1 T2 T)))
([[['inheritance S M] T1] [['inheritance ['int-difference S P] M] T2] [['inheritance P M] T]]
(atomo S) (atomo P) (f-pnp T1 T2 T))
([[['inheritance S M] T1] [['inheritance ['int-difference P S] M] T2] [['inheritance P M] T]]
(atomo S) (atomo P) (f-nnn T1 T2 T))
([[['inheritance M S] T1] [['inheritance M ['ext-intersection L]] T2] [['inheritance M P] T]]
(nonlvaro S) (nonlvaro L) (membero S L)
(fresh [N]
(subtracto L [S] N) (reduceo ['ext-intersection N] P) (f-pnn T1 T2 T)))
([[['inheritance M S] T1] [['inheritance M ['int-intersection L]] T2] [['inheritance M P] T]]
(nonlvaro S) (nonlvaro L) (membero S L)
(fresh [N]
(subtracto L [S] N) (reduceo ['int-intersection N] P) (f-npp T1 T2 T)))
([[['inheritance M S] T1] [['inheritance M ['ext-difference S P]] T2] [['inheritance M P] T]]
(atomo S) (atomo P) (f-pnp T1 T2 T))
([[['inheritance M S] T1] [['inheritance M ['ext-difference P S]] T2] [['inheritance M P] T]]
(atomo S) (atomo P) (f-nnn T1 T2 T))
; implication-based syllogism
([[['implication M P] T1] [['implication S M] T2] [['implication S P] T]]
(noto= S P) (f-ded T1 T2 T))
([[['implication P M] T1] [['implication S M] T2] [['implication S P] T]]
(noto= S P) (f-abd T1 T2 T))
([[['implication M P] T1] [['implication M S] T2] [['implication S P] T]]
(noto= S P) (f-ind T1 T2 T))
([[['implication P M] T1] [['implication M S] T2] [['implication S P] T]]
(noto= S P) (f-exe T1 T2 T))
; implication to equivalence
([[['implication S P] T1] [['implication P S] T2] [['equivalence S P] T]]
(f-int T1 T2 T))
; equivalence-based syllogism
([[['implication P M] T1] [['implication S M] T2] [['equivalence S P] T]]
(noto= S P) (f-com T1 T2 T))
([[['implication M P] T1] [['implication M S] T2] [['equivalence S P] T]]
(noto= S P) (f-com T1 T2 T))
([[['implication M P] T1] [['equivalence S M] T2] [['implication S P] T]]
(noto= S P) (f-ana T1 T2 T))
([[['implication P M] T1] [['equivalence S M] T2] [['implication P S] T]]
(noto= S P) (f-ana T1 T2 T))
([[['equivalence M P] T1] [['equivalence S M] T2] [['equivalence S P] T]]
(noto= S P) (f-res T1 T2 T))
; implication-based composition
([[['implication P M] T1] [['implication S M] T2] [['implication N M] T]]
(noto= S P) (reduceo ['disjunction [P S]] N) (f-int T1 T2 T))
([[['implication P M] T1] [['implication S M] T2] [['implication N M] T]]
(noto= S P) (reduceo ['conjunction [P S]] N) (f-uni T1 T2 T))
([[['implication M P] T1] [['implication M S] T2] [['implication M N] T]]
(noto= S P) (reduceo ['conjunction [P S]] N) (f-int T1 T2 T))
([[['implication M P] T1] [['implication M S] T2] [['implication M N] T]]
(noto= S P) (reduceo ['disjunction [P S]] N) (f-uni T1 T2 T))
; implication-based decomposition
([[['implication S M] T1] [['implication ['disjunction L] M] T2] [['implication P M] T]]
(nonlvaro S) (nonlvaro L) (membero S L)
(fresh [N]
(subtracto L [S] N) (reduceo ['disjunction N] P) (f-pnn T1 T2 T)))
([[['implication S M] T1] [['implication ['conjunction L] M] T2] [['implication P M] T]]
(nonlvaro S) (nonlvaro L) (membero S L)
(fresh [N]
(subtracto L [S] N) (reduceo ['conjunction N] P) (f-npp T1 T2 T)))
([[['implication M S] T1] [['implication M ['conjunction L]] T2] [['implication M P] T]]
(nonlvaro S) (nonlvaro L) (membero S L)
(fresh [N]
(subtracto L [S] N) (reduceo ['conjunction N] P) (f-pnn T1 T2 T)))
([[['implication M S] T1] [['implication M ['disjunction L]] T2] [['implication M P] T]]
(nonlvaro S) (nonlvaro L) (membero S L)
(fresh [N]
(subtracto L [S] N) (reduceo ['disjunction N] P) (f-npp T1 T2 T)))
; conditional syllogism
([[['implication M P] T1] [M T2] [P T]]
;(nonlvaro P1) (== P1 P)
(groundo P) (f-ded T1 T2 T))
([[['implication P M] T1] [M T2] [P T]]
(groundo P) (f-abd T1 T2 T))
([[M T1] [['equivalence S M] T2] [S T]]
(groundo S) (f-ana T1 T2 T))
; conditional composition
([[P T1] [S T2] [C T]]
(project [S P] (= C ['implication S P])) (f-ind T1 T2 T))
([[P T1] [S T2] [C T]]
(project [S P] (= C ['equivalence S P])) (f-com T1 T2 T))
([[P T1] [S T2] [C T]]
(fresh [N]
(reduceo ['conjunction [P S]] N)
(project [N] (= N C)) (f-int T1 T2 T)))
([[P T1] [S T2] [C T]]
(fresh [N]
(reduceo ['disjunction [P S]] N)
(project [N] (= N C)) (f-uni T1 T2 T)))
; propositional decomposition
([[S T1] [['conjunction L] T2] [P T]]
(nonlvaro S) (nonlvaro L) (membero S L)
(fresh [N]
(subtracto L [S] N) (reduceo ['conjunction N] P) (f-pnn T1 T2 T)))
([[S T1] [['disjunction L] T2] [P T]]
(nonlvaro S) (nonlvaro L) (membero S L)
(fresh [N]
(subtracto L [S] N) (reduceo ['disjunction N] P) (f-npp T1 T2 T)))
; multi-conditional syllogism
([[['implication ['conjunction L] C] T1] [M T2] [['implication P C] T]]
(fresh [A]
(nonlvaro L) (membero M L) (subtracto L [M] A)
(!= A []) (reduceo ['conjunction A] P) (f-ded T1 T2 T)))
([[['implication ['conjunction L] C] T1] [['implication P C] T2] [M T]]
(fresh [A]
(nonlvaro L) (membero M L) (subtracto L [M] A) (!= A [])
(reduceo ['conjunction A] P) (f-abd T1 T2 T)))
([[['implication ['conjunction L] C] T1] [M T2] [S T]]
(fresh [x] (conso M L x)
(project [x C] (= S ['implication ['conjunction x] C])) (f-ind T1 T2 T)))
([[['implication ['conjunction Lm] C] T1] [['implication A M] T2] [['implication P C] T]]
(fresh [La]
(nonlvaro Lm) (replaceo Lm M La A)
(reduceo ['conjunction La] P) (f-ded T1 T2 T)))
([[['implication ['conjunction Lm] C] T1] [['implication ['conjunction La] C] T2] [['implication A M] T]]
(nonlvaro Lm) (replaceo Lm M La A) (f-abd T1 T2 T))
([[['implication ['conjunction La] C] T1] [['implication A M] T2] [['implication P C] T]]
(fresh [Lm]
(nonlvaro La) (replaceo Lm M La A)
(reduceo ['conjunction Lm] P) (f-ind T1 T2 T)))
; variable introduction
([[['inheritance M P] T1] [['inheritance M S] T2] [['implication ['inheritance X S] ['inheritance X P]] T]]
(noto= S P) (f-ind T1 T2 T))
([[['inheritance P M] T1] [['inheritance S M] T2] [['implication ['inheritance P X] ['inheritance S X]] T]]
(noto= S P) (f-abd T1 T2 T))
([[['inheritance M P] T1] [['inheritance M S] T2] [['equivalence ['inheritance X S] ['inheritance X P]] T]]
(noto= S P) (f-com T1 T2 T))
([[['inheritance P M] T1] [['inheritance S M] T2] [['equivalence ['inheritance P X] ['inheritance S X]] T]]
(noto= S P) (f-com T1 T2 T))
([[['inheritance M P] T1] [['inheritance M S] T2] [['conjunction [['inheritance ['var Y []] S] ['inheritance ['var Y []] P]]] T]]
(noto= S P) (f-int T1 T2 T))
([[['inheritance P M] T1] [['inheritance S M] T2] [['conjunction [['inheritance S ['var Y []]] ['inheritance P ['var Y []]]]] T]]
(noto= S P) (f-int T1 T2 T))
; 2nd variable introduction
([[['implication A ['inheritance M1 P]] T1] [['inheritance M2 S] T2] [['implication ['conjunction [A ['inheritance X S]]] ['inheritance X P]] T]]
(noto= S P) (= M1 M2) (noto= A ['inheritance M2 S]) (f-ind T1 T2 T))
([[['implication A ['inheritance M1 P]] T1] [['inheritance M2 S] T2] [['conjunction [['implication A ['inheritance ['var Y []] P]] ['inheritance ['var Y []] S]]] T]]
(noto= S P) (= M1 M2) (noto= A ['inheritance M2 S]) (f-int T1 T2 T))
([[['conjunction L1] T1] [['inheritance M S] T2] [['implication ['inheritance Y S] ['conjunction [['inheritance Y P2] . L3]]] T]]
(fresh [P L2]
(subtracto L1 [['inheritance M P]] L2) (noto= L1 L2)
(noto= S P) (dependento P Y P2) (dependento L2 Y L3) (f-ind T1 T2 T)))
([[['conjunction L1] T1] [['inheritance M S] T2] [['conjunction [['inheritance ['var Y []] S] ['inheritance ['var Y []] P] . L2]] T]]
(subtracto L1 [['inheritance M P]] L2) (noto= L1 L2) (noto= S P) (f-int T1 T2 T))
([[['implication A ['inheritance P M1]] T1] [['inheritance S M2] T2] [['implication ['conjunction [A ['inheritance P X]]] ['inheritance S X]] T]]
(noto= S P) (= M1 M2) (noto= A ['inheritance S M2]) (f-abd T1 T2 T))
([[['implication A ['inheritance P M1]] T1] [['inheritance S M2] T2] [['conjunction [['implication A ['inheritance P ['var Y []]]] ['inheritance S ['var Y []]]]] T]]
(noto= S P) (= M1 M2) (noto= A ['inheritance S M2]) (f-int T1 T2 T))
([[['conjunction L1] T1] [['inheritance S M] T2] [['implication ['inheritance S Y] ['conjunction [['inheritance P2 Y] . L3]]] T]]
(fresh [P L2]
(subtracto L1 [['inheritance P M]] L2) (noto= L1 L2) (noto= S P)
(dependento P Y P2) (dependento L2 Y L3) (f-abd T1 T2 T)))
([[['conjunction L1] T1] [['inheritance S M] T2] [['conjunction [['inheritance S ['var Y []]] ['inheritance P ['var Y []]] . L2]] T]]
(subtracto L1 [['inheritance P M]] L2) (noto= L1 L2) (noto= S P) (f-int T1 T2 T))
; dependent variable elimination
([[['conjunction L1] T1] [['inheritance M S] T2] [C T]]
(fresh [N D L2 L3 T0]
(subtracto L1 [['inheritance ['var N D] S]] L2) (project [L1 L2] (!= L1 L2))
(replace-var L2 ['var N D] L3 M) (reduceo ['conjunction L3] C)
(f-cnv T2 T0) (f-ana T1 T0 T)))
([[['conjunction L1] T1] [['inheritance S M] T2] [C T]]
(fresh [N D L2 L3 T0]
(subtracto L1 [['inheritance S ['var N D]]] L2) (project [L1 L2] (!= L1 L2))
(replace-var L2 ['var N D] L3 M) (reduceo ['conjunction L3] C)
(f-cnv T2 T0) (f-ana T1 T0 T))))
(defn choice [[f1 c1] [f2 c2]]
(if (>= c1 c2) [f1 c1] [f2 c2]))
(defn inference
([A1 A2] (inference2 A1 A2))
([A1 A2 A3] (inference3 A1 A2 A3)))
[rules {:keys [task-type] :as task} belief]
(generate-conclusions (rules task-type) task belief))
(defna replace-var [A1 A2 A3 A4]
([[] _ [] _])
([[['inheritance S1 P] . T1] S1 [['inheritance S2 P] . T2] S2]
(replace-var T1 S1 T2 S2))
([[['inheritance S P1] . T1] P1 [['inheritance S P2] . T2] P2]
(replace-var T1 P1 T2 P2)))
(def revision t/revision)
(defne replace-all [A1 A2 A3 A4]
([[H . T1] H1 [H . T2] H2]
(replace-var T1 H1 T2 H2)))
;===============================================================================
;inheritance
(defna inheritance [A1 A2]
([['ext-intersection Ls] P] (includeo [P] Ls))
([S ['int-intersection Lp]] (includeo [S] Lp))
([['ext-intersection S] ['ext-intersection P]] (includeo P S) (!= P [(lvar)]))
([['int-intersection S] ['int-intersection P]] (includeo S P) (!= S [(lvar)]))
([['ext-set S] ['ext-set P]] (includeo S P))
([['int-set S] ['int-set P]] (includeo P S))
([['ext-difference S P] S] (nonlvaro S) (nonlvaro P))
([S ['int-difference S P]] (nonlvaro S) (nonlvaro P))
([['product L1] R]
(fresh [L2]
(nonlvaro L1) (membero ['ext-image R L2] L1)
(replaceo L1 ['ext-image R L2] L2)))
([R ['product L1]]
(fresh [L2]
(nonlvaro L1) (membero ['int-image R L2] L1)
(replaceo L1 ['int-image R L2] L2))))
;===============================================================================
;similarity
; There is a green cut in prolog's version, the second version of similarity
; implements it.
(defna similarity [A1 A2]
([X Y] (all (nonlvaro X) (reduceo X Y) (!= X Y)))
([['ext-intersection L1] ['ext-intersection L2]] (same-seto L1 L2))
([['int-intersection L1] ['int-intersection L2]] (same-seto L1 L2))
([['ext-set L1] ['ext-set L2]] (same-seto L1 L2))
([['int-set L1] ['int-set L2]] (same-seto L1 L2)))
;===============================================================================
;implication
(defna implication [A1 A2]
([['similarity S P] ['inheritance S P]])
([['equivalence S P] ['implication S P]])
([['conjunction L] M] (nonlvaro L) (membero M L))
([M ['disjunction L]] (nonlvaro L) (membero M L))
([['conjunction L1] ['conjunction L2]]
(nonlvaro L1) (nonlvaro L2) (subseto L2 L1))
([['disjunction L1] ['disjunction L2]]
(nonlvaro L1) (nonlvaro L2) (subseto L1 L2))
([['inheritance S P]
['inheritance ['ext-intersection Ls] ['ext-intersection Lp]]]
(fresh [L] (nonlvaro Ls) (nonlvaro Lp) (replaceo Ls S L P) (sameo L Lp)))
([['inheritance S P]
['inheritance ['int-intersection Ls] ['int-intersection Lp]]]
(fresh [L] (nonlvaro Ls) (nonlvaro Lp) (replaceo Ls S L P) (sameo L Lp)))
([['similarity S P]
['similarity ['ext-intersection Ls] ['ext-intersection Lp]]]
(fresh [L] (nonlvaro Ls) (nonlvaro Lp) (replaceo Ls S L P) (sameo L Lp)))
([['similarity S P]
['similarity ['int-intersection Ls] ['int-intersection Lp]]]
(fresh [L] (nonlvaro Ls) (nonlvaro Lp) (replaceo Ls S L P) (sameo L Lp)))
([['inheritance S P]
['inheritance ['ext-difference S M] ['ext-difference P M]]] (nonlvaro M))
([['inheritance S P]
['inheritance ['int-difference S M] ['int-difference P M]]] (nonlvaro M))
([['similarity S P] ['similarity ['ext-difference S M] ['ext-difference P M]]]
(nonlvaro M))
([['similarity S P] ['similarity ['int-difference S M] ['int-difference P M]]]
(nonlvaro M))
([['inheritance S P] ['inheritance ['ext-difference M P] ['ext-difference M S]]]
(nonlvaro M))
([['inheritance S P] ['inheritance ['int-difference M P] ['int-difference M S]]]
(nonlvaro M))
([['similarity S P] ['similarity ['ext-difference M P] ['ext-difference M S]]]
(nonlvaro M))
([['similarity S P] ['similarity ['int-difference M P] ['int-difference M S]]]
(nonlvaro M))
([['inheritance S P] ['negation ['inheritance S ['ext-difference M P]]]]
(nonlvaro M))
([['inheritance S ['ext-difference M P]] ['negation ['inheritance S P]]]
(nonlvaro M))
([['inheritance S P] ['negation ['inheritance ['int-difference M S] P]]]
(nonlvaro M))
([['inheritance ['int-difference M S] P] ['negation ['inheritance S P]]]
(nonlvaro M))
([['inheritance S P] ['inheritance ['ext-image S M] ['ext-image P M]]]
(nonlvaro M))
([['inheritance S P] ['inheritance ['int-image S M] ['int-image P M]]]
(nonlvaro M))
([['inheritance S P] [inheritance ['ext-image M Lp] ['ext-image M Ls]]]
(fresh [L1 L2 x1 x2] (nonlvaro Ls) (nonlvaro Lp)
(conso S L2 x1) (appendo L1 x1 Ls)
(conso P L2 x2) (appendo L1 x2 Lp)))
([['inheritance S P] [inheritance ['int-image M Lp] ['int-image M Ls]]]
(fresh [L1 L2 x1 x2]
(nonlvaro Ls) (nonlvaro Lp)
(conso S L2 x1) (appendo L1 x1 Ls)
(conso P L2 x2) (appendo L1 x2 Lp)))
([['negation M] ['negation ['conjunction L]]] (includeo [M] L))
([['negation ['disjunction L]] ['negation M]] (includeo [M] L))
([['implication S P] ['implication ['conjunction Ls] ['conjunction Lp]]]
(fresh [L] (nonlvaro Ls) (nonlvaro Lp) (replaceo Ls S L P) (sameo L Lp)))
([['implication S P] ['implication ['disjunction Ls] ['disjunction Lp]]]
(fresh [L] (nonlvaro Ls) (nonlvaro Lp) (replaceo Ls S L P) (sameo L Lp)))
([['equivalence S P] ['equivalence ['conjunction Ls] ['conjunction Lp]]]
(fresh [L] (nonlvaro Ls) (nonlvaro Lp) (replaceo Ls S L P) (sameo L Lp)))
([['equivalence S P] ['equivalence ['disjunction Ls] ['disjunction Lp]]]
(fresh [L] (nonlvaro Ls) (nonlvaro Lp) (replaceo Ls S L P) (sameo L Lp))))
;===============================================================================
;equialence
(defna equivalence [A1 A2]
([X Y] (all (nonlvaro X) (reduceo X Y) (!= X Y)))
([['similarity S P] ['similarity P S]])
([['inheritance S ['ext-set [P]]] ['similarity S ['ext-set [P]]]])
([['inheritance ['int-set [S]] P] ['similarity ['int-set [S]] P]])
([['inheritance S ['ext-intersection Lp]] ['conjunction L]]
(fresh [P] (findallo ['inheritance S P] (membero P Lp) L)))
([['inheritance ['int-intersection Ls] P] ['conjunction L]]
(fresh [S] (findallo ['inheritance S P] (membero S Ls) L)))
([['inheritance S ['ext-difference P1 P2]]
['conjunction [['inheritance S P1] ['negation ['inheritance S P2]]]]])
([['inheritance ['int-difference S1 S2] P]
['conjunction [['inheritance S1 P] ['negation ['inheritance S2 P]]]]])
([['inheritance ['product Ls] ['product Lp]] ['conjunction L]]
(equ-producto Ls Lp L))
([['inheritance ['product [S . L]] ['product [P . L]]] ['inheritance S P]]
(nonlvaro L))
([['inheritance S P] ['inheritance ['product [H . Ls]] ['product [H . Lp]]]]
(nonlvaro H)
(equivalence ['inheritance ['product Ls] ['product Lp]] ['inheritance S P]))
([['inheritance ['product L] R] ['inheritance T ['ext-image R L1]]]
(replaceo L T L1))
([['inheritance R ['product L]] ['inheritance ['int-image R L1] T]]
(replaceo L T L1))
([['equivalence S P] ['equivalence P S]])
([['equivalence ['negation S] P] ['equivalence ['negation P] S]])
([['conjunction L1] ['conjunction L2]] (same-seto L1 L2))
([['disjunction L1] ['disjunction L2]] (same-seto L1 L2))
([['implication S ['conjunction Lp]] ['conjunction L]]
(fresh [P] (findallo ['implication S P] (membero P Lp) L)))
([['implication ['disjunction Ls] P] ['conjunction L]]
(fresh [S] (findallo ['implication S P] (membero S Ls) L)))
([T1 T2]
(noto (atomo T1)) (noto (atomo T2)) (nonlvaro T1) (nonlvaro T2)
(fresh [L1 L2] (== T1 L1) (== T2 L2) (equivalence-list L1 L2))))
(defna equivalence-list [A1 A2]
([L L])
([[H . L1] [H . L2]] (equivalence-list L1 L2))
([[H1 . L1] [H2 . L2]] (similarity H1 H2) (equivalence-list L1 L2))
([[H1 . L1] [H2 . L2]] (equivalence H1 H2) (equivalence-list L1 L2)))
;===============================================================================
;compound term structure reduction
(defna reduceo [A1 A2]
([['similarity ['ext-set [S]] ['ext-set [P]]] ['similarity S P]])
([['similarity ['int-set [S]] ['int-set [P]]] ['similarity S P]])
([['instance S P] ['inheritance ['ext-set [S]] P]])
([['property S P] ['inheritance S ['int-set [P]]]])
([['inst-prop S P] ['inheritance ['ext-set [S]] ['int-set [P]]]])
([['ext-intersection [T]] T])
([['int-intersection [T]] T])
([['ext-intersection [['ext-intersection L1] ['ext-intersection L2]]] ['ext-intersection L]]
(uniono L1 L2 L))
([['ext-intersection [['ext-intersection L1] L2]] ['ext-intersection L]]
(uniono L1 [L2] L))
([['ext-intersection [L1 ['ext-intersection L2]]] ['ext-intersection L]]
(uniono [L1] L2 L))
([['ext-intersection [['ext-set L1] ['ext-set L2]]] ['ext-set L]]
(intersectiono L1 L2 L))
([['ext-intersection [['int-set L1] ['int-set L2]]] ['int-set L]]
(uniono L1 L2 L))
([['int-intersection [['int-intersection L1] ['int-intersection L2]]] ['int-intersection L]]
(uniono L1 L2 L))
([['int-intersection [['int-intersection L1] L2]] ['int-intersection L]]
(uniono L1 [L2] L))
([['int-intersection [L1 ['int-intersection L2]]] ['int-intersection L]]
(uniono [L1] L2 L))
([['int-intersection [['int-set L1] ['int-set L2]]] ['int-set L]]
(intersectiono L1 L2 L))
([['int-intersection [['ext-set L1] ['ext-set L2]]] ['ext-set L]]
(uniono L1 L2 L))
([['ext-difference ['ext-set L1] ['ext-set L2]] ['ext-set L]]
(subtracto L1 L2 L))
([['int-difference ['int-set L1] ['int-set L2]] ['int-set L]]
(subtracto L1 L2 L))
([['product ['product L] T] ['product L1]]
(appendo L [T] L1))
([['ext-image ['product L1] L2] T1]
(membero T1 L1) (replaceo L1 T1 L2))
([['int-image ['product L1] L2] T1]
(membero T1 L1) (replaceo L1 T1 L2))
([['negation ['negation S]] S])
([['conjunction [T]] T])
([['disjunction [T]] T])
([['conjunction [['conjunction L1] ['conjunction L2]]] ['conjunction L]]
(uniono L1 L2 L))
([['conjunction [['conjunction L1] L2]] ['conjunction L]]
(uniono L1 [L2] L))
([['conjunction [L1 ['conjunction L2]]] ['conjunction L]]
(uniono [L1] L2 L))
([['disjunction ['disjunction L1] ['disjunction L2]] ['disjunction L]]
(uniono L1 L2 L))
([['disjunction ['disjunction L1] L2] ['disjunction L]]
(uniono L1 [L2] L))
([['disjunction L1 ['disjunction L2]] ['disjunction L]]
(uniono [L1] L2 L))
([X X]))
(comment
:shift-occurrence-forward ;pre
:shift-occurrence-backward ;pre
:linkage-temporal)
-61
View File
@@ -1,61 +0,0 @@
(ns nal.cut
(:refer-clojure :exclude [== reduce replace])
(:require [clojure.core.logic :refer
[run* project fresh conde conso run membero defne
nonlvaro == emptyo succeed fail conda s# u# onceo defna] :as l]
[clojure.core.logic.pldb :refer [db-rel db with-db]]))
;https://en.wikibooks.org/wiki/Prolog/Cuts_and_Negation
;===============================================================================
;(db-rel b p)
;(db-rel c p)
;
;(defn a [X Y]
; (all (onceo (b X)) (c Y)))
;
;(with-db
; (db [b 'k]
; [b 1]
; [c 1]
; [c 2]
; [c 3])
; (run 10 [X Y]
; (a X Y)))
;
; is equal to
;
; a(X, Y) :- b(X), !, c(Y).
; b(1).
; b(2).
; b(3).
;
; c(1).
; c(2).
; c(3).
;
;===============================================================================
;(run* [x y]
; (conda
; [(== 2 x) u#]
; [(== 1 y)]))
;
; is equal to
;
; k(X, _) :- X = 2, !, fail.
; k(_, Y) :- Y = 1.
;
;===============================================================================
;(run* [x y]
; (conda
; [(== 2 x)]
; [(== 1 y)]))
;
; is equal to
;
; k(X, _) :- X = 2, !.
; k(_, Y) :- Y = 1.
;
; == acts as =/2
; = acts as ==/2
; != acts as \==/2
; noto= acts as \=/2
+23
View File
@@ -0,0 +1,23 @@
(ns nal.deriver
(:require
[nal.deriver.utils :refer [walk]]
[nal.deriver.key-path :refer [mall-paths all-paths mpath-invariants
path-with-max-level]]
[nal.deriver.rules :refer [rule]]))
(defn get-matcher [rules p1 p2]
(let [matchers (->> (mall-paths p1 p2)
(filter rules)
(map rules)
(map (fn [el] (:matcher el))))]
(case (count matchers)
0 (constantly [])
1 (first matchers)
(fn [t1 t2] (mapcat #(% t1 t2) matchers)))))
(def mget-matcher (memoize get-matcher))
(def mpath (memoize path-with-max-level))
(defn generate-conclusions
[rules {p1 :statement :as t1} {p2 :statement :as t2}]
(let [matcher (mget-matcher rules (mpath p1) (mpath p2))]
(matcher t1 t2)))
+50
View File
@@ -0,0 +1,50 @@
(ns nal.deriver.backward-rules
(:require [nal.deriver.key-path :refer [rule-path]]
[clojure.string :as s]))
;http://pastebin.com/3zLX7rPx
(defn allow-backward?
"Return true if rule allows backward inference."
[{:keys [conclusions]}]
(some #{:allow-backward} (:post (first conclusions))))
(defn has-prefix? [prefix cond]
(when (keyword? cond)
(s/starts-with? (str cond) prefix)))
(defn not-equal? [cond]
(when (coll? cond)
(= :!= (first cond))))
(defn check-not-equal [pre conclusion]
(if (some not-equal? pre)
(let [pre (remove not-equal? pre)]
(if (and (coll? conclusion) (= 3 (count conclusion)))
(let [[_ t1 t2] conclusion]
(conj pre (list :!= t1 t2)))
pre))
pre))
(defn expand-backward-rules
"If rule allows backward inference it will be expanded to three rules,
where first one is the rule itself, and rest rules will be generated by
swapping conclusion with every premise."
[{:keys [p1 p2 conclusions pre] :as rule}]
(mapcat (fn [{:keys [conclusion post] :as fc}]
(let [post (reduce #(remove (partial has-prefix? %2) %1)
post [":t/" ":d/"])]
(conj (map
(fn [r] (update r :pre conj :question?))
[(assoc rule :conclusions [(assoc fc :post post)])
(assoc rule :p1 conclusion
:conclusions [{:conclusion p1
:post post}]
:full-path (rule-path conclusion p2)
:pre (check-not-equal pre p1))
(assoc rule :p2 conclusion
:conclusions [{:conclusion p2
:post post}]
:full-path (rule-path p1 conclusion)
:pre (check-not-equal pre p2))])
rule)))
conclusions))
+58
View File
@@ -0,0 +1,58 @@
(ns nal.deriver.key-path)
(defn path
"Generates premises \"path\" by replacing terms with :any"
[statement]
(if (coll? statement)
(let [[fst & tail] statement]
(conj (map path tail) fst))
:any))
(defn path-with-max-level
([statement] (path-with-max-level 0 statement))
([level statement]
(if (coll? statement)
(let [[fst & tail] statement]
(cons fst
(if (> 1 level)
(let [next-level (if (= 'conj fst) level (inc level))]
(map #(path-with-max-level next-level %) tail))
(repeat (count tail) :any))))
:any)))
(defn rule-path
"Generates detailed pattern for the rule."
[p1 p2]
[(path p1) :and (path p2)])
(defn cart
"Cartesian product."
[colls]
(if (empty? colls)
'(())
(for [x (first colls)
more (cart (rest colls))]
(cons x more))))
(declare mpath-invariants)
(defn path-invariants
"Generates all pathes that will match with path from args."
[path]
(if (coll? path)
(let [[op & args] path
args-inv (map mpath-invariants args)]
(concat (cart (concat [[op]] args-inv)) [:any]))
[path]))
(def mpath-invariants (memoize path-invariants))
;(all-paths 'Y '(==> (seq-conj X A1 A2 A3) B))
(defn all-paths
"Generates all pathes for pair of premises."
[p1 p2]
(let [paths1 (mpath-invariants p1)
paths2 (mpath-invariants p2)]
(cart [paths1 [:and] paths2])))
(def mall-paths (memoize all-paths))
+59
View File
@@ -0,0 +1,59 @@
(ns nal.deriver.list-expansion
(:require [nal.deriver.utils :refer [walk]]
[clojure.string :as s]))
(def max-elements-in-list 7)
(defn get-list
"Finds the first :list element in the rule."
[prefix statement]
(cond
(and (keyword? statement) (s/starts-with? (str statement) prefix))
statement
(coll? statement) (some identity (map #(get-list prefix %) statement))
:default nil))
(defn gen-symbols
"Generates n symbols with prefix."
[prefix n]
(map #(symbol (str prefix (inc %))) (range n)))
(defn replace-list-elemets
"Replaces :list/ with symbols."
[statement l list-name n]
(walk statement
(and (coll? el) (some #{l} el))
(mapcat (fn [e] (if (= l e)
(concat '() (gen-symbols list-name n))
(list e))) el)))
(defn list-name
"Fetches name of the list."
[l]
(->> l str (drop 6) s/join))
(defn expand-:from-element
"Expands :from/ element in rule, so as a result will be created n rules, in
each of them :form/A will be replaced with A1 or A2 or A3, etc."
[statement from-name list-name n]
(map (fn [idx]
(walk statement
(= from-name el) (symbol (str list-name idx))))
(range 1 (inc n))))
(defn generate-all-lists
"Expands rules with :list/ elements, as a result will be created 5 rules,
where :list/ will be replaced with A1, A1 A2, ..., A1..A5."
[r]
(let [list (get-list ":list" r)
l-name (list-name list)]
(mapcat #(let [st (replace-list-elemets r list l-name %)]
(if-let [from-name (get-list ":from" st)]
(expand-:from-element st from-name l-name %)
[st]))
(range 1 (inc max-elements-in-list)))))
(defn contains-list?
"Checks if rule contains any :list element."
[r]
(get-list ":list" r))
+345
View File
@@ -0,0 +1,345 @@
(ns nal.deriver.matching
(:require
[nal.deriver.utils :refer [walk operator? not-operator?]]
[clojure.core.unify :as u]
[clojure.set :refer [map-invert intersection]]
[clojure.string :as s]
[nal.deriver
[set-functions :refer [f-map not-empty-diff? not-empty-inter?]]
[substitution :refer [munification-map substitute]]
[preconditions :refer [sets compound-precondition conclusion-transformation
implications-and-equivalences get-terms abs
preconditions-transformations]]
[normalization :refer [commutative-ops sort-commutative reducible-ops]
:as n]
[truth :as t]]))
;operators/functions that shouldn't be quoted
(def reserved-operators
#{`= `not= `seq? `first `and `let `pos? `> `>= `< `<= `coll? `set `quote
`count 'aops `- `not-empty-diff? `not-empty-inter? `walk `munification-map
`substitute `sets `some `deref `do `vreset! `volatile! `fn `mapv `if
`sort-commutative `n/reduce-ext-inter `n/reduce-symilarity `complement
`n/reduce-int-dif `n/reduce-and `n/reduce-ext-dif `n/reduce-image
`n/reduce-int-inter `n/reduce-neg `n/reduce-or `nil? `not `or `abs
`implications-and-equivalences `get-terms `empty? `intersection
`n/reduce-seq-conj `clojure.core.match/match})
(defn operators->placeholders
[statement]
(walk statement
(and (symbol? :el)
(operator? :el)) '_
(= :interval :el) '_
(coll? :el) (vec :el)))
(defn quote-operators
[statement]
(walk statement
(and
(not (reserved-operators :el))
(symbol? :el)
(or (operator? :el) (#{'Y 'X} :el))) `'~:el
(and (coll? :el)
((complement map?) :el)
(let [f (first :el)]
(and (not (reserved-operators f))
(not (fn? f)))))
(vec :el)))
(defn form-conclusion
"Formation of cocnlusion in terms of task and truth/desire functions"
[{:keys [t1 t2 task-type]}
{c :statement tf :t-function pj :p/judgement df :d-function
sc :shift-conditions}]
(let [conclusion-type (if pj :judgement task-type)
conclusion {:statement c
:task-type conclusion-type
:occurrence :t-occurrence}
conclusion (case conclusion-type
:judgement (assoc conclusion :truth (list tf t1 t2))
:goal (assoc conclusion :desire (list df t1 t2))
conclusion)]
(if sc
(conclusion-transformation sc conclusion)
conclusion)))
(defn traverse-node
"Generates code for precondition node."
[vars result {:keys [conclusions children condition]}]
`(when ~(quote-operators condition)
~(when-not (zero? (count conclusions))
`(vswap! ~result concat
~@(set (map #(mapv (partial form-conclusion vars) %)
(quote-operators conclusions)))))
~@(map (fn [n] (traverse-node vars result n)) children)))
(defn traversal
"Walk through preconditions tree and generates code for matcher."
[vars tree]
(let [results (gensym)]
`(let [~results (volatile! [])]
~(traverse-node vars results tree)
@~results)))
(defn replace-occurrences
"Reblaces occurrences keywords from matcher's code by generated symbols."
[code]
(let [t-occurrence (gensym) b-occurrence (gensym)]
(walk code
(= :el :t-occurrence) t-occurrence
(= :el :b-occurrence) b-occurrence)))
(defn match-rules
"Generates code of function that will match premises. Generated function
should be called with task and beleif as arguments."
[rules pattern task-type]
(let [t1 (gensym) t2 (gensym)
task (gensym) belief (gensym)
truth-kw (if (= :goal task-type) :desire :truth)]
(replace-occurrences
`(fn [{p1# :statement ~t1 ~truth-kw :t-occurrence :occurrence :as ~task}
{p2# :statement ~t2 :truth :b-occurrence :occurrence :as ~belief}]
(let [~(operators->placeholders (first pattern)) p1#
~(operators->placeholders (second pattern)) p2#]
~(traversal {:t1 t1
:t2 t2
:task task
:belief belief
:task-type task-type}
rules))))))
(defn find-and-replace-symbols
"Replaces all terms in statemnt to placeholders that will be used in pattern
matching or unification. Return vector, where the first element is map from
placeolder to term and the second is statement with replaced terms.
Form instance:
(find-and-replace-symbols '[--> [- A B] C] \"x\")
;[{x0 A, x1 B, x2 C} [(quote -->) [(quote -) x0 x1] x2]]
"
[statement prefix]
(let [cnt (volatile! 0)
sym-map (volatile! {})
get-sym #(symbol (str prefix %))
result (walk statement
(and (symbol? el) (not-operator? el))
(let [s (get-sym @cnt)]
(vswap! cnt inc)
(vswap! sym-map assoc s el)
s))]
[@sym-map result]))
(defn symbols->placeholders
"Replaces "
[premise]
(second (find-and-replace-symbols premise "x")))
(defn symbol-ordering-keyfn
[sym]
(if (symbol? sym)
(try (Integer/parseInt (s/join (drop 1 (str sym))))
(catch Exception _ -100))
-1))
(defn sort-placeholders
"Sorts placeholder for preconditions. If we have two precondition like
(!= x0 x2) and (!= x2 x0) they are equal but we can not check this easely.
So this function sort [x2 x0] to [x0 x2], so we can reason that
(!= x0 x2) and (!= x2 x0) are equal."
[tail]
(sort-by symbol-ordering-keyfn tail))
(defn apply-preconditions
"Generates code for preconditions."
[preconditions]
(reduce (fn [ac condition]
(if (seq? condition)
(concat ac (compound-precondition condition))
ac))
[] preconditions))
(defn replace-symbols
"Replaces elements from statement if finds them in sym-map."
[conclusion sym-map]
(let [sym-map (map-invert sym-map)]
(walk conclusion
(sym-map el) (sym-map el))))
(defn find-kv-by-prefix [prefix coll]
(first (filter #(and (keyword? %) (s/starts-with? (str %) prefix)) coll)))
(defn get-truth-fn [post] (find-kv-by-prefix ":t/" post))
(defn get-desire-fn [post] (find-kv-by-prefix ":d/" post))
(defn get-aliases
"Filter map of symbols and keep only aliases of symbol that have lower
order-key (to avoid preconditions with swapped symbols, like (= x1 x2)
(= x2 x1)."
[symbols-map alias]
(let [sym (symbols-map alias)]
(->> (dissoc symbols-map alias)
(filter (fn [[a v]]
(and (< (symbol-ordering-keyfn alias)
(symbol-ordering-keyfn a))
(= v sym))))
keys)))
(defn aliases->conditins
[symbols-map alias]
(mapcat #(list `= alias %) (get-aliases symbols-map alias)))
(defn check-conditions [syms]
(->> (keys syms)
(keep (partial aliases->conditins syms))
(filter not-empty)))
(defn commutative? [st]
(and (coll? st) (some commutative-ops st)))
(defn check-commutative [conclusion]
(if (commutative? conclusion)
`(sort-commutative ~(sort-commutative conclusion))
conclusion))
(defn check-reduction [conclusion]
(walk conclusion
(and (coll? :el) (reducible-ops (first :el)) (<= 3 (count :el)))
`(~(reducible-ops (first :el)) ~:el)))
(defn find-shift-precondition
[preconditions]
(first
(filter
#(and (coll? %)
(#{:shift-occurrence-backward
:shift-occurrence-forward}
(first %))) preconditions)))
(defn premises-pattern
"Creates map with preconditions and conclusions regarding to the main pattern
of rules branch.
Example:
the main pattern of riles branch is
[[--> :any [- :any :any]] :and [--> :any :any]]
before it will be applied to pattern matching it will be transformed to
[[--> x1 [- x2 x3]] [--> x4 x5]]
When whe want to use the pattern above to derive conclusions from some
another rule, we have to map placeholders from pattern to terms in this rule.
For rule with premises [[--> A B] [--> B C]] and conclusion [--> A C]
map will be
{x1 A, ['- x2 x3] B, x4 B, x5 C}
From this map, conditions will be the list with one condition:
[(= ['- x2 x3] x4)]
and conclusion will be
[--> x1 x4]."
[pattern premise {:keys [post conclusion]} preconditions]
(let [[sym-map pat] (find-and-replace-symbols premise "?a")
unification-map (u/unify pattern pat)
sym-map (into {} (map (fn [[k v]] [(k unification-map) v]) sym-map))
inverted-sym-map (map-invert sym-map)
pre (walk (apply-preconditions preconditions)
(inverted-sym-map el) (inverted-sym-map el)
(seq? el)
(let [[f & tail] el]
(if-not (#{`munification-map `not-empty-diff?} f)
(concat (list f) (sort-placeholders tail))
el)))]
{:conclusion {:statement (-> conclusion
(preconditions-transformations preconditions)
(replace-symbols sym-map)
check-commutative
check-reduction)
:shift-conditions (replace-symbols
(find-shift-precondition preconditions)
sym-map)
:t-function (t/tvtypes (get-truth-fn post))
:d-function (t/dvtypes (get-desire-fn post))
:p/judgement (some #{:p/judgement} post)}
:conditions (remove nil?
(walk (concat (check-conditions sym-map) pre)
(and (coll? el) (= \a (first (str (first el)))))
(concat '() el)
(and (coll? el) (not ((conj reserved-operators 'quote)
(first el))))
(vec el)))}))
(defn conditions->conclusions-map
"Creates map from conditions to conclusions."
[main rules]
(->> rules
(map (fn [[premises conclusions preconditions]]
(premises-pattern main premises conclusions preconditions)))
(group-by :conditions)
(map (fn [[k v]] [k (map :conclusion v)]))))
(defrecord TreeNode [condition conclusions children])
(defn group-conditions
"Groups conditions->conclusions map by first condition and remove it."
[conds]
(into {} (map (fn [[k v]]
[k (map (fn [[k v]]
[(drop 1 k) (set v)]) v)])
(group-by #(-> % first first) conds))))
(defn generate-tree
"Generates tree of conditions from conditions->conclusions map."
([conds] (generate-tree true conds))
([cond conds]
(let [grouped-conditions (group-conditions conds)
reached-keys (map second (grouped-conditions nil))
other (dissoc grouped-conditions nil)]
(->TreeNode cond reached-keys
(map (fn [[cond conds]]
(generate-tree cond conds))
other)))))
(defn conds-priorities-map
"Generates the map of priorities for coditions according to their frequency."
[conds]
(->> (mapcat (fn [[cnds k]]
(if (not-empty cnds)
(map (fn [c] [c k]) cnds)
[(list '() k)])) conds)
(group-by first)
(map (fn [[k v]] [k (+ (- (count v)) (rand 0.4))]))
(into {})))
(defn sort-conds
"Sorts conditions in conditions->conclusions map according to the map of
priorities of conditions. If condition occurs frequntly it will have higher
priority."
[conds cpm]
(map (fn [[cnds k]] [(sort-by cpm cnds) k]) conds))
(defn gen-rules
"Prepeares data for generation of conditions tree and then generates tree."
[main-pattern rules]
(let [rules (mapcat (fn [{:keys [p1 p2 conclusions pre]}]
(map #(vector [p1 p2] % pre) conclusions))
rules)
cond-conclusions-m (conditions->conclusions-map main-pattern rules)
cpm (conds-priorities-map cond-conclusions-m)
sorted-conds (sort-conds cond-conclusions-m cpm)]
(generate-tree sorted-conds)))
(defn generate-matching
"Generates code for rule matcher."
[rules task-type]
(->> rules
(map (fn [[k {:keys [pattern rules] :as v}]]
(let [main-pattern (symbols->placeholders pattern)
match-fn-code (-> main-pattern
(gen-rules rules)
(match-rules main-pattern task-type))]
[k (assoc v :matcher (eval match-fn-code)
:matcher-code match-fn-code)])))
(into {})))
+196
View File
@@ -0,0 +1,196 @@
(ns nal.deriver.normalization
(:require [clojure.string :as s]
[clojure.set :as set]
[clojure.core.match :as m]))
;operators that should be movet from infix to prefix position
(def operators
#{'&| '--> '<-> '==> 'retro-impl 'pred-impl 'seq-conj 'inst 'prop 'inst-prop
'int-image 'ext-image '=/> '=|> '| '<=> '</> '<|> '- 'int-dif '|| '&&
'ext-inter 'conj})
;commutative: <-> <=> <|> & | && ||
;not commutative: --> ==> =/> =\> </> &/ - ~
(def commutative-ops #{'<-> '<=> '<|> '| '|| 'conj 'ext-inter})
(defn infix->prefix
"Makes transformaitions like [A --> B] to [--> A B]."
[premise]
(if (coll? premise)
(let [[f s & tail] premise]
(map infix->prefix
(if (operators s)
(concat [s f] tail)
premise)))
premise))
(defn- neg-symbol?
"Checks if symbol starts with --, true for --A, --X"
[el]
(and (not= el '-->) (symbol? el) (s/starts-with? (str el) "--")))
(defn- trim-negation
"Removes -- from symbols with negation:
(trim-negation '--A) => A"
[el]
(symbol (s/join (drop 2 (str el)))))
(defn neg [el] (list '-- el))
(defn replace-negation
"Replaces negations's \"new notation\"."
[statement]
(cond
(neg-symbol? statement) (neg (trim-negation statement))
(or (vector? statement) (and (seq? statement) (not= '-- (first statement))))
(:st
(reduce
(fn [{:keys [prev st] :as ac} el]
(if (= '-- el)
(assoc ac :prev true)
(->> [(cond prev (neg el)
(coll? el) (replace-negation el)
(neg-symbol? el) (neg (trim-negation el))
:else el)]
(concat st)
(assoc ac :prev false :st))))
{:prev false :st '()}
statement))
:else statement))
(defn sort-commutative [conclusions]
(if (coll? conclusions)
(let [f (first conclusions)]
(if (commutative-ops f)
(vec (conj (sort-by hash (drop 1 conclusions)) f))
conclusions))
conclusions))
;https://gist.github.com/TonyLo1/a3f8e05458c5e90c2e72
(defn union
([c1 c2] (sort-by hash (set (concat c1 c2))))
([op c1 c2] (vec (conj (union c1 c2) op))))
(defn diff
([c1 c2] (into '() (set/difference (set c1) (set c2))))
([op c1 c2] (vec (conj (diff c1 c2) op))))
(defn reduce-ext-inter
[st]
(m/match
st
[_ t] t
[_ ['ext-inter & l1] ['ext-inter & l2]] (union 'ext-inter l1 l2)
[_ ['ext-inter & l1] l2] (union 'ext-inter l1 [l2])
[_ l1 ['ext-inter & l2]] (union 'ext-inter [l1] l2)
[_ ['int-set & l1] ['int-set & l2]] (union 'int-set l1 l2)
;[_ ['ext-set & l1] ['ext-set & l2]] (union 'ext-set l1 l2)
:else st))
(defn reduce-int-inter
[st]
(m/match st
['| t] t
['| ['| & l1] ['| & l2]] (union '| l1 l2)
['| ['| & l1] l2] (union '| l1 [l2])
['| l1 ['| & l2]] (union '| [l1] l2)
['| ['int-set & l1] ['int-set & l2]] (union 'int-set l1 l2)
['| ['ext-set & l1] ['ext-set & l2]] (union 'ext-set l1 l2)
:else st))
(defn reduce-int-dif
[st]
(m/match st
[_ ['int-set & l1] ['int-set & l2]] (diff 'int-set l1 l2)
:else st))
(defn reduce-ext-dif
[st]
(m/match st
[_ ['ext-set & l1] ['ext-set & l2]] (diff 'ext-set l1 l2)
:else st))
(defn reduce-symilarity
[st]
(m/match st
['<-> ['ext-set s] ['ext-set p]] ['<-> s p]
['<-> ['int-set s] ['int-set p]] ['<-> s p]
:else st))
(defn reduce-production
[st]
(m/match st
['* ['* & l1] & l2] (vec (conj (concat l1 l2) '*))
:else st))
(defn reduce-image
[st]
(m/match st
[_ ['* t1 t2] t3] (if (and (= t2 t3) (not= t1 t2)) t1 st)
:else st))
(defn reduce-neg
[st]
(m/match st
['-- ['-- t]] t
:else st))
(defn reduce-or
[st]
(m/match st
['|| t] t
['|| ['|| & l1] ['|| & l2]] (union '|| l1 l2)
['|| ['|| & l1] l2] (union '|| l1 [l2])
['|| l1 ['|| & l2]] (union '|| [l1] l2)
['|| t1 t2] (if (= t1 t2) t1 st)
:else st))
(defn reduce-and
[st]
(m/match st
['conj t] t
['conj ['conj & l1] ['conj & l2]] (union 'conj l1 l2)
['conj ['conj & l1] l2] (union 'conj l1 [l2])
['conj l1 ['conj & l2]] (union 'conj [l1] l2)
['conj t1 t2] (if (= t1 t2) t1 st)
:else st))
(defn reduce-seq-conj
[st]
(let [cnt (count st)]
(cond
(= 3 cnt) (second st)
(odd? cnt) (vec (butlast st))
:else st)))
(def reducible-ops
{'ext-inter `reduce-ext-inter
'| `reduce-int-inter
'- `reduce-ext-dif
'int-dif `reduce-int-dif
'<-> `reduce-symilarity
'* `reduce-production
'int-image `reduce-image
'ext-image `reduce-image
'-- `reduce-neg
'conj `reduce-and
'|| `reduce-or})
(defn reduce-ops
[st]
(let [f (first st)]
(case f
ext-inter (reduce-ext-inter st)
| (reduce-int-inter st)
- (reduce-ext-dif st)
int-dif (reduce-int-dif st)
<-> (reduce-symilarity st)
* (reduce-production st)
int-image (reduce-image st)
ext-image (reduce-image st)
-- (reduce-neg st)
conj (reduce-and st)
|| (reduce-or st)
st)))
+201
View File
@@ -0,0 +1,201 @@
(ns nal.deriver.preconditions
(:require [nal.deriver.set-functions
:refer [f-map not-empty-diff? not-empty-inter?]]
[nal.deriver.utils :refer [walk]]
[nal.deriver.substitution :refer [substitute munification-map]]
[nal.deriver.terms-permutation :refer [implications equivalences]]
[clojure.set :refer [union intersection]]
[narjure.defaults :refer [temporal-window-duration]]
[clojure.core.match :as m]
[nal.deriver.normalization :refer [reduce-seq-conj]]))
(defn abs [^long n] (Math/abs n))
;TODO preconditions
;:shift-occurrence-forward :shift-occurrence-backward
(defmulti compound-precondition
"Expands compound precondition to clojure sequence
that will be evaluted later"
first)
(defmethod compound-precondition :default [_] [])
(defmethod compound-precondition :!=
[[_ & args]]
[`(not= ~@args)])
(defn check-set [set-type arg]
`(and (coll? ~arg) (= ~set-type (first ~arg))))
(defmethod compound-precondition :set-ext? [[_ arg]]
[(check-set 'ext-set arg)])
(defmethod compound-precondition :set-int? [[_ arg]]
[(check-set 'int-set arg)])
(def sets '#{ext-set int-set})
(defn set-conditions [set1 set2]
[`(coll? ~set1)
`(coll? ~set2)
`(let [k# 1 afop# (first ~set1)]
(and (sets afop#) (= afop# (first ~set2))))])
(defmethod compound-precondition :difference [[_ arg1 arg2]]
(concat (set-conditions arg1 arg2)
[`(not-empty-diff? ~arg1 ~arg2)]))
(defmethod compound-precondition :union [[_ arg1 arg2]]
(set-conditions arg1 arg2))
(defmethod compound-precondition :intersection [[_ arg1 arg2]]
(concat (set-conditions arg1 arg2)
[`(not-empty-inter? ~arg1 ~arg2)]))
(defmethod compound-precondition :substitute-if-unifies
[[_ arg1 arg2 arg3]]
[`(munification-map ~arg1 ~arg2 ~arg3)])
(defmethod compound-precondition :contains?
[[_ arg1 arg2]]
[`(some (set [~arg2]) ~arg1)])
(def implications-and-equivalences
(union implications equivalences))
(defmethod compound-precondition :not-implication-or-equivalence
[[_ arg]]
[`(if (coll? ~arg)
(nil? (~`implications-and-equivalences (first ~arg)))
true)])
(defn get-terms
[st]
(if (coll? st)
(mapcat get-terms (rest st))
[st]))
(defmethod compound-precondition :no-common-subterm
[[_ arg1 arg2]]
[`(empty? (intersection (set (get-terms ~arg1))
(set (get-terms ~arg2))))])
(defmethod compound-precondition :not-set?
[[_ arg]]
[`(or (not (coll? ~arg)) (not (sets (first ~arg))))])
(defmethod compound-precondition :measure-time
[_]
[`(not= :eternal :t-occurrence)
`(not= :eternal :b-occurrence)
`(<= ~temporal-window-duration (abs (- :t-occurrence :b-occurrence)))])
(defmethod compound-precondition :concurrent
[_]
[`(> ~temporal-window-duration (abs (- :t-occurrence :b-occurrence)))])
;-------------------------------------------------------------------------------
(defmulti precondition-transformation (fn [arg1 _] (first arg1)))
(defmethod precondition-transformation :default [_ conclusion] conclusion)
(defn sets-transformation
[[cond-name el1 el2 el3] conclusion]
(walk conclusion (= :el el3)
`(~(f-map cond-name) ~el1 ~el2)))
(doall (map
#(defmethod precondition-transformation %
[cond concl] (sets-transformation cond concl))
[:difference :union :intersection]))
(defmethod precondition-transformation :substitute
[[_ el1 el2] conclusion]
`(walk ~conclusion
(= :el ~el1) ~el2))
(defmethod precondition-transformation :substitute-from-list
[[_ el1 el2] conclusion]
`(mapv (fn [k#]
(if (= k# ~el1)
k#
(walk k# (= :el ~el1) ~el2)))
~conclusion))
(defmethod precondition-transformation :substitute-if-unifies
[[_ p1 p2 p3] conclusion]
`(substitute ~p1 ~p2 ~p3 ~conclusion))
(defmethod precondition-transformation :measure-time
[[_ arg] conclusion]
(let [mt (gensym)]
(walk `(let [~arg (abs (- :t-occurrence :b-occurrence))]
~(walk conclusion
(= :el arg) [:interval arg]))
(= :el arg) mt)))
(defn check-precondition
[conclusion precondition]
(if (seq? precondition)
(precondition-transformation precondition conclusion)
conclusion))
(defn preconditions-transformations
"Some transformations of conclusion may be required by precondition."
[conclusion preconditions]
(reduce check-precondition conclusion preconditions))
;-------------------------------------------------------------------------------
(defmulti conclusion-transformation (fn [arg1 _] (first arg1)))
(defn shift-forward-let
([sym conclusion] (shift-forward-let sym nil conclusion nil))
([sym op conclusion duration]
`(let [interval# ~sym
~@(if duration
`[:t-occurrence (~op :t-occurrence ~duration)]
[])
:t-occurrence (if interval# (+ :t-occurrence interval#)
:t-occurrence)]
~conclusion)))
(defmethod conclusion-transformation :shift-occurrence-forward
[args concl]
(m/match (mapv #(if (and (coll? %) (= 'quote (first %)))
(second %) %) (rest args))
[(:or '=|> '==>)] concl
['pred-impl] `(let [:t-occurrence (+ :t-occurrence ~temporal-window-duration)] ~concl)
['retro-impl] `(let [:t-occurrence (- :t-occurrence ~temporal-window-duration)] ~concl)
[sym (:or '=|> '==>)] (shift-forward-let sym concl)
[sym 'pred-impl] (shift-forward-let sym `+ concl temporal-window-duration)
[sym 'retro-impl] (shift-forward-let sym `- concl temporal-window-duration)))
(defn backward-interval-check [sym]
`(and (coll? ~sym) (= (first ~sym) (quote ~'seq-conj))
(let [cnt# (count ~sym)]
(and (odd? cnt#) (get-in ~sym [(dec cnt#) 1])))))
(defn shift-backward-let
([sym conclusion] (shift-backward-let sym nil conclusion nil))
([sym op conclusion duration]
`(let [interval# ~(backward-interval-check sym)
~@(if duration
`[:t-occurrence (~op :t-occurrence ~duration)]
[])
:t-occurrence (if interval# (+ :t-occurrence interval#)
:t-occurrence)]
(if interval#
(update ~conclusion :statement reduce-seq-conj)
~conclusion))))
(defmethod conclusion-transformation :shift-occurrence-backward
[args concl]
(let [duration (- temporal-window-duration)]
(m/match (mapv #(if (and (coll? %) (= 'quote (first %)))
(second %) %) (rest args))
[(:or '=|> '==>)] concl
['pred-impl] `(let [:t-occurrence (+ :t-occurrence ~duration)] ~concl)
['retro-impl] `(let [:t-occurrence (- :t-occurrence ~duration)] ~concl)
[sym (:or '=|> '==>)] (shift-backward-let sym concl)
[sym 'pred-impl] (shift-backward-let sym `+ concl duration)
[sym 'retro-impl] (shift-backward-let sym `- concl duration))))
+24
View File
@@ -0,0 +1,24 @@
(ns nal.deriver.premises-swapping
(:require [nal.deriver.key-path :refer [rule-path]]
[nal.deriver.normalization :refer [commutative-ops]]))
;the set of keys which prevent premises swapping for rule
(def anti-swapping-keys
#{:question? :judgement? :goal? :measure-time :t/belief-structural-deduction
:t/structural-deduction :t/belief-structural-difference :t/identity
:t/negation :union :intersection :t/intersection :t/union})
(defn allow-swapping?
"Checks if rule allow swapping of premises."
[{:keys [pre conclusions]}]
(let [{:keys [post conclusion]} (first conclusions)]
(and (not-any? anti-swapping-keys (flatten (concat pre post)))
(not-any? commutative-ops (flatten conclusion)))))
(defn swap-premises
[{:keys [p1 p2] :as rule}]
(assoc rule :p1 p2
:p2 p1
:full-path (rule-path p2 p1)))
(defn swap [rule] [rule (swap-premises rule)])
+143
View File
@@ -0,0 +1,143 @@
(ns nal.deriver.rules
(:require [clojure.string :as s]
[clojure.set :refer [map-invert]]
[nal.deriver
[key-path :refer [rule-path all-paths path-invariants path]]
[utils :refer [walk]]
[list-expansion :refer [contains-list? generate-all-lists]]
[premises-swapping :refer [allow-swapping? swap]]
[matching :refer [generate-matching]]
[backward-rules :refer [allow-backward? expand-backward-rules]]
[normalization :refer [infix->prefix replace-negation]]
[terms-permutation :refer [order-for-all-same? generate-all-orders]]]))
(defn options
"Generates map from rest of the rule's args."
[args]
(when (seq args)
(into {} (map vec (partition 2 args)))))
(defn get-conclusions
"Parses conclusions from the rule."
[c opts]
(if (and (seq? c) (some #{:post} c))
(map (fn [[c _ post]] {:conclusion c :post post}) (partition 3 c))
[{:conclusion c :post (:post opts)}]))
(defn rule
"Generates rule from #R statement."
[data]
(let [[p1 p2 _ c & other] (replace-negation data)]
(let [p1 (infix->prefix p1)
p2 (infix->prefix p2)
c (infix->prefix c)
opts (options other)
conclusions (get-conclusions c opts)]
(map (fn [c]
{:p1 p1
:p2 p2
:conclusions [c]
:full-path (rule-path p1 p2)
:pre (infix->prefix (:pre opts))})
conclusions))))
(defn check-duplication
"Checks if there are rules with same premises and preconditions but with
different conclusions, merges them if they exist."
[rules]
(vals (reduce (fn [ac {:keys [p1 p2 pre conclusions] :as r}]
(let [k [p1 p2 pre]]
(if (ac k)
(update-in ac [k :conclusions] concat conclusions)
(assoc ac k r))))
{} rules)))
(defn question?
"Return true if rule allows only question as task."
[{:keys [pre]}]
(some #{:question?} pre))
(defn goal?
"Return true if rule allows only goal as task."
[{pre :pre [{post :post}] :conclusions}]
(or (some #{:goal?} pre)
(some (fn [el] (and (keyword? el)
(s/starts-with? (str el) ":d/")))
post)))
(defn judgement?
"Return true if rule allows only judgement as task."
[{:keys [pre] :as rule}]
(not (or (question? rule) (some #{:goal} pre))))
(defn add-possible-paths
"Selects all rules that will match the same path as current rule and adds
these rules to the set of rules that matches path.
For instance:
current rule's path [[--> [- :any :any] :any] :and [--> [:any :any]]]
so, if we find rule with path [[--> :any :any] :and [--> [:any :any]]],
it matches to current's rule path too, hence it should be added to the set
of rules that matches [[--> [- :any :any] :any] :and [--> [:any :any]]] path."
[ac [k {:keys [all]}]]
(let [rules (mapcat :rules (vals (select-keys ac all)))]
(-> ac
(update-in [k :rules] concat rules)
(update-in [k :rules] set))))
(defn rule->map
"Adds rule to map of rules, conjoin rule to set of rules that
matches to pattern. Rules paths are keys in this map."
[ac {:keys [p1 p2 full-path] :as rule}]
(-> ac
(update-in [full-path :rules] conj rule)
(assoc-in [full-path :pattern] [p1 p2])
(assoc-in [full-path :all] (all-paths (path p1) (path p2)))
(assoc-in [full-path :starts-with] (set (path-invariants p1)))
(assoc-in [full-path :end-with] (set (path-invariants p2)))))
(defn rules-map
"Generates map from list of #R satetments, whetre key is path, and value is
another map with keys pattern ans rules. Pattern is will be used to match
values from the premises, rules will be used to generate deriver."
[ruleset task-type]
(let [rules (reduce rule->map {} ruleset)]
(generate-matching rules task-type)))
;---------------------------------------------------------------------------
(defmacro rules->> [raw-rules & transformations]
(let [pairs (partition 2 transformations)]
(reduce (fn [code [pred fun]]
`(mapcat (fn [rule#]
(if (~pred rule#)
(~fun rule#)
[rule#]))
~code))
`~raw-rules
pairs)))
(defmacro defrules [name & rules]
`(def ~name (quote ~rules)))
(defn compile-rules
"Define rules. Rules must be #R statements."
;TODO exception on duplication of the rule
[& rules]
(time
(let [rules (rules->> (apply concat rules)
contains-list? generate-all-lists
contains-list? generate-all-lists
identity rule
order-for-all-same? generate-all-orders
allow-swapping? swap
allow-backward? expand-backward-rules)
judgement-rules# (check-duplication (filter judgement? rules))
question-rules# (check-duplication (filter question? rules))
goal-rules# (check-duplication (filter goal? rules))]
(println "Q rules:" (count question-rules#))
(println "J rules:" (count judgement-rules#))
(println "G rules:" (count goal-rules#))
{:judgement (rules-map judgement-rules# :judgement)
:question (rules-map question-rules# :question)
:goal (rules-map goal-rules# :goal)})))
+22
View File
@@ -0,0 +1,22 @@
(ns nal.deriver.set-functions
(:require [clojure.set :as set]))
;todo performance
(defn difference [[op & set1] [_ & set2]]
(into [op] (sort-by hash (set/difference (set set1) (set set2)))))
(defn union [[op & set1] [_ & set2]]
(into [op] (sort-by hash (set/union (set set1) (set set2)))))
(defn intersection [[op & set1] [_ & set2]]
(into [op] (sort-by hash (set/intersection (set set1) (set set2)))))
(def f-map {:difference difference
:union union
:intersection intersection})
(defn not-empty-diff? [[_ & set1] [_ & set2]]
(not-empty (set/difference (set set1) (set set2))))
(defn not-empty-inter? [[_ & set1] [_ & set2]]
(not-empty (set/intersection (set set1) (set set2))))
+39
View File
@@ -0,0 +1,39 @@
(ns nal.deriver.substitution
(:require [nal.deriver.utils :refer [walk]]
[clojure.core.unify :as u]
[clojure.string :as s]))
(defn replace-vars
"Defn replaces var-elements from statemts by placeholders for unification."
[var-type statement]
(walk statement
(and (coll? :el) (= var-type (first :el)))
(->> :el second (str "?") symbol)))
(defn unification-map
"Returns map of inified elements from both collections."
[var-symbol p2 p3]
(let [var-type (if (= var-symbol "$")
'ind-var
'dep-var)]
((u/make-occurs-unify-fn #(and (coll? %) (= var-type (first %)))) p2 p3)))
(def munification-map (memoize unification-map))
(defn placeholder->symbol [pl]
(->> pl str (drop 1) s/join symbol))
(defn replace-placeholders
"Updates keys in unification map from placeholders like ?X to vectors like
['dep-var X]"
[var-type u-map]
(->> u-map
(map (fn [[k v]] [[var-type (placeholder->symbol k)] v]))
(into {})))
(defn substitute
"Unifies p2 and p3, then replaces elements from the unification map
inside the conclusion."
[var-symbol p2 p3 conclusion]
(let [u-map (munification-map var-symbol p2 p3)]
(walk conclusion (u-map el) (u-map el))))
+52
View File
@@ -0,0 +1,52 @@
(ns nal.deriver.terms-permutation
(:require [nal.deriver.utils :refer [walk]]))
(defn contains-op?
"Checks if statement contains operators from set."
[statement s]
(cond
(symbol? statement) (s statement)
(or (keyword? statement)
(char? statement)
(string? statement)
(number? statement)) false
:default (some identity (map #(contains-op? % s) statement))))
(defn replace-op
"Replaces operator \"from\" by operator \"to\""
[statement from to]
(walk statement (= from el) to))
(defn permute-op
"Makes permuatation of operators from s in statement."
[statement s]
(if-let [op (contains-op? statement s)]
(map #(replace-op statement op %) s)
[statement]))
;equivalences, implications, conjunctions - sets of operators that are use in
; permutation for :order-for-all-same postcondititon
(def equivalences #{'<=> '</> '<|>})
(def implications #{'==> 'pred-impl '=|> 'retro-impl})
(def conjunctions #{'conj '&| 'seq-conj})
(defn generate-all-orders
"Permutes all operators in statement with :order-for-all-same precondition."
[{:keys [p1 p2 conclusions full-path pre] :as rule}]
(let [{:keys [conclusion] :as c1} (first conclusions)
statements (->> (permute-op [p1 p2 conclusion full-path pre] equivalences)
(mapcat (fn [st] (permute-op st conjunctions)))
(mapcat (fn [st] (permute-op st implications)))
set)]
(map (fn [[p1 p2 c full-path pre]]
(assoc rule :p1 p1
:p2 p2
:full-path full-path
:conclusions [(assoc c1 :conclusion c)]
:pre pre))
statements)))
(defn order-for-all-same?
"Return true if rule contains order-for-all-same postcondition"
[{:keys [conclusions]}]
(some #{:order-for-all-same} (:post (first conclusions))))
+171
View File
@@ -0,0 +1,171 @@
(ns nal.deriver.truth
(:require [narjure.defaults :refer [horizon] :as d]))
;https://github.com/opennars/opennars/blob/6611ee7f0b1428676b01ae4a382241a77ae3a346/nars_logic/src/main/java/nars/nal/meta/BeliefFunction.java
;https://github.com/opennars/opennars/blob/7b27dacec4cdbe77ca03d89296323d49875ac213/nars_logic/src/main/java/nars/truth/TruthFunctions.java
(defn t-and
(^double [^double a ^double b] (* a b))
(^double [^double a ^double b ^double c] (* a b c))
(^double [^double a ^double b ^double c ^double d] (* a b c d)))
(defn t-or
(^double [^double a ^double b] (- 1 (* (- 1 a) (- 1 b)))))
(defn w2c ^double [^double w]
(let [^double h horizon] (/ w (+ w h))))
(defn c2w ^double [^double c]
(let [^double h horizon] (/ (* h c) (- 1 c))))
;--------------------------------------------
(defn conversion [_ p1]
(when-let [[f c] p1] [1 (w2c (and f c))]))
(defn negation [[^double f ^double c] _] [(- 1 f) c])
(defn contraposition [[^double f ^double c]]
[0 (w2c (and (- 1 f) c))])
(defn revision [[^double f1 ^double c1] [^double f2 ^double c2]]
(let [w1 (c2w c1)
w2 (c2w c2)
w (+ w1 w2)]
[(/ (+ (* w1 f1) (* w2 f2)) w) (w2c w)]))
(defn deduction [[^double f1 ^double c1] [^double f2 ^double c2]]
[(t-and f1 f2) (t-and c1 c2)])
(defn a-deduction [[^double f1 ^double c1] c2] [f1 (t-and f1 c1 c2)])
(defn analogy [[^double f1 ^double c1] [^double f2 ^double c2]]
[(t-and f1 f2) (t-and c1 c2 f2)])
(defn resemblance [[^double f1 ^double c1] [^double f2 ^double c2]]
[(t-and f1 f2) (t-and c1 c2 (t-or f1 f2))])
(defn abduction [[^double f1 ^double c1] [^double f2 ^double c2]]
[f1 (w2c (t-and f2 c1 c2))])
(defn induction [p1 p2] (abduction p2 p1))
(defn exemplification [[^double f1 ^double c1] [^double f2 ^double c2]]
[1 (w2c (t-and f1 f2 c1 c2))])
(defn comparison [[^double f1 ^double c1] [^double f2 ^double c2]]
(let [f0 (t-or f1 f2)
f (if (zero? f0) 0 (/ (t-and f1 f2) f0))
c (w2c (t-and f0 c1 c2))]
[f c]))
(defn union [[^double f1 ^double c1] [^double f2 ^double c2]]
[(t-or f1 f2) (t-and c1 c2)])
(defn intersection [[^double f1 ^double c1] [^double f2 ^double c2]]
[(t-and f1 f2) (t-and c1 c2)])
(defn anonymous-analogy [[^double f1 ^double c1] p2] (analogy p2 [f1 (w2c c1)]))
(defn decompose-pnn [[^double f1 ^double c1] p2]
(when p2
(let [[^double f2 ^double c2] p2
fn (t-and f1 (- 1 f2))]
[(- 1 fn) (t-and fn c1 c2)])))
(defn decompose-npp [[^double f1 ^double c1] p2]
(when p2
(let [[^double f2 ^double c2] p2
f (t-and (- 1 f1) f2)]
[f (t-and f c1 c2)])))
(defn decompose-pnp [[^double f1 ^double c1] p2]
(when p2
(let [[^double f2 ^double c2] p2
f (t-and f1 (- 1 f2))]
[f (t-and f c1 c2)])))
(defn decompose-ppp [p1 p2] (decompose-npp (negation p1 p2) p2))
(defn decompose-nnn [[^double f1 ^double c1] p2]
(when p2
(let [[^double f2 ^double c2] p2
fn (t-and (- 1 f1) (- 1 f2))]
[(- 1 fn) (t-and fn c1 c2)])))
(defn difference [[^double f1 ^double c1] [^double f2 ^double c2]]
[(t-and f1 (- 1 f2)) (t-and c1 c2)])
(defn structual-intersection [_ p2] (deduction p2 [1 d/judgement-confidence]))
(defn structual-deduction [p1 _] (deduction p1 [1 d/judgement-confidence]))
(defn structual-abduction [p1 _] (abduction p1 [1 d/judgement-confidence]))
(defn reduce-conjunction [p1 p2]
(-> (negation p1 p2)
(intersection p2)
(a-deduction 1)
(negation p2)))
(defn t-identity [p1 _] p1)
(defn belief-identity [p1 p2] (when p2 p1))
(defn belief-structural-deduction [_ p2]
(when p2 (deduction p2 [1 d/judgement-confidence])))
(defn belief-structural-difference [_ p2]
(when p2
(let [[^double f ^double c] (deduction p2 [1 d/judgement-confidence])]
[(- 1 f) c])))
(defn belief-negation [_ p2] (when p2 (negation p2 nil)))
(defn desire-weak [[f1 c1] [f2 c2]]
[(t-and f1 f2) (t-and c1 c2 f2 (w2c 1.0))])
(defn desire-induction
[[f1 c1] [f2 c2]]
[f1 (w2c (t-and f2 c1 c2))])
(defn desire-structural-strong
[t _]
(analogy t [1.0 d/judgement-confidence]))
(def tvtypes
{:t/structural-deduction structual-abduction
:t/struct-int structual-intersection
:t/struct-abd structual-abduction
:t/identity t-identity
:t/conversion conversion
:t/contraposition contraposition
:t/negation negation
:t/comparison comparison
:t/intersection intersection
:t/union union
:t/difference difference
:t/decompose-ppp decompose-ppp
:t/decompose-pnn decompose-pnn
:t/decompose-nnn decompose-nnn
:t/decompose-npp decompose-npp
:t/decompose-pnp decompose-pnp
:t/induction induction
:t/abduction abduction
:t/deduction deduction
:t/exemplification exemplification
:t/analogy analogy
:t/resemblance resemblance
:t/anonymous-analogy anonymous-analogy
:t/belief-identity belief-identity
:t/belief-structural-deduction belief-structural-deduction
:t/belief-structural-difference belief-structural-difference
:t/belief-negation belief-negation
:t/reduce-conjunction reduce-conjunction})
(def dvtypes
{:d/strong analogy
:d/deduction intersection
:d/weak desire-weak
:d/induction desire-induction
:d/identity identity
:d/negation negation
:d/structural-strong desire-structural-strong})
+22
View File
@@ -0,0 +1,22 @@
(ns nal.deriver.utils
(:require [clojure.walk :as w]))
(defn not-operator?
"Checks if element is not operator"
[el] (re-matches #"[akxA-Z$]" (-> el str first str)))
(def operator? (complement not-operator?))
(defmacro walk
"Macro that helps to replace elements during walk. The first argument
is collection, rest of the arguments are cond-like
expressions. Default result of cond is element itself.
el (optionally :el) is reserved name for current element of collection."
[coll & conditions]
(let [el (gensym)
replace-el (fn [coll]
(w/postwalk #(if (or (= 'el %) (= :el %)) el %) coll))]
`(w/postwalk
(fn [~el] (cond ~@(replace-el conditions)
:else ~el))
~coll)))
+56
View File
@@ -0,0 +1,56 @@
(ns nal.reader
(:require [clojure.string :as s])
(:import (clojure.lang LispReader)))
(defn dispatch-reader-macro [ch fun]
(let [dm (.get
(doto
(.getDeclaredField LispReader "dispatchMacros")
(.setAccessible true))
nil)]
(aset dm (int ch) fun)))
(defn fetch-rule
([rdr] (fetch-rule rdr "" 0))
([rdr prev cnt]
(let [c (.read rdr)
cnt (case (char c)
\] (dec cnt)
\[ (inc cnt)
cnt)]
(if (neg? cnt)
prev
(recur rdr (str prev (char c)) cnt)))))
(defn add-brackets [s]
(str "[" s "]"))
(defn replacements [s]
(-> s
(s/replace #"\{([^\}]*)\}" "(ext-set $1)")
(s/replace #"\[([^\]]*)]" "(int-set $1)")
(s/replace #"=\\>" "retro-impl")
(s/replace #"=/>" "pred-impl")
(s/replace #"~" "int-dif")
(s/replace #"&/" "seq-conj ")
(s/replace #"\(&\s" "(ext-inter ")
(s/replace #"\s&\s" " ext-inter ")
(s/replace #"&&" "conj")
(s/replace #"\{--" "inst")
(s/replace #"--]" "prop")
(s/replace #"\{-\]" "inst-prop")
(s/replace #"\(\\" "(int-image")
(s/replace #"\(/" "(ext-image")
(s/replace #"\$([A-Z])" "(ind-var $1)")
(s/replace #"#([A-Z])" "(dep-var $1)")))
(defn read-rule [s]
(-> s replacements add-brackets read-string))
(defn rule [rdr letter-R opts & other]
(let [c (.read rdr)]
(if (= c (int \[))
(read-rule (fetch-rule rdr))
(throw (Exception. (str "Reader barfed on " (char c)))))))
(dispatch-reader-macro \R rule)
+452
View File
@@ -0,0 +1,452 @@
(ns nal.rules
(:require [nal.deriver.rules :refer [defrules compile-rules]]
nal.reader))
(declare --S S --P P <-> |- --> ==> M || && =|> -- A Ai B <=>)
(defrules all-rules
;Similarity to Inheritance
#R[(S --> P) (S <-> P) |- (S --> P) :post (:t/struct-int :p/judgement) :pre (:question?)]
;Inheritance to Similarity
#R[(S <-> P) (S --> P) |- (S <-> P) :post (:t/struct-abd :p/judgement) :pre (:question?)]
;Set Definition Similarity to Inheritance
#R[(S <-> {P}) S |- (S --> {P}) :post (:t/identity :d/identity :allow-backward)]
#R[(S <-> {P}) {P} |- (S --> {P}) :post (:t/identity :d/identity :allow-backward)]
#R[([S] <-> P) [S] |- ([S] --> P) :post (:t/identity :d/identity :allow-backward)]
#R[([S] <-> P) P |- ([S] --> P) :post (:t/identity :d/identity :allow-backward)]
#R[({S} <-> {P}) {S} |- ({P} --> {S}) :post (:t/identity :d/identity :allow-backward)]
#R[({S} <-> {P}) {P} |- ({P} --> {S}) :post (:t/identity :d/identity :allow-backward)]
#R[([S] <-> [P]) [S] |- ([P] --> [S]) :post (:t/identity :d/identity :allow-backward)]
#R[([S] <-> [P]) [P] |- ([P] --> [S]) :post (:t/identity :d/identity :allow-backward)]
;Set Definition Unwrap
#R[({S} <-> {P}) {S} |- (S <-> P) :post (:t/identity :d/identity :allow-backward)]
#R[({S} <-> {P}) {P} |- (S <-> P) :post (:t/identity :d/identity :allow-backward)]
#R[([S] <-> [P]) [S] |- (S <-> P) :post (:t/identity :d/identity :allow-backward)]
#R[([S] <-> [P]) [P] |- (S <-> P) :post (:t/identity :d/identity :allow-backward)]
; Nothing is more specific than a instance so it's similar
#R[(S --> {P}) S |- (S <-> {P}) :post (:t/identity :d/identity :allow-backward)]
#R[(S --> {P}) {P} |- (S <-> {P}) :post (:t/identity :d/identity :allow-backward)]
; nothing is more general than a property so it's similar
#R[([S] --> P) [S] |- ([S] <-> P) :post (:t/identity :d/identity :allow-backward)]
#R[([S] --> P) P |- ([S] <-> P) :post (:t/identity :d/identity :allow-backward)]
; Immediate Inference
; If S can stand for P P can to a certain low degree also represent the class S
; If after S usually P happens then it might be a good guess that usually before P happens S happens.
#R[(P --> S) (S --> P) |- (P --> S) :post (:t/conversion :p/judgement) :pre (:question?)]
#R[(P ==> S) (S ==> P) |- (P ==> S) :post (:t/conversion :p/judgement) :pre (:question?)]
#R[(P =|> S) (S =|> P) |- (P =|> S) :post (:t/conversion :p/judgement) :pre (:question?)]
#R[(P =\> S) (S =/> P) |- (P =\> S) :post (:t/conversion :p/judgement) :pre (:question?)]
#R[(P =/> S) (S =\> P) |- (P =/> S) :post (:t/conversion :p/judgement) :pre (:question?)]
; "If not smoking lets you be healthy being not healthy may be the result of smoking"
#R[(--S ==> P) P |- (--P ==> S) :post (:t/contraposition :allow-backward)]
#R[(--S ==> P) --S |- (--P ==> S) :post (:t/contraposition :allow-backward)]
#R[(--S =|> P) P |- (--P =|> S) :post (:t/contraposition :allow-backward)]
#R[(--S =|> P) --S |- (--P =|> S) :post (:t/contraposition :allow-backward)]
#R[(--S =/> P) P |- (--P =\> S) :post (:t/contraposition :allow-backward)]
#R[(--S =/> P) --S |- (--P =\> S) :post (:t/contraposition :allow-backward)]
#R[(--S =\> P) P |- (--P =/> S) :post (:t/contraposition :allow-backward)]
#R[(--S =\> P) --S |- (--P =/> S) :post (:t/contraposition :allow-backward)]
; A belief b <f c> is equal to --b <1-f c> which is the negation rule:
#R[(A --> B) A |- --(A --> B) :post (:t/negation :d/negation :allow-backward)]
#R[(A --> B) B |- --(A --> B) :post (:t/negation :d/negation :allow-backward)]
#R[--(A --> B) A |- (A --> B) :post (:t/negation :d/negation :allow-backward)]
#R[--(A --> B) B |- (A --> B) :post (:t/negation :d/negation :allow-backward)]
#R[(A <-> B) A |- --(A <-> B) :post (:t/negation :d/negation :allow-backward)]
#R[(A <-> B) B |- --(A <-> B) :post (:t/negation :d/negation :allow-backward)]
#R[--(A <-> B) A |- (A <-> B) :post (:t/negation :d/negation :allow-backward)]
#R[--(A <-> B) B |- (A <-> B) :post (:t/negation :d/negation :allow-backward)]
#R[(A ==> B) A |- --(A ==> B) :post (:t/negation :d/negation :allow-backward :order-for-all-same)]
#R[(A ==> B) B |- --(A ==> B) :post (:t/negation :d/negation :allow-backward :order-for-all-same)]
#R[--(A ==> B) A |- (A ==> B) :post (:t/negation :d/negation :allow-backward :order-for-all-same)]
#R[--(A ==> B) B |- (A ==> B) :post (:t/negation :d/negation :allow-backward :order-for-all-same)]
#R[(A <=> B) A |- --(A <=> B) :post (:t/negation :d/negation :allow-backward :order-for-all-same)]
#R[(A <=> B) B |- --(A <=> B) :post (:t/negation :d/negation :allow-backward :order-for-all-same)]
#R[--(A <=> B) A |- (A <=> B) :post (:t/negation :d/negation :allow-backward :order-for-all-same)]
#R[--(A <=> B) B |- (A <=> B) :post (:t/negation :d/negation :allow-backward :order-for-all-same)]
; If A is a special case of B and B is a special case of C so is A a special case of C (strong) the other variations are hypotheses (weak)
#R[(A --> B) (B --> C) |- (A --> C) :pre ((:!= A C)) :post (:t/deduction :d/strong :allow-backward)]
#R[(A --> B) (A --> C) |- (C --> B) :pre ((:!= B C)) :post (:t/abduction :d/weak :allow-backward)]
#R[(A --> C) (B --> C) |- (B --> A) :pre ((:!= A B)) :post (:t/induction :d/weak :allow-backward)]
#R[(A --> B) (B --> C) |- (C --> A) :pre ((:!= C A)) :post (:t/exemplification :d/weak :allow-backward)]
; similarity from inheritance
; If S is a special case of P and P is a special case of S then S and P are similar
#R[(S --> P) (P --> S) |- (S <-> P) :post (:t/intersection :d/strong :allow-backward)]
; inheritance from similarty <- TODO check why this one was missing
#R[(S <-> P) (P --> S) |- (S --> P) :post (:t/reduce-conjunction :d/strong :allow-backward)]
; similarity-based syllogism
; If P and S are a special case of M then they might be similar (weak)
; also if P and S are a general case of M
#R[(P --> M) (S --> M) |- (S <-> P) :post (:t/comparison :d/weak :allow-backward) :pre ((:!= S P))]
#R[(M --> P) (M --> S) |- (S <-> P) :post (:t/comparison :d/weak :allow-backward) :pre ((:!= S P))]
; If M is a special case of P and S and M are similar then S is also a special case of P (strong)
#R[(M --> P) (S <-> M) |- (S --> P) :pre ((:!= S P)) :post (:t/analogy :d/strong :allow-backward)]
#R[(P --> M) (S <-> M) |- (P --> S) :pre ((:!= S P)) :post (:t/analogy :d/strong :allow-backward)]
#R[(M <-> P) (S <-> M) |- (S <-> P) :pre ((:!= S P)) :post (:t/resemblance :d/strong :allow-backward)]
; inheritance-based composition
; If P and S are in the intension/extension of M then union/difference and intersection can be built:
#R[(P --> M) (S --> M) |- (((S | P) --> M) :post (:t/intersection)
((S & P) --> M) :post (:t/union)
((P ~ S) --> M) :post (:t/difference))
:pre ((:not-set? S) (:not-set? P)(:!= S P) (:no-common-subterm S P))]
#R[(M --> P) (M --> S) |- ((M --> (P & S)) :post (:t/intersection)
(M --> (P | S)) :post (:t/union)
(M --> (P - S)) :post (:t/difference))
:pre ((:not-set? S) (:not-set? P)(:!= S P) (:no-common-subterm S P))]
; inheritance-based decomposition
; if (S --> M) is the case and ((| S :list/A) --> M) is not the case then ((| :list/A) --> M) is not the case hence :t/decompose-pnn
#R[(S --> M) ((| S :list/A) --> M) |- ((| :list/A) --> M) :post (:t/decompose-pnn)]
#R[(S --> M) ((& S :list/A) --> M) |- ((& :list/A) --> M) :post (:t/decompose-npp)]
#R[(S --> M) ((S - P) --> M) |- (P --> M) :post (:t/decompose-pnp)]
#R[(S --> M) ((P - S) --> M) |- (P --> M) :post (:t/decompose-nnn)]
#R[(M --> S) (M --> (& S :list/A)) |- (M --> (& :list/A)) :post (:t/decompose-pnn)]
#R[(M --> S) (M --> (| S :list/A)) |- (M --> (| :list/A)) :post (:t/decompose-npp)]
#R[(M --> S) (M --> (S ~ P)) |- (M --> P) :post (:t/decompose-pnp)]
#R[(M --> S) (M --> (P ~ S)) |- (M --> P) :post (:t/decompose-nnn)]
; Set comprehension:
#R[(C --> A) (C --> B) |- (C --> R) :post (:t/union) :pre ((:set-ext? A) (:union A B R))]
#R[(C --> A) (C --> B) |- (C --> R) :post (:t/intersection) :pre ((:set-int? A) (:union A B R))]
#R[(A --> C) (B --> C) |- (R --> C) :post (:t/intersection) :pre ((:set-ext? A) (:union A B R))]
#R[(A --> C) (B --> C) |- (R --> C) :post (:t/union) :pre ((:set-int? A) (:union A B R))]
#R[(C --> A) (C --> B) |- (C --> R) :post (:t/intersection) :pre ((:set-ext? A) (:intersection A B R))]
#R[(C --> A) (C --> B) |- (C --> R) :post (:t/union) :pre ((:set-int? A) (:intersection A B R))]
#R[(A --> C) (B --> C) |- (R --> C) :post (:t/union) :pre ((:set-ext? A) (:intersection A B R))]
#R[(A --> C) (B --> C) |- (R --> C) :post (:t/intersection) :pre ((:set-int? A) (:intersection A B R))]
#R[(C --> A) (C --> B) |- (C --> R) :post (:t/difference) :pre ((:difference A B R))]
#R[(A --> C) (B --> C) |- (R --> C) :post (:t/difference) :pre ((:difference A B R))]
; Set element takeout:
#R[(C --> {:list/A}) C |- (C --> {:from/A}) :post (:t/structural-deduction)]
#R[(C --> [:list/A]) C |- (C --> [:from/A]) :post (:t/structural-deduction)]
#R[({:list/A} --> C) C |- ({:from/A} --> C) :post (:t/structural-deduction)]
#R[([:list/A] --> C) C |- ([:from/A] --> C) :post (:t/structural-deduction)]
; NAL3 single premise inference:
#R[((| :list/A) --> M) M |- (:from/A --> M) :post (:t/structural-deduction)]
#R[(M --> (& :list/A)) M |- (M --> :from/A) :post (:t/structural-deduction)]
#R[((B - G) --> S) S |- (B --> S) :post (:t/structural-deduction)]
#R[(R --> (B ~ S)) R |- (R --> B) :post (:t/structural-deduction)]
; NAL4 - Transformations between products and images:
; Relations and transforming them into different representations so that arguments and the relation it'self can become the subject or predicate
#R[((* :list/A) --> M) Ai |- (Ai --> (/ M :list/A))
:pre ((:substitute-from-list Ai _) (:contains? (:list/A) Ai))
:post (:t/identity :d/identity)]
#R[(M --> (* :list/A)) Ai |- ((\ M :list/A) --> Ai)
:pre ((:substitute-from-list Ai _) (:contains? (:list/A) Ai))
:post (:t/identity :d/identity)]
#R[(Ai --> (/ M :list/A )) M |- ((* :list/A) --> M)
:pre ((:substitute-from-list _ Ai) (:contains? (:list/A) Ai))
:post (:t/identity :d/identity)]
#R[((\ M :list/A) --> Ai) M |- (M --> (:list/A))
:pre ((:substitute-from-list _ Ai) (:contains? (:list/A) Ai))
:post (:t/identity :d/identity)]
; implication-based syllogism
#R[(M ==> P) (S ==> M) |- (S ==> P) :post (:t/deduction :order-for-all-same :allow-backward) :pre ((:!= S P))]
#R[(P ==> M) (S ==> M) |- (S ==> P) :post (:t/induction :allow-backward) :pre ((:!= S P))]
#R[(P =|> M) (S =|> M) |- (S =|> P) :post (:t/induction :allow-backward) :pre ((:!= S P))]
#R[(P =/> M) (S =/> M) |- (S =|> P) :post (:t/induction :allow-backward) :pre ((:!= S P))]
#R[(P =\> M) (S =\> M) |- (S =|> P) :post (:t/induction :allow-backward) :pre ((:!= S P))]
#R[(M ==> P) (M ==> S) |- (S ==> P) :post (:t/abduction :allow-backward) :pre ((:!= S P))]
#R[(M =/> P) (M =/> S) |- (S =|> P) :post (:t/abduction :allow-backward) :pre ((:!= S P))]
#R[(M =|> P) (M =|> S) |- (S =|> P) :post (:t/abduction :allow-backward) :pre ((:!= S P))]
#R[(M =\> P) (M =\> S) |- (S =|> P) :post (:t/abduction :allow-backward) :pre ((:!= S P))]
#R[(P ==> M) (M ==> S) |- (S ==> P) :post (:t/exemplification :allow-backward) :pre ((:!= S P))]
#R[(P =/> M) (M =/> S) |- (S =\> P) :post (:t/exemplification :allow-backward) :pre ((:!= S P))]
#R[(P =\> M) (M =\> S) |- (S =/> P) :post (:t/exemplification :allow-backward) :pre ((:!= S P))]
#R[(P =|> M) (M =|> S) |- (S =|> P) :post (:t/exemplification :allow-backward) :pre ((:!= S P))]
; equivalence-based syllogism
; Same as for inheritance again
#R[(P ==> M) (S ==> M) |- (S <=> P) :pre ((:!= S P)) :post (:t/comparison :allow-backward)]
#R[(P =/> M) (S =/> M) |- ((S <|> P) :post (:t/comparison :allow-backward)
(S </> P) :post (:t/comparison :allow-backward)
(P </> S) :post (:t/comparison :allow-backward))
:pre ((:!= S P))]
#R[(P =|> M) (S =|> M) |- (S <|> P) :pre ((:!= S P)) :post (:t/comparison :allow-backward)]
#R[(P =\> M) (S =\> M) |- ((S <|> P) :post (:t/comparison :allow-backward)
(S </> P) :post (:t/comparison :allow-backward)
(P </> S) :post (:t/comparison :allow-backward))
:pre ((:!= S P))]
#R[(M ==> P) (M ==> S) |- (S <=> P) :pre ((:!= S P)) :post (:t/comparison :allow-backward)]
#R[(M =/> P) (M =/> S) |- ((S <|> P) :post (:t/comparison :allow-backward)
(S </> P) :post (:t/comparison :allow-backward)
(P </> S) :post (:t/comparison :allow-backward))
:pre ((:!= S P))]
#R[(M =|> P) (M =|> S) |- (S <|> P) :pre ((:!= S P)) :post (:t/comparison :allow-backward)]
; Same as for inheritance again
#R[(M ==> P) (S <=> M) |- (S ==> P) :pre ((:!= S P)) :post (:t/analogy :allow-backward)]
#R[(M =/> P) (S </> M) |- (S =/> P) :pre ((:!= S P)) :post (:t/analogy :allow-backward)]
#R[(M =/> P) (S <|> M) |- (S =/> P) :pre ((:!= S P)) :post (:t/analogy :allow-backward)]
#R[(M =|> P) (S <|> M) |- (S =|> P) :pre ((:!= S P)) :post (:t/analogy :allow-backward)]
#R[(M =\> P) (M </> S) |- (S =\> P) :pre ((:!= S P)) :post (:t/analogy :allow-backward)]
#R[(M =\> P) (S <|> M) |- (S =\> P) :pre ((:!= S P)) :post (:t/analogy :allow-backward)]
#R[(P ==> M) (S <=> M) |- (P ==> S) :pre ((:!= S P)) :post (:t/analogy :allow-backward)]
#R[(P =/> M) (S <|> M) |- (P =/> S) :pre ((:!= S P)) :post (:t/analogy :allow-backward)]
#R[(P =|> M) (S <|> M) |- (P =|> S) :pre ((:!= S P)) :post (:t/analogy :allow-backward)]
#R[(P =\> M) (S </> M) |- (P =\> S) :pre ((:!= S P)) :post (:t/analogy :allow-backward)]
#R[(P =\> M) (S <|> M) |- (P =\> S) :pre ((:!= S P)) :post (:t/analogy :allow-backward)]
#R[(M <=> P) (S <=> M) |- (S <=> P) :pre ((:!= S P)) :post (:t/resemblance :order-for-all-same :allow-backward)]
#R[(M </> P) (S <|> M) |- (S </> P) :pre ((:!= S P)) :post (:t/resemblance :allow-backward)]
#R[(M <|> P) (S </> M) |- (S </> P) :pre ((:!= S P)) :post (:t/resemblance :allow-backward)]
; implication-based composition
; Same as for inheritance again
#R[(P ==> M) (S ==> M) |- (((P || S) ==> M) :post (:t/intersection)
((P && S) ==> M) :post (:t/union))
:pre ((:!= S P))]
#R[(P =|> M) (S =|> M) |- (((P || S) =|> M) :post (:t/intersection)
((P &| S) =|> M) :post (:t/union))
:pre ((:!= S P))]
#R[(P =/> M) (S =/> M) |- (((P || S) =/> M) :post (:t/intersection)
((P &| S) =/> M) :post (:t/union))
:pre ((:!= S P)) ]
#R[(P =\> M) (S =\> M) |- (((P || S) =\> M) :post (:t/intersection)
((P &| S) =\> M) :post (:t/union))
:pre ((:!= S P))]
#R[(M ==> P) (M ==> S) |- ((M ==> (P && S)) :post (:t/intersection)
(M ==> (P || S)) :post (:t/union))
:pre ((:!= S P))]
#R[(M =/> P) (M =/> S) |- ((M =/> (P &| S)) :post (:t/intersection)
(M =/> (P || S)) :post (:t/union))
:pre ((:!= S P))]
#R[(M =|> P) (M =|> S) |- ((M =|> (P &| S)) :post (:t/intersection)
(M =|> (P || S)) :post (:t/union))
:pre ((:!= S P))]
#R[(M =\> P) (M =\> S) |- ((M =\> (P &| S)) :post (:t/intersection)
(M =\> (P || S)) :post (:t/union))
:pre ((:!= S P))]
#R[(D =/> R) (D =\> K) |- ((K =/> R) :post (:t/abduction)
(R =\> K) :post (:t/induction)
(K </> R) :post (:t/comparison))
:pre ((:!= R K))]
; implication-based decomposition
; Same as for inheritance again
#R[(S ==> M) ((|| S :list/A) ==> M) |- ((|| :list/A) ==> M) :post (:t/decompose-pnn :order-for-all-same)]
#R[(S ==> M) ((&& S :list/A) ==> M) |- ((&& :list/A) ==> M) :post (:t/decompose-npp :order-for-all-same :seq-interval-from-premises)]
#R[(M ==> S) (M ==> (&& S :list/A)) |- (M ==> (&& :list/A)) :post (:t/decompose-pnn :order-for-all-same :seq-interval-from-premises)]
#R[(M ==> S) (M ==> (|| S :list/A)) |- (M ==> (|| :list/A)) :post (:t/decompose-npp :order-for-all-same)]
; conditional syllogism
; If after M P usually happens and M happens it means P is expected to happen
#R[M (M ==> P) |- P :post (:t/deduction :d/induction :order-for-all-same) :pre ((:shift-occurrence-forward ==>))]
#R[M (P ==> M) |- P :post (:t/abduction :d/deduction :order-for-all-same) :pre ((:shift-occurrence-backward ==>))]
#R[M (S <=> M) |- S :post (:t/analogy :d/strong :order-for-all-same) :pre ((:shift-occurrence-backward <=>))]
#R[M (M <=> S) |- S :post (:t/analogy :d/strong :order-for-all-same) :pre ((:shift-occurrence-forward ==>))]
; conjunction decompose
#R[(&& :list/A) Ai |- Ai :pre (:contains? (:list/A) Ai) :post (:t/structural-deduction :d/structural-strong)]
#R[(&/ :list/A) Ai |- Ai :pre (:contains? (:list/A) Ai) :post (:t/structural-deduction :d/structural-strong)]
#R[(&| :list/A) Ai |- Ai :pre (:contains? (:list/A) Ai) :post (:t/structural-deduction :d/structural-strong)]
#R[(&/ B :list/A) B |- (&/ :list/A) :pre (:goal?) :post (:t/deduction :d/strong :seq-interval-from-premises)]
; propositional decomposition
; If S is the case and (&& S :list/A) is not the case it can't be that (&& :list/A) is the case
#R[S (&/ S :list/A) |- (&/ :list/A) :post (:t/decompose-pnn :seq-interval-from-premises)]
#R[S (&| S :list/A) |- (&| :list/A) :post (:t/decompose-pnn)]
#R[S (&& S :list/A) |- (&& :list/A) :post (:t/decompose-pnn)]
#R[S (|| S :list/A) |- (|| :list/A) :post (:t/decompose-npp)]
; Additional for negation: https://groups.google.com/forum/#!topic/open-nars/g-7r0jjq2Vc
#R[S (&/ (-- S) :list/A) |- (&/ :list/A) :post (:t/decompose-nnn :seq-interval-from-premises)]
#R[S (&| (-- S) :list/A) |- (&| :list/A) :post (:t/decompose-nnn)]
#R[S (&& (-- S) :list/A) |- (&& :list/A) :post (:t/decompose-nnn)]
#R[S (|| (-- S) :list/A) |- (|| :list/A) :post (:t/decompose-ppp)]
; multi-conditional syllogism
; Inference about the pre/postconditions
#R[Y ((&& X :list/A) ==> B) |- ((&& :list/A) ==> B) :pre ((:substitute-if-unifies "$" X Y)) :post (:t/deduction :order-for-all-same :seq-interval-from-premises)]
#R[((&& M :list/A) ==> C) ((&& :list/A) ==> C) |- M :post (:t/abduction :order-for-all-same)]
; Can be derived by NAL7 rules so this won't be necessary there (:order-for-all-same left out here)
; the first rule does not have :order-for-all-same because it would be invalid see: https://groups.google.com/forum/#!topic/open-nars/r5UJo64Qhrk
#R[((&& :list/A) ==> C) M |- ((&& M :list/A) ==> C) :pre ((:not-implication-or-equivalence M)) :post (:t/induction)]
#R[((&& :list/A) =|> C) M |- ((&& M :list/A) =|> C) :pre ((:not-implication-or-equivalence M)) :post (:t/induction)]
#R[((&& :list/A) =/> C) M |- ((&& M :list/A) =/> C) :pre ((:not-implication-or-equivalence M)) :post (:t/induction)]
#R[((&& :list/A) =\> C) M |- ((&& M :list/A) =\> C) :pre ((:not-implication-or-equivalence M)) :post (:t/induction)]
#R[(A ==> M) ((&& M :list/A) ==> C) |- ((&& A :list/A) ==> C) :post (:t/deduction :order-for-all-same :seq-interval-from-premises)]
#R[((&& M :list/A) ==> C) ((&& A :list/A) ==> C) |- (A ==> M) :post (:t/induction :order-for-all-same)]
#R[(A ==> M) ((&& A :list/A) ==> C) |- ((&& M :list/A) ==> C) :post (:t/abduction :order-for-all-same :seq-interval-from-premises)]
; variable introduction
; Introduce variables by common subject or predicate
#R[(S --> M) (P --> M) |- (((P --> $X) ==> (S --> $X)) :post (:t/abduction)
((S --> $X) ==> (P --> $X)) :post (:t/induction)
((P --> $X) <=> (S --> $X)) :post (:t/comparison)
(&& (S --> #Y) (P --> #Y)) :post (:t/intersection))
:pre ((:!= S P))]
#R[(S --> M) (P --> M) |- (((&/ (P --> $X) I) =/> (S --> $X)) :post (:t/induction :linkage-temporal)
((S --> $X) =\> (&/ (P --> $X) I)) :post (:t/abduction :linkage-temporal)
((&/ (P --> $X) I) </> (S --> $X)) :post (:t/comparison :linkage-temporal)
(&/ (P --> #Y) I (S --> #Y)) :post (:t/intersection :linkage-temporal))
:pre ((:!= S P) (:measure-time I))]
#R[(S --> M) (P --> M) |- (((P --> $X) =|> (S --> $X)) :post (:t/abduction :linkage-temporal)
((S --> $X) =|> (P --> $X)) :post (:t/induction :linkage-temporal)
((P --> $X) <|> (S --> $X)) :post (:t/comparison :linkage-temporal)
(&| (P --> #Y) (S --> #Y)) :post (:t/intersection :linkage-temporal))
:pre ((:!= S P) (:concurrent Task Belief))]
#R[(M --> S) (M --> P) |- ((($X --> S) ==> ($X --> P)) :post (:t/induction)
(($X --> P) ==> ($X --> S)) :post (:t/abduction)
(($X --> S) <=> ($X --> P)) :post (:t/comparison)
(&& (#Y --> S) (#Y --> P)) :post (:t/intersection))
:pre ((:!= S P)) ]
#R[(M --> S) (M --> P) |- (((&/ ($X --> P) I) =/> ($X --> S)) :post (:t/induction :linkage-temporal)
(($X --> S) =\> (&/ ($X --> P) I)) :post (:t/abduction :linkage-temporal)
((&/ ($X --> P) I) </> ($X --> S)) :post (:t/comparison :linkage-temporal)
(&/ (#Y --> P) I (#Y --> S)) :post (:t/intersection :linkage-temporal))
:pre ((:!= S P) (:measure-time I))]
#R[(M --> S) (M --> P) |- ((($X --> S) =|> ($X --> P)) :post (:t/induction :linkage-temporal)
(($X --> P) =|> ($X --> S)) :post (:t/abduction :linkage-temporal)
(($X --> S) <|> ($X --> P)) :post (:t/comparison :linkage-temporal)
(&| (#Y --> S) (#Y --> P)) :post (:t/intersection :linkage-temporal))
:pre ((:!= S P) (:concurrent (M --> P) (M --> S)))]
; 2nd variable introduction
#R[(A ==> (M --> P)) (M --> S) |- (((&& A ($X --> S)) ==> ($X --> P)) :post (:t/induction)
(&& (A ==> (#Y --> P)) (#Y --> S)) :post (:t/intersection))
:pre ((:!= A (M --> S)))]
#R[(&& (M --> P) :list/A) (M --> S) |- ((($Y --> S) ==> (&& ($Y --> P) :list/A)) :post (:t/induction)
(&& (#Y --> S) (#Y --> P) :list/A) :post (:t/intersection))
:pre ((:!= S P))]
#R[(A ==> (P --> M)) (S --> M) |- (((&& A (P --> $X)) ==> (S --> $X)) :post (:t/abduction)
(&& (A ==> (P --> #Y)) (S --> #Y)) :post (:t/intersection)) ]
#R[(&& (P --> M) :list/A) (S --> M) |- (((S --> $Y) ==> (&& (P --> $Y) :list/A)) :post (:t/abduction)
(&& (S --> #Y) (P --> #Y) :list/A) :post (:t/intersection))
:pre ((:!= S P))]
#R[(A --> L) ((A --> S) ==> R) |- ((&& (#X --> L) (#X --> S)) ==> R) :post (:t/induction)]
#R[(A --> L) ((&& (A --> S) :list/A) ==> R) |- ((&& (#X --> L) (#X --> S) :list/A) ==> R) :pre ((:substitute A #X)) :post (:t/induction)]
; dependent variable elimination
; Decomposition with elimination of a variable
#R[B (&& A :list/A) |- (&& :list/A) :pre (:judgement? (:substitute-if-unifies "#" A B)) :post (:t/anonymous-analogy :d/strong :order-for-all-same :seq-interval-from-premises)]
; conditional abduction by dependent variable
#R[((A --> R) ==> Z) ((&& (#Y --> B) (#Y --> R) :list/A) ==> Z) |- (A --> B) :post (:t/abduction)]
#R[((A --> R) ==> Z) ((&& (#Y --> B) (#Y --> R)) ==> Z) |- (A --> B) :post (:t/abduction)]
; conditional deduction "An inverse inference has been implemented as a form of deduction" https://code.google.com/p/open-nars/issues/detail?id=40&can=1
#R[(U --> L) ((&& (#X --> L) (#X --> R)) ==> Z) |- ((U --> R) ==> Z) :post (:t/deduction)]
#R[(U --> L) ((&& (#X --> L) (#X --> R) :list/A) ==> Z) |- ((&& (U --> R) :list/A) ==> Z) :pre ((:substitute #X U)) :post (:t/deduction)]
; independent variable elimination
#R[B (A ==> C) |- C :post (:t/deduction :order-for-all-same) :pre ((:substitute-if-unifies "$" A B) (:shift-occurrence-forward ==>))]
#R[B (C ==> A) |- C :post (:t/abduction :order-for-all-same) :pre ((:substitute-if-unifies "$" A B) (:shift-occurrence-backward C ==>))]
#R[B (A <=> C) |- C :post (:t/deduction :order-for-all-same) :pre ((:substitute-if-unifies "$" A B) (:shift-occurrence-backward <=>))]
#R[B (C <=> A) |- C :post (:t/deduction :order-for-all-same) :pre ((:substitute-if-unifies "$" A B) (:shift-occurrence-forward <=>))]
; second level variable handling rules
; second level variable elimination (termlink level2 growth needed in order for these rules to work)
#R[(A --> K) (&& (#X --> L) (($Y --> K) ==> (&& :list/A))) |- (&& (#X --> L) :list/A) :pre ((:substitute $Y A)) :post (:t/deduction)]
#R[(A --> K) (($X --> L) ==> (&& (#Y --> K) :list/A)) |- (($X --> L) ==> (&& :list/A)) :pre ((:substitute #Y A)) :post (:t/anonymous-analogy)]
; precondition combiner inference rule (variable_unification6):
#R[((&& C :list/A) ==> Z) ((&& C :list/B) ==> Z) |- (((&& :list/A) ==> (&& :list/B)) :post (:t/induction)
((&& :list/B) ==> (&& :list/A)) :post (:t/induction))]
#R[(Z ==> (&& C :list/A)) (Z ==> (&& C :list/B)) |- (((&& :list/A) ==> (&& :list/B)) :post (:t/abduction)
((&& :list/B) ==> (&& :list/A)) :post (:t/abduction))]
; NAL7 specific inference
; Reasoning about temporal statements. those are using the ==> relation because relation in time is a relation of the truth between statements.
#R[X ((&/ K (:interval I)) ==> B) |- B :post (:t/deduction :d/induction :order-for-all-same) :pre ((:substitute-if-unifies "$" K X) (:shift-occurrence-forward I ==>))]
#_#R[X (XI ==> B) |- B :post (:t/deduction :d/induction :order-for-all-same) :pre ((:substitute-if-unifies "$" XI (&/ X :interval)) (:shift-occurrence-forward XI ==>))]
#_#R[X (BI ==> Y) |- BI :post (:t/abduction :d/deduction :order-for-all-same) :pre ((:substitute-if-unifies "$" Y X) (:shift-occurrence-backward BI ==>))]
; Temporal induction:
; When P and then S happened according to an observation by induction (weak) it may be that alyways after P usually S happens.
#R[P S |- (((&/ S I) =/> P) :post (:t/induction :linkage-temporal)
(P =\> (&/ S I)) :post (:t/abduction :linkage-temporal)
((&/ S I) </> P) :post (:t/comparison :linkage-temporal)
(&/ S I P) :post (:t/intersection :linkage-temporal))
:pre ((:measure-time I))]
#R[P S |- ((S =|> P) :post (:t/induction :linkage-temporal)
;(P =|> S) :post (:t/induction :linkage-temporal)
(S <|> P) :post (:t/comparison :linkage-temporal)
(&| S P) :post (:t/intersection :linkage-temporal))
:pre [(:concurrent Task Belief) (:not-implication-or-equivalence P) (:not-implication-or-equivalence S)]]
; here now are the backward inference rules which should really only work on backward inference:
#R[(A --> S) (B --> S) |- ((A --> B) :post (:p/question)
(B --> A) :post (:p/question)
(A <-> B) :post (:p/question))
:pre (:question?)]
; and the backward inference driven forward inference:
; NAL2:
#R[([A] <-> [B]) (A <-> B) |- ([A] <-> [B]) :pre (:question?) :post (:t/belief-identity :p/judgement)]
#R[({A} <-> {B}) (A <-> B) |- ({A} <-> {B}) :pre (:question?) :post (:t/belief-identity :p/judgement)]
#R[([A] --> [B]) (A <-> B) |- ([A] --> [B]) :pre (:question?) :post (:t/belief-identity :p/judgement)]
#R[({A} --> {B}) (A <-> B) |- ({A} --> {B}) :pre (:question?) :post (:t/belief-identity :p/judgement)]
; NAL3:
; composition on both sides of a statement:
#R[((& B :list/A) --> (& A :list/A)) (B --> A) |- ((& B :list/A) --> (& A :list/A)) :pre (:question?) :post (:t/belief-structural-deduction :p/judgement)]
#R[((| B :list/A) --> (| A :list/A)) (B --> A) |- ((| B :list/A) --> (| A :list/A)) :pre (:question?) :post (:t/belief-structural-deduction :p/judgement)]
#R[((- S A) --> (- S B)) (B --> A) |- ((- S A) --> (- S B)) :pre (:question?) :post (:t/belief-structural-deduction :p/judgement)]
#R[((~ S A) --> (~ S B)) (B --> A) |- ((~ S A) --> (~ S B)) :pre (:question?) :post (:t/belief-structural-deduction :p/judgement)]
; composition on one side of a statement:
#R[(W --> (| B :list/A)) (W --> B) |- (W --> (| B :list/A)) :pre (:question?) :post (:t/belief-structural-deduction :p/judgement)]
#R[((& B :list/A) --> W) (B --> W) |- ((& B :list/A) --> W) :pre (:question?) :post (:t/belief-structural-deduction :p/judgement)]
#R[(W --> (- S B)) (W --> B) |- (W --> (- S B)) :pre (:question?) :post (:t/belief-structural-difference :p/judgement)]
#R[((~ S B) --> W) (B --> W) |- ((~ S B) --> W) :pre (:question?) :post (:t/belief-structural-difference :p/judgement)]
; NAL4:
; composition on both sides of a statement:
#R[((* B P) --> Z) (B --> A) |- ((* B P) --> (* A P)) :pre (:question?) :post (:t/belief-structural-deduction :p/judgement)]
#R[((* P B) --> Z) (B --> A) |- ((* P B) --> (* P A)) :pre (:question?) :post (:t/belief-structural-deduction :p/judgement)]
#R[((* B P) <-> Z) (B <-> A) |- ((* B P) <-> (* A P)) :pre (:question?) :post (:t/belief-structural-deduction :p/judgement)]
#R[((* P B) <-> Z) (B <-> A) |- ((* P B) <-> (* P A)) :pre (:question?) :post (:t/belief-structural-deduction :p/judgement)]
#R[((\ N A _) --> Z) (N --> R) |- ((\ N A _) --> (\ R A _)) :pre (:question?) :post (:t/belief-structural-deduction :p/judgement)]
#R[((/ N _ B) --> Z) (S --> B) |- ((/ N _ B) --> (/ N _ S)) :pre (:question?) :post (:t/belief-structural-deduction :p/judgement)]
; NAL5:
#R[--A A |- --A :pre (:question?) :post (:t/belief-negation :p/judgement)]
#R[A --A |- A :pre (:question?) :post (:t/belief-negation :p/judgement)]
; compound composition one premise
#R[(|| B :list/A) B |- (|| B :list/A) :pre (:question?) :post (:t/belief-structural-deduction :p/judgement)]
)
-108
View File
@@ -1,108 +0,0 @@
(ns nal.truth-value
(:refer-clojure :exclude [== < >])
(:require [clojure.core.logic :refer [project fresh == defne]]
[clojure.core.logic.arithmetic :refer [< >]]
[nal.utils :refer :all]))
(declare f-exp f-neg f-cnv f-cnt f-ded f-ana f-res f-abd f-exe f-com f-int
f-uni f-dif f-pnn f-npp f-pnp f-nnn)
(defne f-rev [A1 A2 A3]
([[F1 C1] [F2 C2] [F C]]
(< C1 1) (< C2 1)
(project [C1 C2]
(fresh [M1 M2]
(== M1 (/ C1 (- 1 C1)))
(== M2 (/ C2 (- 1 C2)))
(project [M1 M2 F1 F2]
(== F (/ (+ (* M1 F1) (* M2 F2)) (+ M1 M2)))
(== C (/ (+ M1 M2) (+ M1 M2 1))))))))
(defne f-exp [A1 A2]
([[F C] E] (project [F C] (== E (+ (* C (- F 0.5)) 0.5)))))
(defne f-neg [A1 A2] ([[F1 C1] [F C1]] (u-not F1 F)))
(defne f-cnv [A1 A2]
([[F1 C1] [1 C]] (fresh [W] (u-and [F1 C1] W) (u-w2c W C))))
(defne f-cnt [A1 A2]
([[F1 C1] [0 C]] (fresh [F0 W] (u-not F1 F0) (u-and [F0 C1] W) (u-w2c W C))))
(defne f-ded [A1 A2 A3]
([[F1 C1] [F2 C2] [F C]] (u-and [F1 F2] F) (u-and [C1 C2 F] C)))
(defne f-ana [A1 A2 A3]
([[F1 C1] [F2 C2] [F C]] (u-and [F1 F2] F) (u-and [C1 C2 F2] C)))
(defne f-res [A1 A2 A3]
([[F1 C1] [F2 C2] [F C]]
(fresh [F0] (u-and [F1 F2] F) (u-or [F1 F2] F0) (u-and [C1 C2 F0] C))))
(defne f-abd [A1 A2 A3]
([[F1 C1] [F2 C2] [F2 C]]
(fresh [W] (u-and [F1 C1 C2] W) (u-w2c W C))))
(defn f-ind [T1 T2 T] (f-abd T2 T1 T))
(defne f-exe [A1 A2 A3]
([[F1 C1] [F2 C2] [1 C]]
(fresh [W] (u-and [F1 C1 F2 C2] W) (u-w2c W C))))
(defne f-com [A1 A2 A3]
([[0 C1] [0 C2] [0 0]])
([[F1 C1] [F2 C2] [F C]]
(fresh [F0 W]
(u-or [F1 F2] F0)
(project [F0] (> F0 0))
(project [F1 F2 F0] (== F (/ (* F1 F2) F0)))
(u-and [F0 C1 C2] W)
(u-w2c W C))))
(defne f-int [A1 A2 A3]
([[F1 C1] [F2 C2] [F C]]
(u-and [F1 F2] F)
(u-and [C1 C2] C)))
(defne f-uni [A1 A2 A3]
([[F1 C1] [F2 C2] [F C]]
(u-or [F1 F2] F)
(u-and [C1 C2] C)))
(defne f-dif [A1 A2 A3]
([[F1 C1] [F2 C2] [F C]]
(fresh [F0]
(u-not F2 F0)
(u-and [F1 F0] F)
(u-and [C1 C2] C))))
(defne f-pnn [A1 A2 A3]
([[F1 C1] [F2 C2] [F C]]
(fresh [F2n Fn]
(u-not F2 F2n)
(u-and [F1 F2n] Fn)
(u-not Fn F)
(u-and [Fn C1 C2] C))))
(defne f-npp [A1 A2 A3]
([[F1 C1] [F2 C2] [F C]]
(fresh [F1n]
(u-not F1 F1n)
(u-and [F1n F2] F)
(u-and [F C1 C2] C))))
(defne f-pnp [A1 A2 A3]
([[F1 C1] [F2 C2] [F C]]
(fresh [F2n]
(u-not F2 F2n)
(u-and [F1 F2n] F)
(u-and [F C1 C2] C))))
(defne f-nnn [A1 A2 A3]
([[F1 C1] [F2 C2] [F C]]
(fresh [F1n F2n Fn]
(u-not F1 F1n)
(u-not F2 F2n)
(u-and [F1n F2n] Fn)
(u-not Fn F)
(u-and [Fn C1 C2] C))))
-90
View File
@@ -1,90 +0,0 @@
(ns nal.utils
(:refer-clojure :exclude [!= == reduce replace >= <= > < =])
(:require [clojure.core.logic
:refer [project fresh == defne lvaro conda nonlvaro run* membero
conso defna onceo u# s# appendo != and* all]
:as l]
[clojure.core.logic.arithmetic :refer [=]]))
(declare u-and u-or subtracto intersectiono uniono subseto deleteo)
(defn u-not [n0 n]
(project [n0] (== n (- 1 n0))))
(defne u-and [A1 A2]
([[N] N])
([[N0 . Nt] N]
(fresh [N1]
(u-and Nt N1)
(project [N0 N1] (== N (* N0 N1))))))
(defne u-or [A1 A2]
([[N] N])
([[N0 . Nt] N]
(fresh [N1]
(u-or Nt N1)
(project [N0 N1] (== N (- (+ N0 N1) (* N0 N1)))))))
(defn u-w2c [w c]
(project [w c] (== c (/ w (inc w)))))
;ported from prlog
;http://eclipseclp.org/doc/bips/lib/lists/subtract-3.html
(defna subtracto [L1 L2 L3]
([[] _ []])
([[Head . Tail] L2 L3]
(onceo (membero Head L2))
(subtracto Tail L2 L3))
([[Head . Tail1] L2 [Head . Tail3]]
(subtracto Tail1 L2 Tail3)))
;ported from prolog
;http://eclipseclp.org/doc/bips/lib/lists/intersection-3.html
(defna intersectiono [S1 S2 S3]
([[] _ []])
([[Head . L1tail] L2 L3]
(onceo (membero Head L2))
(fresh [L3tail]
(conso Head L3tail L3)
(intersectiono L1tail L2 L3tail)))
([[_ . L1tail] L2 L3]
(intersectiono L1tail L2 L3)))
;ported from prolog
;http://eclipseclp.org/doc/bips/lib/lists/union-3.html
(defna uniono [L1 L2 L3]
([[] L L])
([[Head . L1tail] L2 L3]
(onceo (membero Head L2))
(uniono L1tail L2 L3))
([[Head . L1tail] L2 [Head . L3tail]]
(uniono L1tail L2 L3tail)))
(defn trueo [a] (== true a))
;http://eclipseclp.org/doc/bips/kernel/typetest/atom-1.html
(defn atomo [a]
(nonlvaro a) (project [a] (trueo (symbol? a))))
(defn noto [a] (conda [a u#] [s#]))
(defmacro findallo [T G L]
`(== ~L (run* [q#] (== q# ~T) ~G)))
(defna subseto [A1 A2]
([[] A2])
([[X . L] [X . S]] (subseto L S))
([L [_ . S]] (subseto L S)))
(defn nonlvarso [lvars]
(and* (map (fn [l] (nonlvaro l)) lvars)))
(defn groundo [T]
(all (nonlvaro T)
(project [T]
(conda [(trueo (coll? T)) (nonlvarso T)]
[s#]))))
(defn noto= [x y]
"Like \\= in prolog."
(noto (== x y)))
+53
View File
@@ -0,0 +1,53 @@
(ns narjure.bag
(:require [clojure.data.priority-map :refer [priority-map-keyfn-by]]))
;TODO take/get semantic and names to be decided
(defprotocol Bag
(put-el [this item])
(get-el [this] [this k])
(remove-el [this k])
(take-el [this] [this k])
(count-els [this]))
;TODO must be discussed
(defn randomize-priority [priority]
(* (rand) priority))
(defn assoc-to-bag
"Set some random priority for a new item and slice map."
[col {:keys [key priority] :as v} capacity]
(let [ncol (->> (randomize-priority priority)
(assoc v :rand-priority)
(assoc col key))]
(if (> (count ncol) capacity)
(let [[k] (last ncol)]
(dissoc ncol k))
ncol)))
(defrecord DefaultBag [capacity queue]
Bag
(put-el [_ item]
(DefaultBag. capacity (assoc-to-bag queue item capacity)))
(get-el [_] (second (peek queue)))
(get-el [_ key] (when-let [el (queue key)] el))
(remove-el [_ key] (DefaultBag. capacity (dissoc queue key)))
(take-el [bag]
(when-let [el (get-el bag)]
[el (remove-el bag (:key el))]))
(take-el [bag k]
(when-let [el (get-el bag k)]
[el (remove-el bag k)]))
(count-els [_] (count queue)))
(defn default-bag
([] (default-bag 100))
([capacity]
(DefaultBag. capacity (priority-map-keyfn-by :rand-priority >))))
(extend-protocol Bag
nil
(put-el [_ el] (put-el (default-bag) el))
(get-el ([_]) ([_ _]))
(remove-el [_ _] (default-bag))
(take-el ([_]) ([_ _]))
(count-els [_] 0))
+31
View File
@@ -0,0 +1,31 @@
(ns narjure.control.buffer
(:require [clojure.core.async.impl.protocols :as impl])
(:import [java.util LinkedList]
[clojure.lang Fn Counted]))
(deftype PanickingSlidingBuffer
[^LinkedList buf ^long n ^Fn warning-callback ^long warning-n]
impl/UnblockingBuffer
impl/Buffer
(full? [this]
false)
(remove! [this]
(.removeLast buf))
(add!* [this itm]
(let [size (.size buf)]
(when (>= size warning-n)
(warning-callback size)
(when (= size n)
(impl/remove! this))))
(.addFirst buf itm)
this)
(close-buf! [this])
Counted
(count [this]
(.size buf)))
(defn panicking-sliding-buffer
([n callback]
(panicking-sliding-buffer n callback (Math/round (* 0.9 n))))
([n callback warning-n]
(PanickingSlidingBuffer. (LinkedList.) n callback warning-n)))
+110
View File
@@ -0,0 +1,110 @@
(ns narjure.control.flow
(:require [clojure.core.async :refer [go-loop <! >! chan]]
[clojure.set :as set]))
(defn check-element-in-map
"Checks if elements exist in map, if not
assocs elements to map with default value."
([s m] (check-element-in-map s 0 m))
([s default m]
(reduce (fn [ac k]
(if (ac k)
ac
(assoc ac k default)))
m s)))
(defn all
"Returns set aff all functions from workflow."
[wf]
(set (flatten wf)))
(defn kw->fn
"Transform function's keyword to var."
[kw]
(->> (str kw)
(drop 1)
(apply str)
symbol
resolve))
(defn vertex
"Creates vertex of flow graph. Arguments:
- functions: collection of collections, where first element is function
and second (optional) is output port
- inputs: ports (edges) that should be listened by vertex
- p: number of parallelism"
[functions inputs p]
(doseq [in inputs
_ (range (* p (count functions)))]
(go-loop []
(when-let [val (<! in)]
(doseq [[f out] functions]
(let [results (f val)]
(when out
(doseq [res (if (map? results)
[results]
results)]
(>! out res)))))
(recur)))))
(defn check-output
"Creates output port for function if it is necessary."
[buffer [function output-cnt]]
[function (when (pos? output-cnt) (chan buffer))])
(defn fn-outputs
"Generates map where keys are functions and values are ports which
will be used to send result of execution of functions."
[workflow buffer]
(->> (group-by first workflow)
(map (fn [[n t]] [n (count t)]))
(into {})
(check-element-in-map (all workflow))
(map #(check-output buffer %))
(into {})))
(defn fn-inputs [workflow]
"Groups functions to identify vertexes and edges that they should listen.
Returns map {vertexes edges ...}
in: [[:a :b]
[:a :c]
[:c :d]
[:b :d]]
out: {[:c :b] [:a], [:d] [:b :c]}"
(->> workflow
(reduce (fn [ac [k v]]
(update ac k conj v))
{})
(reduce (fn [ac [k v]]
(update ac v conj k))
{})))
(defn generate-flow
"Generates flow of functions which is discribed by pairs of functions,
where result of fisrt function will be sent to input of the second function.
Optionally map of configuration params can be passed.
Possible configs:
- parallelism: map where keys are functions and values
are parallelization numbers
- default-p: defaulp parallelization number
- buffer: capacity of fixed buffer for all channels"
;TODO configuration for custom buffers
([workflow] (generate-flow workflow {}))
([workflow {:keys [parallelism default-p buffer]
:or {parallelism {}
default-p 1
buffer 100}}]
(let [in (chan buffer)
outputs (assoc (fn-outputs workflow buffer) :in in)
inputs (fn-inputs workflow)
all-inputs (set (mapcat key inputs))
input-tasks (set/difference (all workflow) all-inputs)
it2 (assoc inputs input-tasks [:in])]
(doseq [[tasks from] it2]
(vertex
(map (fn [f] [(kw->fn f) (outputs f)]) tasks)
(map outputs from)
(apply max (map #(parallelism % default-p) tasks))))
in)))
+69
View File
@@ -0,0 +1,69 @@
(ns narjure.control.general-inference
(:require [narjure.system :refer [memory inference]]
[narjure.control.flow :as f]
[narjure.memory.api :as m]))
;; select-concepts
;; | | | |
;; v v v ... v
;; select-task-link --> update-tasklink-budget
;; |
;; v
;; select-term-link --> update-termlink-budget
;; |
;; v
;; do-inference
(def workflow
[[::select-concepts ::select-task-link]
[::select-task-link ::update-tasklink-budget]
[::select-task-link ::select-term-link]
[::select-term-link ::update-termlink-budget]
[::select-term-link ::do-inference]])
(defn select-concept [_]
(m/pull-activated-concepts memory))
(defn select-task-link [concept]
(let [{task-id :task} (m/select-tasklink memory concept)
task (m/task memory task-id)]
{:task task
:concept concept}))
;(defn update-concept-budget [data] data)
(defn select-term-link [{:keys [concept task] :as data}]
(let [{linked-concept :concept} (m/select-termlink memory concept)
occurrence (:occurrence task)
truth (m/select-truth memory linked-concept occurrence)]
(assoc data :truth truth
:term (m/term memory linked-concept))))
;(defn update-tasklink-budget [data] data)
;(defn update-termlink-budget [data] data)
(defn do-inference [{:keys [task truth term]}]
(let [{statement :term
:keys [frequency confidence plausibility
desirability task-type occurrence]}
task
task {:statement statement
:desire [plausibility desirability]
:truth [frequency confidence]
:task-type task-type
:occurrence occurrence}
truth (assoc truth :statement term)
results (inference task truth)]
(doseq [res results]
(m/push-task memory res))))
(def default-parallelism {::do-inference 4})
(defn general-inference-flow
[{:keys [parallelism]
:or {parallelism default-parallelism}
:as options}]
(f/generate-flow workflow options))
+37
View File
@@ -0,0 +1,37 @@
(ns narjure.control.local-inference)
(def workflow
[[:read-task :answer-yn-question]
[:read-task :answer-general-question]
[:answer-yn-question :out-answers]
[:answer-general-question :out-answers]
[:read-task :find-related-concepts]
[:find-related-concepts :out-update-tasklinks]
[:find-related-concepts :out-check-tasklinks-capacity]
[:read-task :belief-revision]
[:belief-revision :out-update-beliefs]
[:read-task :goal-revision]
[:goal-revision :out-update-goals]])
(defn answer-yn-question [{:keys [task] :as segment}]
(println :yn)
{})
(defn answer-general-question [{:keys [task] :as segment}]
(println :general)
{})
(defn find-related-concepts [{:keys [task] :as segment}]
(println :rel)
{})
(defn belief-revision [{:keys [task] :as segment}]
(println :bel)
{})
(defn goal-revision [{:keys [task] :as segment}]
(println :goal)
{})
+284
View File
@@ -0,0 +1,284 @@
(ns narjure.cycle
(:require [narjure.bag :refer :all]
[narjure.narsese :refer [parse]]
[nal.core :as c]
[clojure.set :refer [intersection union]]))
;TODO think about modules
(declare task->buffer)
;TODO create record for memory abstraction, but only after
;api will become more/less stable
(defn memory [buffer concepts]
{:concepts concepts
:cycles-cnt 0
:tasks-cnt 0
:buffer buffer
:local-inf-results #{}
:forward-inf-results #{}
:answers []})
(defn default-memory
([] (default-memory 100 100))
([buffer-capacity concepts-capacity]
(memory (default-bag buffer-capacity)
(default-bag concepts-capacity))))
(defn default-concept [term]
{:key term
:priority 1
:tasks (default-bag 100)
:beliefs (default-bag 100)
;here will be the map with patterns for possible questions
:answers {}})
(defn get-concept
"Check for concept in database, creates new in case in didn't find it."
[concepts term]
(if-let [concept (get-el concepts term)]
concept
(default-concept term)))
(defn overlapping-evidences?
[belief task]
(let [belief-ev-base (:evidental-base belief)
task-ev-base (:evidental-base task)]
(not-empty (intersection belief-ev-base task-ev-base))))
;TODO should be configurable
(def max-ev-base 100)
(defn total-ev-base
;TODO https://github.com/opennars/opennars/wiki/Stamp-In-NARS#evidential-base
[b1 b2]
(let [b1-ev-base (:evidental-base b1)
b2-ev-base (:evidental-base b2)]
(set (take max-ev-base (union b1-ev-base b2-ev-base)))))
;TODO bad name for function
(defn inf-statement
[{:keys [statement truth]}]
[statement truth])
(defn choice [belief task]
(let [statement (:statement belief)
truth (c/choice (:truth belief) (:truth task))]
{:statement statement
:key statement
:truth truth
:evidental-base (total-ev-base belief task)}))
(defn revision [belief task]
;TODO selecting the one with lower complexity here
;<(&/,<tim --> cat>,<tom --> cat>) =/> <sam --> cat>>.
;<<tim --> cat> =/> <sam --> cat>>.
;<?how =/> <sam --> cat>>?
;
;<<tim --> cat> =/> <sam --> cat>>. :12791129: %1.00;0.90%
;
; because the other ranking params, truth expectation and originality are in
; both cases the same, so complexity is the determining factor
; in this case
(let [statement (:statement belief)
truth (c/revision (:truth belief) (:truth task))]
{:statement statement
:key statement
:truth truth
:evidental-base (total-ev-base belief task)}))
(defn local-inference
"Revision/choice"
[belief task]
(when belief
(if (overlapping-evidences? belief task)
(choice belief task)
(revision belief task))))
(defn possible-questions
"Vector of questions that can be answered by the term."
[[copula term1 term2 :as term]]
[term [copula term1 '_0] [copula '_0 term2]])
(defn choice-with-nil [b t]
(if (nil? b)
t
(choice b t)))
(defn update-answers
[concept questions belief]
(reduce
(fn [c q]
(update-in c [:answers q] choice-with-nil belief))
concept questions))
(defmulti task->concept (fn [& args] (:action (first args))))
(defmethod task->concept :question
[{:keys [statement] :as task} {:keys [concepts] :as m} term]
(let [concept (get-concept concepts term)
answer (get-in concept [:answers statement])
upd-concept (update concept :tasks put-el task)
upd-m (update m :concepts put-el upd-concept)]
(if answer
(update upd-m :answers conj [task answer])
(task->buffer upd-m task))))
(defmethod task->concept :default
[{:keys [statement] :as task} {:keys [concepts] :as m} term]
(let [{:keys [beliefs] :as concept} (get-concept concepts term)
belief (get-el beliefs statement)
result (local-inference belief task)
task (if result (merge task result) (assoc task :key statement))
questions (possible-questions statement)
upd-concept (-> concept
(update :beliefs put-el task)
(update :tasks put-el task)
(update-answers questions task))
upd-m (update m :concepts put-el upd-concept)]
(if result
(update upd-m :local-inf-results conj result)
upd-m)))
(defn task->concepts
[m {:keys [terms] :as task}]
(reduce (partial task->concept task) m terms))
(def tasks-to-fetch 100)
(defn buffer->tasks
"Fetch portion of tasks from the buffer for processing"
[{:keys [buffer] :as m}]
(let [[buffer tasks]
(reduce (fn [[buf tasks] _]
(let [[task buf] (take-el buf)]
[buf (conj tasks task)])) [buffer []]
(range tasks-to-fetch))]
(assoc m :buffer buffer
:tasks tasks)))
(defn filling-tasks
"1. Select tasks in the buffer to insert into the corresponding concepts,
which may include the creation of new concepts (I'm not sure about the rest)
and beliefs, as well as direct processing on the tasks."
[{:keys [tasks] :as m}]
(dissoc (reduce task->concepts m tasks) :tasks))
(defn forward-inference [task belief]
(let [t (inf-statement task)
b (inf-statement belief)
conclusions (c/inference t b)
total-ev-base (total-ev-base belief task)]
(map (fn [[statement truth]]
{:statement statement
:key statement
:truth truth
:evidental-base total-ev-base})
conclusions)))
(defn inference
"2. Select a concept from the memory, then select a task and a belief
from the concept.
3. Feed the task and the belief to the inference engine
to produce derived tasks."
[{:keys [concepts] :as m}]
(let [;select concept
[{:keys [tasks beliefs] :as concept} concepts] (take-el concepts)
;select task
[{:keys [statement] :as task} tasks] (take-el tasks)
same-belief (get-el beliefs statement)
;select belief
[belief beliefs] (take-el (remove-el beliefs statement))]
(if (and task belief)
;if both task and belief were found start inference
;just return memory otherwise
(let [new-tasks (forward-inference task belief)
;update memory, putting tasks/beliefs/concepts back
upd-beliefs (-> beliefs
(put-el belief)
(put-el same-belief))
upd-concept (assoc concept :beliefs upd-beliefs
:tasks tasks)
upd-concepts (put-el concepts upd-concept)]
(->
;filling buffer via new tasks and update memory
(reduce task->buffer m new-tasks)
(assoc :concepts upd-concepts)
(update :forward-inf-results union (set new-tasks))))
(update m :concepts put-el (update concept :priority - 0.4)))))
(defn print-results! [{:keys [local-inf-results
forward-inf-results
answers] :as m}]
(when (not-empty local-inf-results)
(println "Local inference:")
(doall (map (fn [r] (println (inf-statement r))) local-inf-results)))
(when (not-empty forward-inf-results)
(println "Forward inference:")
(doall (map (fn [r] (println (inf-statement r))) forward-inf-results)))
(when (not-empty answers)
(println "Answers:")
(doall (map (fn [[q a]]
(println (:statement q) "? " a))
answers))))
(defn choose-answers
[{:keys [answers] :as m}]
(let [by-question (group-by first answers)]
(assoc m :answers
(map (fn [[q ans]]
[q (map inf-statement (map second ans))])
by-question))))
(defn do-cycle
"The cycle of NARS."
[memory]
(-> memory
(update :cycles-cnt inc)
buffer->tasks
filling-tasks
inference
choose-answers))
;TODO what is default priority for the tasks that arrived from the inference?
(defn task-priority [_] 0.8)
(defn pack-task
"Adds some properties to the task to make usable in Bag"
;TODO should be moved somewhere
[task cycle n]
(merge task
{;TODO hash to be replaced
:key (hash task)
:priority (task-priority task)
:cycle cycle
;TODO data structure for evidental base should be discussed
:evidental-base #{n}}))
(defn task->buffer
"Put task into the buffer."
[{:keys [cycles-cnt tasks-cnt] :as m} t]
(let [n-task (inc tasks-cnt)]
(assoc (update m :buffer put-el (pack-task t cycles-cnt n-task))
:tasks-cnt n-task)))
(defn fill-memory [& expression]
(reduce #(task->buffer %1 (parse %2)) (default-memory) expression))
(defn do-cycles [m n]
(reduce (fn [m _] (do-cycle m)) m (range n)))
(defn do-cycles-no-results [n m]
(do (do-cycles n m) nil))
(comment
(def m (fill-memory "<sport --> competition>."
"<chess --> competition>. %0.90%"))
(def mq (fill-memory "<bird --> swimmer>."
"<bird --> swimmer>?"))
(def mqq
(-> (default-memory)
(task->buffer (parse "<bird --> swimmer>."))
do-cycle
(task->buffer (parse "<bird --> swimmer>?"))
do-cycle)))
+35
View File
@@ -0,0 +1,35 @@
(ns narjure.defaults)
(def judgement-frequency 1.0)
(def judgement-confidence 0.9)
(def truth-value
[judgement-frequency judgement-confidence])
(def judgement-priority 0.5)
(def judgement-durability 0.8)
;todo clarify this
(def judgement-quality 0.5)
(def judgement-budget
[judgement-priority judgement-durability judgement-quality])
(def question-priority 0.5)
(def question-durability 0.9)
;todo clarify this
(def question-quality 0.5)
(def question-budget
[judgement-priority judgement-durability judgement-quality])
(def goal-confidence 0.9)
(def goal-priority 0.5)
(def goal-durability 0.8)
(def budgets
{:judgement judgement-budget
:question question-budget})
(def ^{:type double} horizon 1)
(def temporal-window-duration 80)
+32
View File
@@ -0,0 +1,32 @@
(ns narjure.memory.api)
(defprotocol Memory
(term [mem concept])
(select-truth [mem concept occurrence])
(truths [mem concept])
(desires [mem concept])
(tasklinks [mem concept])
(select-tasklink [mem concept])
(termlinks [mem concept])
(select-termlink [mem concept])
(budget [mem concept])
(add-term [mem concept])
(add-truth [mem concept truth])
(add-desire [mem concept desire])
(add-tasklink [mem concept link])
(add-termlink [mem concept link])
(remove-truth [mem concept id])
(remove-desire [mem concept id])
(remove-tasklink [mem concept id])
(remove-termlink [mem concept id])
(update-budget [mem update-fn])
(task [mem id])
(push-task [mem task])
(pop-task [mem])
(activate-concept [mem concept])
(pull-activated-concepts [mem]))
+174
View File
@@ -0,0 +1,174 @@
(ns narjure.memory.redis
(:require [taoensso.carmine :as c]
[narjure.memory.api :refer [Memory]])
(:import (java.util UUID)))
;postfixes for keys
(def truths-pr "_t")
(def desires-pr "_d")
(def tasklinks-pr "_tkl")
(def termlinks-pr "_tml")
(def budget-pr "_bg")
(def task-pr "_tsk")
(def parse-float #(Float/parseFloat %))
(def parse-int #(Integer/parseInt %))
(def parse-boolean #(Boolean/parseBoolean %))
(def truth-schema
{:frequency :float
:confidence :float
:occurrence :int
:evidences :any})
(def desire-schema
{:plausibility :float
:desirability :float
:occurrence :int
:evidences :any})
(def task-schema
{:task-type :keyword
:evidences :vector
:eternal :boolean
:occurrence :int
:frequency :float
:confidence :float
:plausibility :float
:desirability :float
:term :any})
(def termlink-schema
{:priority :float
:durability :float
:quality :float
:concept :string})
(def tasklink-schema
{:priority :float
:durability :float
:quality :float
:task :string})
(def deserialization-fn
{:float parse-float
:int parse-int
:boolean parse-boolean})
(defn apply-schema [val]
(->> val
(map (fn [[k v]]
[k (get deserialization-fn v identity)]))
(into {})))
(def deserialization-map
(reduce (fn [ac [key val]] (assoc ac key (apply-schema val)))
{}
{truths-pr truth-schema
desires-pr desire-schema
task-pr task-schema
tasklinks-pr tasklink-schema
termlinks-pr termlink-schema}))
(defn- check-hash [val]
(if (or (integer? val) (string? val)) val (hash val)))
(defn get-key [concept postfix]
(str (check-hash concept) postfix))
(defn- get-maps-ids [conn concept postfix]
(->> (get-key concept postfix)
c/smembers
(c/wcar conn)))
(defn xf [trans-map]
(comp (partition-all 2)
(map (fn [[k v]]
(let [k (keyword k)
tf (get trans-map k identity)]
[k (tf v)])))))
(defn- get-map-by-key [conn trans-map k]
(into {} (xf trans-map) (c/wcar conn (c/hgetall k))))
(defn- get-maps [conn concept postfix]
(map (partial get-map-by-key conn (deserialization-map postfix))
(get-maps-ids conn concept postfix)))
(defn- get-map [conn concept postfix]
(get-map-by-key conn (deserialization-map postfix) (get-key concept postfix)))
(defn- add-map [conn concept postfix data]
(let [id (str (UUID/randomUUID) postfix)]
(c/wcar conn (c/sadd (get-key concept postfix) id))
(c/wcar conn (c/hmset* id (assoc data :id id)))))
(defn- remove-map [conn concept postfix id]
(c/wcar conn (c/srem (get-key concept postfix) id))
(c/wcar conn (c/del id)))
(defn- push [conn task]
(let [id (str (UUID/randomUUID) "_tsk")]
(c/wcar conn (c/lpush "tasks" id))
(c/wcar conn (c/hmset* id task))))
(defn- tpop [conn]
(let [id (c/wcar conn (c/rpop))]
(get-map-by-key conn (deserialization-map task-pr) id)))
(defn pull-concepts [conn]
(-> (c/wcar
conn
(c/multi)
(c/smembers :active-concepts)
(println val)
(c/del :active-concepts)
(c/exec))
last
first))
(defn- activate-concept* [conn concept]
(c/wcar conn (c/sadd :active-concepts (check-hash concept))))
(defn- select-link [conn concept prefix]
(let [id (->> prefix
(get-key concept)
c/srandmember
(c/wcar conn))]
(get-map-by-key conn (deserialization-map prefix) id)))
(defn get-task [conn id]
(get-map-by-key conn (deserialization-map task-pr) id))
(defrecord RedisMemory
[conn]
Memory
(term [_ concept] (c/wcar conn (c/get (check-hash concept))))
(select-truth [_ concept occurrence] (select-link conn concept truths-pr))
(truths [_ concept] (get-maps conn concept truths-pr))
(desires [_ concept] (get-maps conn concept desires-pr))
(tasklinks [_ concept] (get-maps conn concept tasklinks-pr))
(select-tasklink [_ concept] (select-link conn concept tasklinks-pr))
(termlinks [_ concept] (get-maps conn concept termlinks-pr))
(select-termlink [_ concept] (select-link conn concept termlinks-pr))
(budget [_ concept] (get-map conn concept budget-pr))
(add-term [_ concept] (c/wcar conn (c/set (hash concept) concept)))
(add-truth [_ concept truth] (add-map conn concept truths-pr truth))
(add-desire [_ concept desire] (add-map conn concept desires-pr desire))
(add-tasklink [_ concept link] (add-map conn concept tasklinks-pr link))
(add-termlink [_ concept link] (add-map conn concept termlinks-pr link))
(remove-truth [_ concept id] (remove-map conn concept truths-pr id))
(remove-desire [_ concept id] (remove-map conn concept desires-pr id))
(remove-tasklink [_ concept id] (remove-map conn concept tasklinks-pr id))
(remove-termlink [_ concept id] (remove-map conn concept termlinks-pr id))
(update-budget [_ update-fn])
(task [_ id] (get-task conn id))
(push-task [_ task] (push conn task))
(pop-task [_] (tpop conn))
(activate-concept [_ concept] (activate-concept* conn concept))
(pull-activated-concepts [_] (pull-concepts conn)))
+76 -51
View File
@@ -1,6 +1,7 @@
(ns narjure.narsese
(:require [instaparse.core :as i]
[clojure.java.io :as io]))
[clojure.java.io :as io]
[narjure.defaults :refer :all]))
(def bnf-file "narsese.bnf")
@@ -23,32 +24,21 @@
"<|>" 'concurrent-equivalence})
(def compound-terms
{"{" 'ext-set
"[" 'int-set
"(&," 'ext-intersection
"&" 'ext-intersection
"(|," 'int-intersection
"|" 'int-intersection
"(-," 'ext-difference
"-" 'ext-difference
"(~," 'int-difference
"~" 'int-difference
"(*," 'product
"*" 'product
"(" 'product
"(/," 'ext-image
"(\\," 'int-image
"(--," 'negation
"--" 'negation
"(||," 'disjunction
"||" 'disjunction
"(&&," 'conjunction
"&&" 'conjunction
"(&/," 'sequential-events
{"{" 'ext-set
"[" 'int-set
"&" 'ext-intersection
"|" 'int-intersection
"-" 'ext-difference
"~" 'int-difference
"*" 'product
"(" 'product
"/" 'ext-image
"\\" 'int-image
"--" 'negation
"||" 'disjunction
"&&" 'conjunction
"&/" 'sequential-events
"(&|," 'parallel-events
"&|" 'parallel-events
})
"&|" 'parallel-events})
(defn get-compound-term [[_ operator-srt]]
(compound-terms operator-srt))
@@ -59,6 +49,7 @@
(def ^:dynamic *action* (atom nil))
(def ^:dynamic *lvars* (atom []))
(def ^:dynamic *truth* (atom []))
(def ^:dynamic *budget* (atom []))
(defn keep-cat [fun col]
(into [] (comp (mapcat fun) (filter (complement nil?))) col))
@@ -74,7 +65,11 @@
(defmethod element :sentence [[_ & data]]
(let [filtered (group-by string? data)]
(reset! *action* (actions (first (filtered true))))
(keep element (filtered false))))
(let [cols (filtered false)
last-el (last cols)]
(when (= :truth (first last-el))
(element last-el))
(element (first cols)))))
(defmethod element :statement [[_ & data]]
(if-let [copula (get-copula data)]
@@ -84,18 +79,22 @@
(defmethod element :task [[_ & data]]
`[~@(keep-cat element data)])
(defmethod element :compound-term [[_ _ & data]]
(let [first-el-type (get-in (vec data) [0 0])
comp-operator ((if (= :term first-el-type) second first) data)]
;looks strange but it is because of special syntax for negation --bird.
(defn get-comp-operator [second-el data]
(let [first-el-type (get-in (vec data) [0 0])]
(if (some #{(first second-el)} [:op-negation :op-int-set
:op-ext-set :op-product])
second-el
((if (= :term first-el-type) second first) data))))
(defmethod element :compound-term [[_ second-el & data]]
(let [comp-operator (get-comp-operator second-el data)]
`[~(get-compound-term comp-operator)
~@(keep-cat element (remove string? data))]))
(defmethod element :copula [_])
(defmethod element :op-multi [_])
(defmethod element :op-single [_])
(defmethod element :variable [[_ _ [_ v]]]
(let [v (symbol v)]
(def var-prefixes {"#" "d_" "?" "?"})
(defmethod element :variable [[_ type [_ v]]]
(let [v (symbol (str (var-prefixes type) v))]
(swap! *lvars* conj v)
v))
@@ -109,19 +108,41 @@
[[_ & data]]
`[[~@(mapv element data)]])
(defmethod element :frequency [[_ d]]
(let [d (Double/parseDouble d)]
(swap! *truth* conj d) d))
(defmethod element :confidence [[_ d]]
(let [d (Double/parseDouble d)]
(swap! *truth* conj d) d))
(defmethod element :priority [[_ d]] (Double/parseDouble d))
(defmethod element :durability [[_ d]] (Double/parseDouble d))
(defmacro double-element [n a]
`(defmethod element ~n [[t# d#]]
(let [d# (Double/parseDouble d#)]
(swap! ~a conj d#) d#)))
(defmethod element :default [[_ & data]]
(double-element :frequency *truth*)
(double-element :confidence *truth*)
(double-element :priority *budget*)
(double-element :durability *budget*)
(double-element :quality *budget*)
(defmethod element :task [[_ & data]]
(when (= :budget (ffirst data))
(element (first data)))
(element (last data)))
(defmethod element :term [[_ & data]]
(when (seq? data)
(keep element data)))
(defmethod element :default [_])
;TODO check for variables in statemnts, ignore subterm if it contains variable
(defn terms
"Fetch terms from task."
[statement]
(into #{statement} (rest statement)))
(defn check-truth-value [v]
(concat v (nthrest truth-value (count v))))
(defn check-budget [v act]
(let [budget (budgets act)]
(concat v (nthrest budget (count v)))))
(defn parse
"Parses a Narsese string into task ready for inference"
[narsese-str]
@@ -129,10 +150,14 @@
(if-not (i/failure? data)
(binding [*action* (atom nil)
*lvars* (atom [])
*truth* (atom [])]
(let [parsed-code (element data)]
{:action @*action*
:lvars @*lvars*
:truth @*truth*
:data parsed-code}))
*truth* (atom [])
*budget* (atom [])]
(let [statement (element data)
act @*action*]
{:action act
:lvars @*lvars*
:truth (check-truth-value @*truth*)
:budget (check-budget @*budget* act)
:statement statement
:terms (terms statement)}))
data)))
+27 -50
View File
@@ -1,10 +1,9 @@
(ns narjure.repl
(:require [clojure.string :as s]
[narjure.narsese :refer [parse]]
(:require [narjure.narsese :refer [parse]]
[instaparse.core :as i]
[nal.core :as c]
[clojure.core.logic :as l]
[clojure.tools.nrepl.middleware :refer [set-descriptor!]]))
[clojure.string :refer [trim]]
[clojure.tools.nrepl.middleware :refer [set-descriptor!]]
[narjure.cycle :as cycle]))
(defonce narsese-repl-mode (atom false))
@@ -16,34 +15,14 @@
(reset! narsese-repl-mode false)
(println "Narsese repl was stopped."))
(defonce db (atom {}))
(defonce buffer (atom []))
(defonce db (atom (cycle/default-memory)))
(defn clear-db! [] (reset! db {}))
(defn clear-buffer! [] (reset! buffer {}))
(defn clear-db! [] (reset! db (cycle/default-memory)))
(defmulti collect! :action)
(defn revision! [statement known-truth truth]
;I think that core.logic usage is not necesssary for revision calculation
(let [[[_ new-truth]]
(l/run* [q]
(c/revision [statement known-truth] [statement truth] q))]
(swap! db assoc statement new-truth)
[statement new-truth]))
(defn inference [n st1 st2]
(l/run n [q] (c/inference st1 st2 q)))
(defmethod collect! :judgement [{:keys [truth data]}]
(let [truth (if (empty? truth) [1 0.9] truth)
statement (first data)
known-truth (@db statement)]
(cond (nil? known-truth) (do (swap! db assoc statement truth) [statement truth])
(not= known-truth truth) (revision! statement known-truth truth)
:default [statement known-truth])))
(defmethod collect! :default [_])
(defn collect!
[{:keys [statement truth] :as task}]
(swap! db cycle/task->buffer task)
[statement truth])
(defn- parse-int [s]
(try (Integer/parseInt s) (catch Exception _)))
@@ -51,28 +30,26 @@
(defn- wrap-code [code]
(str "(narjure.repl/handle-narsese \"" code "\")"))
(defn- sentence [{:keys [data]}]
(let [statement (first data)]
[statement (@db statement)]))
(defn run [n]
(let [last-two (map sentence (take-last 2 @buffer))
forward (apply inference n last-two)
backward (if (> n (count forward))
(apply inference n (reverse last-two))
[])]
(into forward backward)))
(swap! db cycle/do-cycles n)
(cycle/print-results! @db)
(swap! db dissoc :forward-inf-results :local-inf-results :answers)
nil)
(defn- get-result [code]
(let [result (parse code)]
(if-not (i/failure? result)
(collect! result)
result)))
(defn handle-narsese [code]
(if-let [n (parse-int (clojure.string/trim code))]
(run n)
(if (= "stop!" (clojure.string/trim code))
(stop-narsese-repl!)
(let [result (parse code)]
(if (and (not (i/failure? result)))
(do (swap! buffer conj result)
(collect! result))
result)))))
(let [n (parse-int (trim code))]
(cond
(integer? n) (do (run n) nil)
(= "stop!" (trim code)) (stop-narsese-repl!)
(= \* (first code)) (do (clear-db!) nil)
(= \/ (first code)) ""
:default (get-result code))))
(defn narsese-handler [handler]
(fn [args & tail]
+15
View File
@@ -0,0 +1,15 @@
(ns narjure.system
(:require [mount.core :refer [defstate]]
[narjure.memory.redis :as r]
[nal.deriver.rules :refer [compile-rules]]
[nal.rules :refer [all-rules]]
[nal.core :as c]))
(declare memory inference)
(def redis-config
{:spec {:host "127.0.0.1" :port 6379}})
(defstate memory :start (r/->RedisMemory redis-config))
(defstate inference :start #(let [rules (compile-rules all-rules)]
(partial c/inference rules)))
-35
View File
@@ -1,35 +0,0 @@
(ns nal.test.args-processing
(:refer-clojure :exclude [== reduce replace])
(:require [clojure.test :refer :all]
[nal.args-processing :refer :all]
[clojure.core.logic :refer [run run* ==]]))
(deftest test-replace
(is (= '([1 (nil 2 3 4)]
[2 (1 nil 3 4)]
[3 (1 2 nil 4)]
[4 (1 2 3 nil)])
(run* [q1 q2] (replaceo [1 2 3 4] q1 q2))))
(is (= '([1 (_0 2 3) _0] [2 (1 _0 3) _0] [3 (1 2 _0) _0])
(run* [q1 q2 q3] (replaceo [1 2 3] q1 q2 q3))))
(is (= '((nil 2)) (run* [q] (replaceo [1 2] 1 q))))
(is (= '((1 2 nil 4)) (run* [q] (replaceo [1 2 3 4] 3 q))))
(is (= '(4) (run* [q] (replaceo [1 2] 1 [4 2] q))))
(is (= '() (run* [q] (replaceo [1 2] 2 [4 2] q)))))
(deftest test-include1
(is (= '(true) (run 1 [q] (== true q) (include1o [1 3] [1 2 3 4]))))
(is (= '() (run 1 [q] (== true q) (include1o [1 3 5] [1 2 3 4])))))
(deftest test-include
(is (= '(_0) (run* [q] (includeo [1 2 3] [1 2 3 4]))))
(is (= '() (run* [q] (includeo [1 2 5] [1 2 3 4]))))
(is (= '() (run* [q] (includeo [1 2 5] q)))))
(deftest test-same-set
(is (= '((1 3 2) (2 1 3) (3 1 2) (2 3 1) (3 2 1))
(run* [q] (same-seto [1 2 3] q)))))
(deftest test-same
(is (= '((1 2 3) (1 3 2) (2 1 3) (3 1 2) (2 3 1) (3 2 1))
(run* [q] (sameo [1 2 3] q)))))
+271
View File
@@ -0,0 +1,271 @@
(ns nal.test.core
(:require [clojure.test :refer :all]
[nal.core :as c]
[nal.rules :as r]
[nal.deriver.rules :refer [compile-rules]]))
(def inference (partial c/inference (compile-rules r/all-rules)))
(deftest test-inference
(are [a1 a2] (= (set a1) (set (apply inference a2)))
'({:statement [==>
[&| [--> [ext-set tim] [int-set driving]]]
[--> [ext-set tim] [int-set dead]]]
:truth [1.0 0.81]
:task-type :judgement
:occurrence 1})
'[{:statement [--> [ext-set tim] [int-set drunk]]
:truth [1 0.9]
:task-type :judgement
:occurrence 1}
{:statement [==> [&| [--> [ind-var X] [int-set drunk]] [--> [ind-var X] [int-set driving]]]
[--> [ind-var X] [int-set dead]]]
:truth [1 0.9]
:occurrence 0}]
'({:occurrence 1
:statement [&| a1 [--> [* a1 a2 a3] m]]
:task-type :judgement
:truth [1.0
0.81]}
{:occurrence 1
:statement [--> a1 [ext-image m _ a2 a3]]
:task-type :judgement
:truth [1
0.9]}
{:occurrence 1
:statement [<|> a1 [--> [* a1 a2 a3] m]]
:task-type :judgement
:truth [1.0
0.44751381215469616]}
{:occurrence 1
:statement [=|> [--> [* a1 a2 a3] m] a1]
:task-type :judgement
:truth [1
0.44751381215469616]}
{:occurrence 1
:statement [=|> a1 [--> [* a1 a2 a3] m]]
:task-type :judgement
:truth [1
0.44751381215469616]})
'[{:statement [--> [* a1 a2 a3] m]
:truth [1 0.9]
:task-type :judgement
:occurrence 1}
{:statement a1
:truth [1 0.9]
:occurrence 0}]
'[{:statement [=|> [--> [* a1 a2 a3] m] a1],
:task-type :judgement,
:occurrence 1,
:truth [1 0.44751381215469616]}
{:statement [<|> a1 [--> [* a1 a2 a3] m]],
:task-type :judgement,
:occurrence 1,
:truth [1.0 0.44751381215469616]}
{:statement [&| [--> [* a1 a2 a3] m] a1],
:task-type :judgement,
:occurrence 1,
:truth [1.0 0.81]}
{:statement [=|> a1 [--> [* a1 a2 a3] m]],
:task-type :judgement,
:occurrence 1,
:truth [1 0.44751381215469616]}]
'[{:statement a1
:truth [1 0.9]
:task-type :judgement
:occurrence 1}
{:statement [--> [* a1 a2 a3] m]
:truth [1 0.9]
:occurrence 0}]
'({:occurrence 1
:statement [&| a1 [conj a1 a2 a3]]
:task-type :judgement
:truth [1.0 0.81]}
{:occurrence 1
:statement [<|> a1 [conj a1 a2 a3]]
:task-type :judgement
:truth [1.0 0.44751381215469616]}
{:occurrence 1
:statement [=|> [conj a1 a2 a3] a1]
:task-type :judgement
:truth [1
0.44751381215469616]}
{:occurrence 1
:statement [=|> a1 [conj a1 a2 a3]]
:task-type :judgement
:truth [1
0.44751381215469616]}
{:occurrence 1
:statement a1
:task-type :judgement
:truth [1 0.44751381215469616]})
'[{:statement [conj a1 a2 a3]
:truth [1 0.9]
:task-type :judgement
:occurrence 1}
{:statement a1
:truth [1 0.9]
:occurrence 0}]
'[{:statement [=|> [--> M S] [[--> M S] [--> M P]]],
:task-type :judgement,
:occurrence 1,
:truth [1 0.44751381215469616]}
{:statement [<|> [--> M S] [[--> M S] [--> M P]]],
:task-type :judgement,
:occurrence 1,
:truth [1.0 0.44751381215469616]}
{:statement [&| [--> M S] [[--> M S] [--> M P]]],
:task-type :judgement,
:occurrence 1,
:truth [1.0 0.81]}
{:statement [=|> [[--> M S] [--> M P]] [--> M S]],
:task-type :judgement,
:occurrence 1,
:truth [1 0.44751381215469616]}]
'[{:statement [[--> M S] [--> M P]]
:truth [1 0.9]
:task-type :judgement
:occurrence 1}
{:statement [--> M S]
:truth [1 0.9]
:occurrence 0}]
'({:statement [conj [--> [dep-var Y] [int-set B]]
[==> [--> [ext-set A] [int-set Y]] [--> [dep-var Y] P]]]
:truth [1.0 0.81]
:task-type :judgement
:occurrence 1}
{:statement [==>
[conj [--> [ext-set A] [int-set Y]] [--> [ind-var X] [int-set B]]]
[--> [ind-var X] P]]
:truth [1 0.44751381215469616]
:task-type :judgement
:occurrence 1})
'[{:statement [==> [--> [ext-set A] [int-set Y]] [--> [ext-set A] P]]
:truth [1 0.9]
:task-type :judgement
:occurrence 1}
{:statement [--> [ext-set A] [int-set B]]
:truth [1 0.9]
:occurrence 1}]
'({:statement [</>
[seq-conj [--> chess competition] [:interval 1000]]
[--> sport competition]],
:task-type :judgement,
:occurrence 1000,
:truth [1.0 0.44751381215469616]}
{:statement [seq-conj
[--> chess competition]
[:interval 1000]
[--> sport competition]],
:task-type :judgement,
:occurrence 1000,
:truth [1.0 0.81]}
{:statement [pred-impl
[seq-conj [--> chess competition] [:interval 1000]]
[--> sport competition]],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [retro-impl
[--> sport competition]
[seq-conj [--> chess competition] [:interval 1000]]],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [--> sport chess],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [--> chess sport],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [<=> [--> chess [ind-var X]] [--> sport [ind-var X]]],
:task-type :judgement,
:occurrence 1000,
:truth [1.0 0.44751381215469616]}
{:statement [conj [--> chess [dep-var Y]] [--> sport [dep-var Y]]],
:task-type :judgement,
:occurrence 1000,
:truth [1.0 0.81]}
{:statement [<-> sport chess],
:task-type :judgement,
:occurrence 1000,
:truth [1.0 0.44751381215469616]}
{:statement [==> [--> chess [ind-var X]] [--> sport [ind-var X]]],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [==> [--> chess [ind-var X]] [--> sport [ind-var X]]],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [==> [--> sport [ind-var X]] [--> chess [ind-var X]]],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [==> [--> sport [ind-var X]] [--> chess [ind-var X]]],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [--> [int-dif chess sport] competition],
:task-type :judgement,
:occurrence 1000,
:truth [0.0 0.81]}
{:statement [--> [| chess sport] competition],
:task-type :judgement,
:occurrence 1000,
:truth [1.0 0.81]}
{:statement [--> [int-dif sport chess] competition],
:task-type :judgement,
:occurrence 1000,
:truth [0.0 0.81]}
{:statement [--> [ext-inter chess sport] competition],
:task-type :judgement,
:occurrence 1000,
:truth [1.0 0.81]}
{:statement [pred-impl
[seq-conj [--> chess [ind-var X]] [:interval 1000]]
[--> sport [ind-var X]]],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [</>
[seq-conj [--> chess [ind-var X]] [:interval 1000]]
[--> sport [ind-var X]]],
:task-type :judgement,
:occurrence 1000,
:truth [1.0 0.44751381215469616]}
{:statement [retro-impl
[--> sport [ind-var X]]
[seq-conj [--> chess [ind-var X]] [:interval 1000]]],
:task-type :judgement,
:occurrence 1000,
:truth [1 0.44751381215469616]}
{:statement [seq-conj
[--> chess [dep-var Y]]
[:interval 1000]
[--> sport [dep-var Y]]],
:task-type :judgement,
:occurrence 1000,
:truth [1.0 0.81]})
['{:statement [--> sport competition]
:truth [1 0.9]
:task-type :judgement
:occurrence 1000}
'{:statement [--> chess competition]
:truth [1 0.9]
:occurrence 0}]))
+26
View File
@@ -0,0 +1,26 @@
(ns nal.test.deriver
(:require [clojure.test :refer :all]
[nal.deriver :refer :all]
[nal.deriver.rules :refer [compile-rules]]
[nal.rules :as r]))
(def rules
(compile-rules '([(P --> M) (S --> M) |- (S <-> P)
:post (:t/comparison :d/weak :allow-backward)
:pre ((:!= S P))])))
(deftest test-generate-conclusions
(is (= (set [{:occurrence 1000
:statement '[<-> sport chess]
:task-type :judgement
:truth [1.0 0.44751381215469616]}])
(set (generate-conclusions
(rules :judgement)
'{:statement [--> sport competition]
:truth [1 0.9]
:task-type :judgement
:occurrence 1000}
'{:statement [--> chess competition]
:truth [1 0.9]
:occurrence 0})))))
+42
View File
@@ -0,0 +1,42 @@
(ns nal.test.deriver.backward-rules
(:require [clojure.test :refer :all]
[nal.deriver.backward-rules :refer :all]))
(deftest test-allow-backward?
(are [res] (= res :allow-backward)
(allow-backward? {:p1 :ok :p2 :boss
:conclusions [{:conclusion '[--> A B]
:post [:allow-backward]}]})
(allow-backward? {:p1 :ok :p2 :boss
:conclusions [{:conclusion '[--> A B]
:post [:t/ok :another-condition
:allow-backward]}]}))
(are [res] (nil? res)
(allow-backward? {:p1 :ok :p2 :boss
:conclusions [{:conclusion '[--> A B]
:post []}]})
(allow-backward? {:p1 :ok :p2 :boss
:conclusions [{:conclusion '[--> A B]
:post [:something :and-more
:t/abduction]}]})))
(def bw-rule
'{:p1 (<-> S (ext-set P))
:p2 S
:conclusions [{:conclusion (--> S (ext-set P))
:post (:t/identity :d/identity :allow-backward)}]
:full-path [(<-> :any (ext-set :any)) :and :any]
:pre nil})
(deftest test-generate-backward-rule
(let [[_ r1 r2 r3 :as rules] (expand-backward-rules bw-rule)]
(is (= 4 (count rules)))
(are [np1 np2 rule]
(let [{:keys [p1 p2]} rule]
(and (= p1 np1) (= p2 np2)))
'(<-> S (ext-set P)) 'S r1
'(--> S (ext-set P)) 'S r2
'(<-> S (ext-set P)) '(--> S (ext-set P)) r3)))
+48
View File
@@ -0,0 +1,48 @@
(ns nal.test.deriver.key-path
(:require [clojure.test :refer :all]
[nal.deriver.key-path :refer :all]
[nal.test.test-utils :refer [both-equal]]))
(deftest test-path
(both-equal
'(--> :any :any) (path '(--> A B))
'(--> (- :any :any) :any) (path '(--> (- B G) S))
'(==> (--> :any :any) (--> :any :any)) (path '(==> (--> $X S) (--> $X P)))))
(deftest test-rule-path
(both-equal
[:any :and :any] (rule-path 'A 'B)
'((--> :any :any) :and (--> :any :any)) (rule-path '(--> A B) '(--> B C))))
(deftest test-cart
(is (= '((1 2 3) (1 2 4) (2 2 3) (2 2 4)) (cart [[1 2] [2] [3 4]]))))
(deftest test-path-invariants
(is (= '((==> (--> :any :any) (--> :any :any))
(==> (--> :any :any) :any)
(==> :any (--> :any :any))
(==> :any :any)
:any)
(path-invariants '(==> (--> :any :any) (--> :any :any))))))
(deftest test-all-paths
(is
(= (set
'([(==> (--> :any :any) (--> :any :any)) :and (--> (- :any :any) :any)]
[(==> (--> :any :any) (--> :any :any)) :and (--> :any :any)]
[(==> (--> :any :any) :any) :and (--> (- :any :any) :any)]
[(==> :any (--> :any :any)) :and (--> (- :any :any) :any)]
[(==> (--> :any :any) :any) :and (--> :any :any)]
[(==> :any (--> :any :any)) :and (--> :any :any)]
[(==> :any :any) :and (--> (- :any :any) :any)]
[(==> (--> :any :any) (--> :any :any)) :and :any]
[(==> :any :any) :and (--> :any :any)]
[(==> (--> :any :any) :any) :and :any]
[(==> :any (--> :any :any)) :and :any]
[:any :and (--> (- :any :any) :any)]
[(==> :any :any) :and :any]
[:any :and (--> :any :any)]
[:any :and :any]))
(set (all-paths
'(==> (--> :any :any) (--> :any :any))
'(--> (- :any :any) :any))))))
+139
View File
@@ -0,0 +1,139 @@
(ns nal.test.deriver.list-expansion
(:require
[clojure.test :refer :all]
[nal.deriver.list-expansion :refer :all]))
(deftest test-get-list
(are [a1 a2] (= a1 (apply get-list a2))
:list/A
[":list"
'[(W --> (| B :list/A))
(W --> B)
|-
(W --> (| B :list/A))
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)]]
:from/A
[":from"
'[(C --> (ext-set :list/A))
C
|-
(C --> (ext-set :from/A))
:post
(:t/structural-deduction)]]))
(deftest test-gen-symbols
(are [a1 a2] (= a1 (apply gen-symbols a2))
'[A1 A2 A3] ['A 3]
'[B1 B2 B3 B4 B5] ['B 5]))
(deftest test-replace-list-elemets
(are [a1 a2] (= a1 (apply replace-list-elemets a2))
'[(W --> (| B A1 A2 A3 A4))
(W --> B)
|-
(W --> (| B A1 A2 A3 A4))
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)]
['[(W --> (| B :list/A))
(W --> B)
|-
(W --> (| B :list/A))
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)]
:list/A
"A"
4]
'[--> B1 B2 B3 B4 B5]
['[--> :list/B] :list/B "B" 5]))
(deftest test-list-name
(are [a1 a2] (= a1 (list-name a2))
"A" :list/A
"B" :list/B))
(deftest test-expand-:from-element
(are [a1 a2] (= a1 (apply expand-:from-element a2))
'([--> k A1] [--> k A2] [--> k A3])
['[--> k :from/A] :from/A "A" 3]
'([--> k [conj d A1]])
['[--> k [conj d :from/A]] :from/A "A" 1]))
(deftest test-generate-all-lists
(are [a1 a2] (= a1 (generate-all-lists a2))
'([(W --> (| B A1))
(W --> B)
|-
(W --> (| B A1))
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)]
[(W --> (| B A1 A2))
(W --> B)
|-
(W --> (| B A1 A2))
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)]
[(W --> (| B A1 A2 A3))
(W --> B)
|-
(W --> (| B A1 A2 A3))
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)]
[(W --> (| B A1 A2 A3 A4))
(W --> B)
|-
(W --> (| B A1 A2 A3 A4))
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)]
[(W --> (| B A1 A2 A3 A4 A5))
(W --> B)
|-
(W --> (| B A1 A2 A3 A4 A5))
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)]
[(W --> (| B A1 A2 A3 A4 A5 A6))
(W --> B)
|-
(W --> (| B A1 A2 A3 A4 A5 A6))
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)]
[(W --> (| B A1 A2 A3 A4 A5 A6 A7))
(W --> B)
|-
(W --> (| B A1 A2 A3 A4 A5 A6 A7))
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)])
'[(W --> (| B :list/A))
(W --> B)
|-
(W --> (| B :list/A))
:pre
(:question?)
:post
(:t/belief-structural-deduction :p/judgment)]))
+26
View File
@@ -0,0 +1,26 @@
(ns nal.test.deriver.matching
(:require [clojure.test :refer :all]
[nal.deriver.matching :refer :all]))
(deftest test-quote-operators
(are [a1 a2] (= a1 (quote-operators a2))
`(let
[~'G__57753
(nal.deriver.preconditions/abs (- :t-occurrence :b-occurrence))]
[:interval ~'G__57753])
`(let
[~'G__57753
(nal.deriver.preconditions/abs (- :t-occurrence :b-occurrence))]
[:interval ~'G__57753])
`(let
[~'G__57753
(nal.deriver.preconditions/abs (- :t-occurrence :b-occurrence))]
[(quote ~'ext-set) ~'G__57753])
`(let
[~'G__57753
(nal.deriver.preconditions/abs (- :t-occurrence :b-occurrence))]
[~'ext-set ~'G__57753])
'[(quote ext-set) x1 x2]
'(ext-set x1 x2)))
+84
View File
@@ -0,0 +1,84 @@
(ns nal.test.deriver.normalization
(:require [clojure.test :refer :all]
[nal.deriver.normalization :refer :all]
[nal.test.test-utils :refer [both-equal]]))
(deftest test-infix->prefix
(both-equal
'(--> A B) (infix->prefix '(A --> B))
'(--> (- B G) S) (infix->prefix '((B - G) --> S))
'(==> (--> $X S) (--> $X P)) (infix->prefix '(($X --> S) ==> ($X --> P)))))
(deftest test-neg-symbol?
(are [el] (true? el)
(#'nal.deriver.normalization/neg-symbol? '--)
(#'nal.deriver.normalization/neg-symbol? '--A))
(are [el] (false? el)
(#'nal.deriver.normalization/neg-symbol? '-->)
(#'nal.deriver.normalization/neg-symbol? 'B)))
(deftest test-trim-negation
(is (= 'A (#'nal.deriver.normalization/trim-negation '--A))))
(deftest test-neg-el
(is (= '(-- A) (neg 'A))))
(deftest test-replace-negation
(both-equal
'(-- A) (replace-negation '--A)
'(A (-- (--> A B))) (replace-negation '(A -- (--> A B)))
'(--> A B) (replace-negation '(--> A B))
'((-- A)) (replace-negation '[-- A])
'(-- A) (replace-negation '(-- A))))
(deftest test-reduce-ops
(are [a1 a2] (= a1 (reduce-ops a2))
1 '[ext-inter 1]
'[ext-inter 1] '[ext-inter [ext-inter 1] [ext-inter 1]]
'[ext-inter 2 1] '[ext-inter [ext-inter 1] [ext-inter 2]]
'[ext-inter 2 1] '[ext-inter [ext-inter 1] 2]
'[ext-inter 2 1] '[ext-inter 1 [ext-inter 2]]
'[int-set 2 1] '[ext-inter [int-set 1] [int-set 2]]
'[int-set 2 1] '[ext-inter [int-set 1] [int-set 2]]
'[ext-inter [ext-set 1] [ext-set 2]]
'[ext-inter [ext-set 1] [ext-set 2]]
2 '[| 2]
'[| 1] '[| [| 1] [| 1]]
'[| 2 1] '[| [| 1] [| 2]]
'[| 2 1] '[| [| 1] 2]
'[| 2 1] '[| 1 [| 2]]
'[int-set 3 2 1] '[| [int-set 1] [int-set 2 3]]
'[ext-set 3 2 1] '[| [ext-set 1] [ext-set 2 3]]
'[int-set 2 4 1] '[| [int-set 1 4] [int-set 2]]
'[ext-set 2 4 1] '[| [ext-set 1 4] [ext-set 2]]
'[ext-set 2 4 1] '[- [ext-set 5 6 2 4 1] [ext-set 5 6]]
'[int-set 2 4 1] '[int-dif [int-set 5 6 2 4 1] [int-set 5 6]]
'[<-> 1 2] '[<-> [ext-set 1] [ext-set 2]]
'[<-> 1 2] '[<-> [int-set 1] [int-set 2]]
'[* 1 2] '[* [* 1] 2]
'[* 1 3 2] '[* [* 1 3] 2]
'[* 1 3 2 4] '[* [* 1 3] 2 4]
1 '[ext-image [* 1 2] 2]
'[ext-image [* 1 2] 3] '[ext-image [* 1 2] 3]
1 '[int-image [* 1 2] 2]
'[int-image [* 1 2] 2 4] '[int-image [* 1 2] 2 4]
1 '[-- [-- 1]]
'[-- 1] '[-- 1]
1 '[conj 1]
1 '[conj 1 1]
'[conj 3 2 4 1] '[conj [conj 2 4] [conj 1 3]]
'[conj 3 2 4] '[conj [conj 2 4] 3]
'[conj 3 2 4] '[conj 2 [conj 3 4]]
1 '[|| 1]
1 '[|| 1 1]
'[|| 3 2 4 1] '[|| [|| 2 4] [|| 1 3]]
'[|| 3 2 4] '[|| [|| 2 4] 3]
'[|| 3 2 4] '[|| 2 [|| 3 4]]))
+34
View File
@@ -0,0 +1,34 @@
(ns nal.test.deriver.preconditions
(:require [clojure.test :refer :all]
[nal.deriver.preconditions :refer :all]))
(deftest test-compount-precondition
(are [c1 c2] (= c1 (compound-precondition c2))
`[(and
(coll? ~'A)
(= ~'ext-set (first ~'A)))]
'(:set-ext? A)
`[(and
(coll? ~'A)
(= ~'int-set (first ~'A)))]
'(:set-int? A)
'[(clojure.core/not= A B)]
'(:!= A B)
'[(nal.deriver.substitution/munification-map "$" A B)]
'(:substitute-if-unifies "$" A B)
`[(if (coll? ~'A)
(nil?
(nal.deriver.preconditions/implications-and-equivalences
(first ~'A)))
true)]
'(:not-implication-or-equivalence A)))
(deftest test-sets-preconditions
(are [c1 c2] (= c1 (count (compound-precondition c2)))
4 '(:difference A B)
3 '(:union A B)
4 '(:intersection A B)))
@@ -0,0 +1,40 @@
(ns nal.test.deriver.premises-swapping
(:require [clojure.test :refer :all]
[nal.deriver.premises-swapping :refer :all]))
(def question-rule
'{:p1 (--> P S)
:p2 (--> S P)
:conclusions [{:conclusion (--> P S)
:post (:t/conversion :p/judgment)}]
:full-path [(--> :any :any) :and (--> :any :any)]
:pre (:question?)})
(def rule-wth-commutative-term
'{:p1 (--> P M),
:p2 (--> S M),
:conclusions [{:conclusion (<-> S P),
:post (:t/comparison :d/weak :allow-backward)}],
:full-path [(--> :any :any) :and (--> :any :any)],
:pre ((:!= S P))})
(def rule
'{:p1 (--> M P),
:p2 (<-> S M),
:conclusions [{:conclusion (--> S P),
:post (:t/analogy :d/strong :allow-backward)}],
:full-path [(--> :any :any) :and (<-> :any :any)],
:pre ((:!= S P))})
(deftest test-allow-swapping?
(are [r] (false? (allow-swapping? r))
question-rule
rule-wth-commutative-term)
(is (true? (allow-swapping? rule))))
(deftest test-swap-premises
(let [{:keys [p1 p2]} rule
swapped (swap-premises rule)]
(is (and (= p1 (swapped :p2))
(= p2 (swapped :p1))))))
+60
View File
@@ -0,0 +1,60 @@
(ns nal.test.deriver.rules
(:require [clojure.test :refer :all]
[nal.deriver.rules :refer :all]
[nal.test.test-utils :refer [both-equal]]))
(deftest test-options
(both-equal {:pre [] :post []} (options [:pre [] :post []])
{:pre []} (options [:pre [] []])))
(deftest test-get-conclusions
(both-equal
[{:conclusion '(--> A B), :post nil}] (get-conclusions '(--> A B) {})
[{:conclusion '(--> A B), :post [:something]}]
(get-conclusions '(--> A B) {:post [:something]})
'({:conclusion (=|> S P), :post (:t/induction :linkage-temporal)}
{:conclusion (=|> P S), :post (:t/induction :linkage-temporal)}
{:conclusion (<|> S P), :post (:t/comparison :linkage-temporal)}
{:conclusion (&| S P), :post (:t/intersection :linkage-temporal)})
(get-conclusions
'((=|> S P) :post (:t/induction :linkage-temporal)
(=|> P S) :post (:t/induction :linkage-temporal)
(<|> S P) :post (:t/comparison :linkage-temporal)
(&| S P) :post (:t/intersection :linkage-temporal)) {})))
(def trule (rule '[(D pred-impl R) (D --> K) |- ((K pred-impl R) :post (:t/abduction)
(R --> K) :post (:t/induction)
(K </> R) :post (:t/comparison))
:pre ((:!= R K))]))
(deftest test-rule
(both-equal
'[{:p1 (--> S P),
:p2 (<-> S P),
:conclusions [{:conclusion (--> S P), :post (:t/struct-int :p/judgment)}],
:full-path [(--> :any :any) :and (<-> :any :any)],
:pre (:question?)}]
(rule '[(S --> P) (S <-> P) |- (S --> P) :post (:t/struct-int :p/judgment)
:pre (:question?)])
'({:conclusions [{:conclusion (pred-impl K R)
:post (:t/abduction)}]
:full-path [(pred-impl :any :any) :and (--> :any :any)]
:p1 (pred-impl D R)
:p2 (--> D K)
:pre ((:!= R K))}
{:conclusions [{:conclusion (--> R K)
:post (:t/induction)}]
:full-path [(pred-impl :any :any) :and (--> :any :any)]
:p1 (pred-impl D R)
:p2 (--> D K)
:pre ((:!= R K))}
{:conclusions [{:conclusion (</> K R)
:post (:t/comparison)}]
:full-path [(pred-impl :any :any) :and (--> :any :any)]
:p1 (pred-impl D R)
:p2 (--> D K)
:pre ((:!= R K))})
trule))
+55
View File
@@ -0,0 +1,55 @@
(ns nal.test.deriver.substitution
(:require [clojure.test :refer :all]
[nal.deriver.substitution :refer :all]))
(deftest test-replace-vars
(are [a1 a2 a3] (= a1 (replace-vars a2 a3))
'[--> ?X a]
'ind-var
'[--> [ind-var X] a]
'[--> [ind-var X] a]
'dep-var
'[--> [ind-var X] a]
'[==> [--> ?X k] ?Y]
'dep-var
'[==> [--> [dep-var X] k] [dep-var Y]]
'[==> [--> ?X k] ?X]
'dep-var
'[==> [--> [dep-var X] k] [dep-var X]]
'[==> [--> ?X k] [ind-var Y]]
'dep-var
'[==> [--> [dep-var X] k] [ind-var Y]]))
(deftest test-unification-map
(are [a1 a2] (= a1 (apply unification-map a2))
;successfully unifies
'{[ind-var X] tim} ["$" '[--> [ind-var X] alcoholic] '[--> tim alcoholic]]
;unifies even if there is no vars
{} ["$" '[--> tim alcoholic] '[--> tim alcoholic]]
;doesn't unify, var can be binded only once
nil ["$" '[==> [--> [ind-var X] a] [--> [ind-var X] b]]
'[==> [--> ok a] [--> boss b]]]
;unifies, different vars can contain the same values
'{[ind-var X] ok [ind-var Y] ok}
["$" '[==> [--> [ind-var X] a] [--> [ind-var Y] b]]
'[==> [--> ok a] [--> ok b]]]))
(deftest test-placeholder->symbol
(are [a1 a2] (= a1 (placeholder->symbol a2))
'X '?X
(symbol "1") '?1))
(deftest test-replace-placeholders
(are [a1 a2] (= a1 (apply replace-placeholders a2))
'{[dep-var X] 1 [dep-var Y] 2}
'[dep-var {?X 1 ?Y 2}]
'{[ind-var X] 1}
'[ind-var {?X 1}]))
+187
View File
@@ -0,0 +1,187 @@
(ns nal.test.deriver.terms-premutation
(:require [clojure.test :refer :all]
[nal.deriver.terms-permutation :refer :all]
[nal.test.test-utils :refer [both-equal]]))
(deftest test-contains-op?
(are [res] ((complement nil?) res)
(contains-op? '[--> [A B] [<-> C [ext-set D K]]] #{'ext-set})
(contains-op? '--> #{'-->})
(contains-op? '[--> A B] #{'-->}))
(are [res] (or (false? res) (nil? res))
(contains-op? '[--> [A B] [<-> C [ext-set D K]]] #{'<=>})
(contains-op? '--> '<->)))
(deftest test-replace-op
(both-equal
'[==> A B] (replace-op '[=|> A B] '=|> '==>)
'[==> A B] (replace-op '[=|> A B] '=|> '==>)
'[--> A [retro-impl A B]] (replace-op '[=|> A [retro-impl A B]] '=|> '-->)))
(deftest test-premure-op
(both-equal
'([=|> A B] [retro-impl A B] [==> A B] [pred-impl A B])
(permute-op '[==> A B] implications)
'([=|> A B] [retro-impl A B] [==> A B] [pred-impl A B])
(permute-op '[=|> A B] implications)
'(conj &| seq-conj) (permute-op '&| conjunctions)))
(def rule1
'{:p1 (==> M P)
:p2 (==> S M)
:conclusions [{:conclusion (==> S P)
:post (:t/deduction :order-for-all-same :allow-backward)}]
:full-path [(==> :any :any) :and (==> :any :any)]})
(def rule2
'{:p1 (==> M P)
:p2 (==> S M)
:conclusions [{:conclusion (==> S P)
:post (:allow-backward)}]
:full-path [(==> :any :any) :and (==> :any :any)]})
(def rule3
'{:p1 (==> S M)
:p2 (==> (conj S :list/A) M)
:conclusions [{:conclusion (==> (conj :list/A) M)
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}]
:full-path [(==> :any :any) :and (==> (conj :any :any) :any)]})
(deftest test-order-for-all-same?
(is ((complement nil?) (order-for-all-same? rule1)))
(is (nil? (order-for-all-same? rule2))))
(def res1
'({:p1 (=|> M P),
:p2 (=|> S M),
:conclusions [{:conclusion (=|> S P),
:post (:t/deduction :order-for-all-same :allow-backward)}],
:full-path [(=|> :any :any) :and (=|> :any :any)],
:pre nil}
{:p1 (==> M P),
:p2 (==> S M),
:conclusions [{:conclusion (==> S P),
:post (:t/deduction :order-for-all-same :allow-backward)}],
:full-path [(==> :any :any) :and (==> :any :any)],
:pre nil}
{:p1 (retro-impl M P),
:p2 (retro-impl S M),
:conclusions [{:conclusion (retro-impl S P),
:post (:t/deduction :order-for-all-same :allow-backward)}],
:full-path [(retro-impl :any :any) :and (retro-impl :any :any)],
:pre nil}
{:p1 (pred-impl M P),
:p2 (pred-impl S M),
:conclusions [{:conclusion (pred-impl S P),
:post (:t/deduction :order-for-all-same :allow-backward)}],
:full-path [(pred-impl :any :any) :and (pred-impl :any :any)],
:pre nil}))
(def res2
'({:p1 (pred-impl S M),
:p2 (pred-impl (seq-conj S :list/A) M),
:conclusions [{:conclusion (pred-impl (seq-conj :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(pred-impl :any :any) :and (pred-impl (seq-conj :any :any) :any)],
:pre nil}
{:p1 (retro-impl S M),
:p2 (retro-impl (seq-conj S :list/A) M),
:conclusions [{:conclusion (retro-impl (seq-conj :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(retro-impl :any :any)
:and
(retro-impl (seq-conj :any :any) :any)],
:pre nil}
{:p1 (=|> S M),
:p2 (=|> (seq-conj S :list/A) M),
:conclusions [{:conclusion (=|> (seq-conj :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(=|> :any :any) :and (=|> (seq-conj :any :any) :any)],
:pre nil}
{:p1 (pred-impl S M),
:p2 (pred-impl (conj S :list/A) M),
:conclusions [{:conclusion (pred-impl (conj :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(pred-impl :any :any) :and (pred-impl (conj :any :any) :any)],
:pre nil}
{:p1 (==> S M),
:p2 (==> (conj S :list/A) M),
:conclusions [{:conclusion (==> (conj :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(==> :any :any) :and (==> (conj :any :any) :any)],
:pre nil}
{:p1 (=|> S M),
:p2 (=|> (&| S :list/A) M),
:conclusions [{:conclusion (=|> (&| :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(=|> :any :any) :and (=|> (&| :any :any) :any)],
:pre nil}
{:p1 (retro-impl S M),
:p2 (retro-impl (conj S :list/A) M),
:conclusions [{:conclusion (retro-impl (conj :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(retro-impl :any :any) :and (retro-impl (conj :any :any) :any)],
:pre nil}
{:p1 (==> S M),
:p2 (==> (&| S :list/A) M),
:conclusions [{:conclusion (==> (&| :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(==> :any :any) :and (==> (&| :any :any) :any)],
:pre nil}
{:p1 (=|> S M),
:p2 (=|> (conj S :list/A) M),
:conclusions [{:conclusion (=|> (conj :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(=|> :any :any) :and (=|> (conj :any :any) :any)],
:pre nil}
{:p1 (pred-impl S M),
:p2 (pred-impl (&| S :list/A) M),
:conclusions [{:conclusion (pred-impl (&| :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(pred-impl :any :any) :and (pred-impl (&| :any :any) :any)],
:pre nil}
{:p1 (==> S M),
:p2 (==> (seq-conj S :list/A) M),
:conclusions [{:conclusion (==> (seq-conj :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(==> :any :any) :and (==> (seq-conj :any :any) :any)],
:pre nil}
{:p1 (retro-impl S M),
:p2 (retro-impl (&| S :list/A) M),
:conclusions [{:conclusion (retro-impl (&| :list/A) M),
:post (:t/decompose-npp
:order-for-all-same
:seq-interval-from-premises)}],
:full-path [(retro-impl :any :any) :and (retro-impl (&| :any :any) :any)],
:pre nil}))
(deftest test-generate-all-orders
(both-equal
res1 (generate-all-orders rule1)
res2 (generate-all-orders rule3)))
+212
View File
@@ -0,0 +1,212 @@
(ns nal.test.deriver.truth
(:require [nal.deriver.truth :refer :all]
[clojure.test :refer :all]
[nal.test.test-utils :refer [both-equal]]))
(deftest test-t-and
(both-equal
0.7 (t-and 1 0.7)
0.8 (t-and 1 1 0.8)
0.9 (t-and 1 1 1 0.9)))
(deftest test-t-or
(both-equal
0.94 (t-or 0.8 0.7)
1.0 (t-or 1 0.7)
1.0 (t-or 0.7 1)))
(deftest test-w2c
(both-equal
0.4117647058823529 (w2c 0.7)
0.16666666666666669 (w2c 0.2)))
(deftest test-c2w
(both-equal
0.7241379310344827 (c2w 0.42)
0.7241379310344827 (c2w 0.42)
0.20048019207683077 (c2w 0.167)))
(deftest test-conversion
(both-equal
[1 0.4736842105263158] (conversion [1 0.9] [1 0.9])
[1 0.4736842105263158] (conversion [0 0.9] [1 0.9])
[1 0.3333333333333333] (conversion [1 0.9] [0.6 0.5])))
(deftest test-negation
(both-equal
[0.0 0.9] (negation [1 0.9] [1 0.9])
[0.0 0.9] (negation [1 0.9] [0.6 0.9])
[0.6 0.6] (negation [0.4 0.6] [0.6 0.9])))
(deftest test-contraposition
(both-equal
[0 0.4736842105263158] (contraposition [1 0.9])
[0 0.4736842105263158] (contraposition [0 0.9])
[0 0.37499999999999994] (contraposition [0 0.6])))
(deftest test-revision
(both-equal
[1.0 0.9473684210526316] (revision [1 0.9] [1 0.9])
[0.95 0.9090909090909091] (revision [1 0.9] [0.5 0.5])
[0.95 0.9090909090909091] (revision [0.5 0.5] [1 0.9])))
(deftest tets-deduction
(both-equal
[1.0 0.81] (deduction [1 0.9] [1 0.9])
[1.0 0.45] (deduction [1 0.9] [1 0.5])
[1.0 0.45] (deduction [1 0.5] [1 0.9])))
(deftest test-analogy
(both-equal
[1.0 0.81] (analogy [1 0.9] [1 0.9])
[1.0 0.27] (analogy [1 0.9] [1 0.3])
[1.0 0.27] (analogy [1 0.3] [1 0.9])
[0.54 0.24300000000000002] (analogy [0.6 0.3] [0.9 0.9])))
(deftest test-resemblance
(both-equal
[1.0 0.81] (resemblance [1 0.9] [1 0.9])
[1.0 0.45] (resemblance [1 0.9] [1 0.5])
[1.0 0.45] (resemblance [1 0.5] [1 0.9])
[0.29700000000000004 0.5224799999999999] (resemblance [0.9 0.8] [0.33 0.7])))
(deftest test-abduction
(both-equal
[1 0.44751381215469616] (abduction [1 0.9] [1 0.9])
[1 0.35064935064935066] (abduction [1 0.9] [1 0.6])
[1 0.35064935064935066] (abduction [1 0.6] [1 0.9])
[0.67 0.10714285714285712] (abduction [0.67 0.6] [1 0.2])))
(deftest test-induction
(both-equal
[1 0.44751381215469616] (induction [1 0.9] [1 0.9])
[1 0.3103448275862069] (induction [1 0.9] [1 0.5])
[1 0.3103448275862069] (induction [1 0.9] [1 0.5])
[0.9 0.12280701754385964] (induction [0.4 0.7] [0.9 0.5])))
(deftest test-exemplification
(both-equal
[1 0.44751381215469616] (exemplification [1 0.9] [1 0.9])
[1 0.3865030674846626] (exemplification [1 0.9] [1 0.7])
[1 0.3865030674846626] (exemplification [1 0.7] [1 0.9])
[1 0.04928506236689991] (exemplification [0.8 0.2] [0.6 0.54])))
(deftest test-comparison
(both-equal
[1.0 0.44751381215469616] (comparison [1 0.9] [1 0.9])
[1.0 0.2647058823529412] (comparison [1 0.9] [1 0.4])
[1.0 0.2647058823529412] (comparison [1 0.4] [1 0.9])
[0.7346938775510206 0.07270029673590506] (comparison [0.8 0.2] [0.9 0.4])))
(deftest test-union
(both-equal
[1.0 0.81] (union [1 0.9] [1 0.9])
[1.0 0.18000000000000002] (union [1 0.9] [1 0.2])
[1.0 0.18000000000000002] (union [1 0.2] [1 0.9])
[0.94 0.08000000000000002] (union [0.7 0.4] [0.8 0.2])))
(deftest test-intersection
(both-equal
[1.0 0.81] (intersection [1 0.9] [1 0.9])
[1.0 0.36000000000000004] (intersection [1 0.9] [1 0.4])
[1.0 0.36000000000000004] (intersection [1 0.4] [1 0.9])
[0.522 0.18000000000000002] (intersection [0.87 0.9] [0.6 0.2])))
(deftest test-anonymous-analogy
(both-equal
[1.0 0.42631578947368426] (anonymous-analogy [1 0.9] [1 0.9])
[1.0 0.14210526315789473] (anonymous-analogy [1 0.9] [1 0.3])
[0.16000000000000003 0.04153846153846154] (anonymous-analogy [0.2 0.3] [0.8 0.9])))
(deftest test-decompose-pnn
(both-equal
[1.0 0.0] (decompose-pnn [1 0.9] [1 0.9])
[0.9 0.018] (decompose-pnn [1 0.9] [0.9 0.2])
[0.9 0.018] (decompose-pnn [1 0.2] [0.9 0.9])
[0.97 0.0053999999999999986] (decompose-pnn [0.3 0.2] [0.9 0.9])))
(deftest test-decompose-npp
(both-equal
[0.0 0.0] (decompose-npp [1 0.9] [1 0.9])
[0.63 0.1134] (decompose-npp [0.3 0.2] [0.9 0.9])
[0.029999999999999992 0.0053999999999999986] (decompose-npp [0.9 0.9] [0.3 0.2])))
(deftest test-decompose-pnp
(both-equal
[0.0 0.0] (decompose-pnp [1 0.9] [1 0.9])
[0.07999999999999999 0.0144] (decompose-pnp [0.4 0.9] [0.8 0.2])
[0.48 0.0864] (decompose-pnp [0.8 0.2] [0.4 0.9])))
(deftest test-decompose-ppp
(both-equal
[1.0 0.81] (decompose-ppp [1 0.9] [1 0.9])
[1.0 0.54] (decompose-ppp [1 0.9] [1 0.6])
[1.0 0.54] (decompose-ppp [1 0.6] [1 0.9])
[0.44 0.2376] (decompose-ppp [0.5 0.9] [0.88 0.6])))
(deftest test-decompose-nnn
(both-equal
[1.0 0.0] (decompose-nnn [1 0.9] [1 0.9])
[0.99 0.008099999999999996] (decompose-nnn [0.9 0.9] [0.9 0.9])
[0.52 0.3888] (decompose-nnn [0.2 0.9] [0.4 0.9])
[0.52 0.3888] (decompose-nnn [0.4 0.9] [0.2 0.9])))
(deftest test-difference
(both-equal
[0.0 0.81] (difference [1 0.9] [1 0.9])
[0.030000000000000006 0.552] (difference [0.1 0.92] [0.7 0.6])
[0.63 0.552] (difference [0.7 0.6] [0.1 0.92])))
(deftest test-structual-intersection
(both-equal
[1.0 0.81] (structual-intersection [1 0.9] [1 0.9])
[1.0 0.27] (structual-intersection [1 0.9] [1 0.3])
[1.0 0.81] (structual-intersection [1 0.3] [1 0.9])))
(deftest test-structual-deduction
(both-equal
[1.0 0.81] (structual-deduction [1 0.9] [1 0.9])
[0.4 0.09000000000000001] (structual-deduction [0.4 0.1] [1 0.9])
[1.0 0.54] (structual-deduction [1 0.6] [0.4 0.1])))
(deftest test-structual-abduction
(both-equal
[1 0.44751381215469616] (structual-abduction [1 0.9] [1 0.9])
[0.7 0.15254237288135597] (structual-abduction [0.7 0.2] [1 0.9])
[1 0.44751381215469616] (structual-abduction [1 0.9] [0.7 0.2])))
(deftest test-reduce-conjunction
(both-equal
[1.0 0.0] (reduce-conjunction [1 0.9] [1 0.9])
[0.7989999999999999 0.16281000000000004] (reduce-conjunction [0.7 0.9] [0.67 0.9])
[0.769 0.18710999999999997] (reduce-conjunction [0.67 0.9] [0.7 0.9])))
(deftest test-t-identity
(both-equal
[1 0.6] (t-identity [1 0.6] [1 0.7])
[0.3 0.6] (t-identity [0.3 0.6] [0.1 0.8])
[0.3 0.6] (t-identity [0.3 0.6] nil)))
(deftest test-belief-identity
(both-equal
[1 0.6] (belief-identity [1 0.6] [1 0.7])
[0.3 0.6] (belief-identity [0.3 0.6] [0.1 0.8]))
(is (nil? (belief-identity [0.3 0.6] nil))))
(deftest test-belief-structural-deduction
(both-equal
[1.0 0.81] (belief-structural-deduction [1 0.9] [1 0.9])
[0.3 0.81] (belief-structural-deduction [1 0.6] [0.3 0.9])
[0.6 0.20700000000000002] (belief-structural-deduction [0.9 0.67] [0.6 0.23])))
(deftest test-belief-structural-difference
(is (nil? (belief-structural-difference [1 0.9] nil)))
(both-equal
[0.5 0.27] (belief-structural-difference [0.9 0.9] [0.5 0.3])
[0.09999999999999998 0.81] (belief-structural-difference [0.5 0.3] [0.9 0.9])))
(deftest test-belief-negation
(both-equal
[0.0 0.9] (belief-negation [1 0.9] [1 0.9])
[0.8 0.1] (belief-negation [0.3 0] [0.2 0.1])
[0.7 0.9] (belief-negation [0.2 0.1] [0.3 0.9])))
-44
View File
@@ -1,44 +0,0 @@
(ns nal.test.nal1
(:refer-clojure :exclude [== reduce replace])
(:require [clojure.test :refer :all]
[nal.core :refer :all]
[clojure.core.logic :refer [run run*]]
[nal.test.test-utils :refer [trun*]]))
(deftest test-revision
(trun* [[nil [0.8 0.8333333333333334]]]
[R] (revision [nil [1 0.8]] [nil [0 0.5]] R)))
(deftest test-choice
(trun* [[nil [1 0.8]]]
[R] (choice [nil [1 0.8]] [nil [0 0.5]] R))
(trun* [[nil [0.8 0.9]]]
[R] (choice [nil [1 0.5]] [nil [0.8 0.9]] R)))
(deftest test-inference-nal1
;deduction
(trun* [[1 0.81]]
[q1] (inference ['(inheritance bird animal) [1 0.9]]
['(inheritance robin bird) [1 0.9]]
['(inheritance robin animal) q1]))
;induction
(trun* [[1 0.44751381215469616]]
[q1] (inference ['(inheritance robin animal) [1 0.9]]
['(inheritance robin bird) [1 0.9]]
['(inheritance bird animal) q1]))
;abduction
(trun* [[1 0.44751381215469616]]
[q1] (inference ['(inheritance bird animal) [1 0.9]]
['(inheritance robin animal) [1 0.9]]
['(inheritance robin bird) q1]))
;examplification
(trun* [[1 0.44751381215469616]]
[q1] (inference ['(inheritance robin bird) [1 0.9]]
['(inheritance bird animal) [1 0.9]]
['(inheritance animal robin) q1]))
;convension
(trun*
[[1 0.4186046511627907]]
[q]
(inference '((inheritance swan bird) [0.9 0.8])
['(inheritance bird swan) q])))
-70
View File
@@ -1,70 +0,0 @@
(ns nal.test.nal2
(:refer-clojure :exclude [== reduce replace])
(:require [clojure.test :refer :all]
[nal.core :refer :all]
[clojure.core.logic :refer [run run*]]
[nal.test.test-utils :refer [trun trun*]]))
(deftest test-inference-nal2
;inheritance to similarity
(trun* [[0.81 0.6400000000000001]]
[q]
(inference '((inheritance swan robin) [0.9 0.8])
'((inheritance robin swan) [0.9 0.8])
['(similarity swan robin) q]))
;comparison
(trun* [[1 0.44751381215469616]]
[q]
(inference ['(inheritance swan swimmer) [1 0.9]]
['(inheritance swan bird) [1 0.9]]
['(similarity bird swimmer) q]))
(trun* [[1 0.44751381215469616]]
[q]
(inference ['(inheritance sport competition) [1 0.9]]
['(inheritance chess competition) [1 0.9]]
['(similarity chess sport) q]))
;analogy
(trun* [[0.9 0.7290000000000001]]
[q]
(inference ['(inheritance swan swimmer) [1 0.9]]
['(similarity gull swan) [0.9 0.9]]
['(inheritance gull swimmer) q]))
(trun* [[0.9 0.7290000000000001]]
[q]
(inference ['(inheritance chess competition) [1 0.9]]
['(similarity sport competition) [0.9 0.9]]
['(inheritance chess sport) q]))
;resemblance
(trun* [[0.7200000000000001 0.7056000000000001]]
[q]
(inference ['(similarity swan robin) [0.8 0.9]]
['(similarity gull swan) [0.9 0.8]]
['(similarity gull robin) q]))
;instance and property
(trun* '([[ext-set [tweety]] bird [1 0.9]])
[S P T] (inference ['(instance tweety bird) [1 0.9]]
[['inheritance S P] T]))
(trun* '([raven [int-set [black]] [1 0.9]])
[S P T] (inference ['(property raven black) [1 0.9]]
[['inheritance S P] T]))
(trun* '([[ext-set [tweety]] [int-set [yellow]] [1 0.9]])
[S P T] (inference ['(inst-prop tweety yellow) [1 0.9]]
[['inheritance S P] T]))
;set definition
(trun* '([[ext-set [tweety]] [ext-set [birdie]] [1 0.8]])
[S P T] (inference ['(inheritance (ext-set [tweety]) (ext-set [birdie]))
[1 0.8]]
[['similarity S P] T]))
(trun* '([[int-set [smart]] [int-set [bright]] [1 0.8]])
[S P T] (inference ['(inheritance (int-set [smart]) (int-set [bright]))
[1 0.8]]
[['similarity S P] T]))
;structure transformation
(trun* [[1 0.9]]
[T]
(inference ['(similarity (ext-set [tweety]) (ext-set [birdie])) [1 0.9]]
['(similarity tweety birdie) T]))
(trun* [[0.8 0.9]]
[T]
(inference ['(similarity (ext-set [smart]) (ext-set [bright])) [0.8 0.9]]
['(similarity smart bright) T])))
-226
View File
@@ -1,226 +0,0 @@
(ns nal.test.nal3
(:refer-clojure :exclude [== reduce replace])
(:require [clojure.test :refer :all]
[nal.core :refer :all]
[clojure.core.logic :refer [run run*]]
[nal.test.test-utils :refer [trun trun*]]))
(deftest test-inference-nal3
;compound construction, two premises
(trun* '([[inheritance bird swimmer] [0.9 0.3932038834951457]]
[[similarity bird swimmer] [0.7346938775510204 0.4425242501951166]]
[[inheritance swan [ext-intersection [swimmer bird]]]
[0.7200000000000001 0.81]]
[[inheritance swan [int-intersection [swimmer bird]]]
[0.9800000000000001 0.81]]
[[inheritance swan [ext-difference swimmer bird]]
[0.17999999999999997 0.81]]
[[implication [inheritance _0 bird] [inheritance _0 swimmer]]
[0.9 0.3932038834951457]]
[[conjunction
[[inheritance [var _0 []] bird] [inheritance [var _0 []] swimmer]]]
[0.7200000000000001 0.81]]
[[equivalence [inheritance _0 bird] [inheritance _0 swimmer]]
[0.7346938775510204 0.4425242501951166]])
[R]
(inference ['(inheritance swan swimmer) [0.9 0.9]]
['(inheritance swan bird) [0.8 0.9]] R))
(trun* '([[inheritance chess sport] [0.8 0.42163100057836905]]
[[similarity chess sport] [0.7346938775510204 0.4425242501951166]]
[[inheritance [int-intersection [sport chess]] competition]
[0.7200000000000001 0.81]]
[[inheritance [ext-intersection [sport chess]] competition]
[0.9800000000000001 0.81]]
[[inheritance [int-difference sport chess] competition]
[0.17999999999999997 0.81]]
[[implication [inheritance sport _0] [inheritance chess _0]]
[0.8 0.42163100057836905]]
[[conjunction
[[inheritance chess [var _0 []]] [inheritance sport [var _0 []]]]]
[0.7200000000000001 0.81]]
[[equivalence [inheritance sport _0] [inheritance chess _0]]
[0.7346938775510204 0.4425242501951166]])
[R]
(inference ['(inheritance sport competition) [0.9 0.9]]
['(inheritance chess competition) [0.8 0.9]] R))
;compound construction, single premise
(trun* [[0.9 0.4444444444444445]] [V]
(inference ['(inheritance swan swimmer) [0.9 0.8]]
['(inheritance swan [ext-intersection [swimmer bird]]) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(inheritance swan swimmer) [0.9 0.8]]
['(inheritance swan (int-intersection [swimmer bird])) V]))
(trun* [[0.9 0.4444444444444445]] [V]
(inference ['(inheritance swan swimmer) [0.9 0.8]]
['(inheritance swan (ext-difference swimmer bird)) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(inheritance swan swimmer) [0.9 0.8]]
['(negation (inheritance swan (ext-difference bird swimmer))) V]))
(trun* [[0.9 0.4444444444444445]] [V]
(inference ['(inheritance sport competition) [0.9 0.8]]
['(inheritance (int-intersection [sport chess]) competition) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(inheritance sport competition) [0.9 0.8]]
['(inheritance (ext-intersection [sport chess]) competition) V]))
(trun* [[0.9 0.4444444444444445]] [V]
(inference ['(inheritance sport competition) [0.9 0.8]]
['(inheritance (int-difference sport chess) competition) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(inheritance sport competition) [0.9 0.8]]
['(negation (inheritance (int-difference chess sport) competition)) V]))
;compound destruction, single premise
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(inheritance swan (ext-intersection [swimmer bird])) [0.9 0.8]]
['(inheritance swan swimmer) V]))
(trun* [[0.9 0.4444444444444445]] [V]
(inference ['(inheritance swan (int-intersection [swimmer bird])) [0.9 0.8]]
['(inheritance swan swimmer) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(inheritance swan (ext-difference swimmer bird)) [0.9 0.8]]
['(inheritance swan swimmer) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(inheritance swan (ext-difference swimmer bird)) [0.9 0.8]]
['(negation (inheritance swan bird)) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(inheritance (int-intersection [sport chess]) competition) [0.9 0.8]]
['(inheritance sport competition) V]))
(trun* [[0.9 0.4444444444444445]] [V]
(inference ['(inheritance (ext-intersection [sport chess]) competition) [0.9 0.8]]
['(inheritance sport competition) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(inheritance (int-difference sport chess) competition) [0.9 0.8]]
['(inheritance sport competition) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(inheritance (int-difference sport chess) competition) [0.9 0.8]]
['(negation (inheritance chess competition)) V]))
;operation on both sides of a relation
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(inheritance bird animal) [0.9 0.8]]
['(inheritance (ext-intersection [swimmer bird])
(ext-intersection [swimmer animal])) V]))
(trun* [[0.9 0.4444444444444445]] [V]
(inference ['(inheritance (ext-intersection [swimmer bird])
(ext-intersection [swimmer animal])) [0.9 0.8]]
['(inheritance bird animal) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(inheritance bird animal) [0.9 0.8]]
['(inheritance (int-intersection [swimmer bird])
(int-intersection [swimmer animal])) V]))
(trun* [[0.9 0.4444444444444445]] [V]
(inference ['(inheritance (int-intersection [swimmer bird])
(int-intersection [swimmer animal])) [0.9 0.8]]
['(inheritance bird animal) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(similarity bird animal) [0.9 0.8]]
['(similarity (ext-intersection [swimmer bird])
(ext-intersection [swimmer animal])) V]))
(trun* [[0.9 0.4444444444444445]] [V]
(inference ['(similarity (ext-intersection [swimmer bird])
(ext-intersection [swimmer animal])) [0.9 0.8]]
['(similarity bird animal) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(similarity bird animal) [0.9 0.8]]
['(similarity (int-intersection [swimmer bird])
(int-intersection [swimmer animal])) V]))
(trun* [[0.9 0.4444444444444445]] [V]
(inference ['(similarity (int-intersection [swimmer bird])
(int-intersection [swimmer animal])) [0.9 0.8]]
['(similarity bird animal) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(inheritance bird animal) [0.9 0.8]]
['(inheritance (ext-difference bird swimmer)
(ext-difference animal swimmer)) V]))
(trun* [[0.9 0.4444444444444445]] [V]
(inference ['(inheritance (ext-difference bird swimmer)
(ext-difference animal swimmer)) [0.9 0.8]]
['(inheritance bird animal) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(inheritance bird animal) [0.9 0.8]]
['(inheritance (int-difference bird swimmer)
(int-difference animal swimmer)) V]))
(trun* [[0.9 0.4444444444444445]] [V]
(inference ['(inheritance (int-difference bird swimmer)
(int-difference animal swimmer)) [0.9 0.8]]
['(inheritance bird animal) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(similarity bird animal) [0.9 0.8]]
['(similarity (ext-difference bird swimmer)
(ext-difference animal swimmer)) V]))
(trun* [[0.9 0.4444444444444445]] [V]
(inference ['(similarity (ext-difference bird swimmer)
(ext-difference animal swimmer)) [0.9 0.8]]
['(similarity bird animal) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(similarity bird animal) [0.9 0.8]]
['(similarity (int-difference bird swimmer)
(int-difference animal swimmer)) V]))
(trun* [[0.9 0.4444444444444445]] [V]
(inference ['(similarity (int-difference bird swimmer)
(int-difference animal swimmer)) [0.9 0.8]]
['(similarity bird animal) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(inheritance bird animal) [0.9 0.8]]
['(inheritance (ext-difference swimmer animal)
(ext-difference swimmer bird)) V]))
(trun* [[0.9 0.4444444444444445]] [V]
(inference ['(inheritance (ext-difference swimmer animal)
(ext-difference swimmer bird)) [0.9 0.8]]
['(inheritance bird animal) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(inheritance bird animal) [0.9 0.8]]
['(inheritance (int-difference swimmer animal)
(int-difference swimmer bird)) V]))
(trun* [[0.9 0.4444444444444445]] [V]
(inference ['(inheritance (int-difference swimmer animal)
(int-difference swimmer bird)) [0.9 0.8]]
['(inheritance bird animal) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(similarity bird animal) [0.9 0.8]]
['(similarity (ext-difference swimmer animal)
(ext-difference swimmer bird)) V]))
(trun* [[0.9 0.4444444444444445]] [V]
(inference ['(similarity (ext-difference swimmer animal)
(ext-difference swimmer bird)) [0.9 0.8]]
['(similarity bird animal) V]))
(trun* [[0.9 0.7200000000000001]] [V]
(inference ['(similarity bird animal) [0.9 0.8]]
['(similarity (int-difference swimmer animal)
(int-difference swimmer bird)) V]))
(trun* [[0.9 0.4444444444444445]] [V]
(inference ['(similarity (int-difference swimmer animal)
(int-difference swimmer bird)) [0.9 0.8]]
['(similarity bird animal) V]))
;set operations
(trun*
'([[inheritance (ext-set [pluto saturn]) (ext-set [venus mars pluto])] [0.9 0.3093922651933702]]
[[similarity (ext-set [pluto saturn]) (ext-set [venus mars pluto])] [0.6494845360824741 0.38302073050345514]]
[[inheritance (ext-set [earth]) [ext-set (pluto)]] [0.63 0.6400000000000001]]
[[inheritance (ext-set [earth]) [ext-set (venus mars pluto saturn)]] [0.9700000000000001 0.6400000000000001]]
[[inheritance (ext-set [earth]) [ext-set (venus mars)]] [0.2700000000000001 0.6400000000000001]]
[[implication [inheritance _0 (ext-set [pluto saturn])] [inheritance _0 (ext-set [venus mars pluto])]] [0.9 0.3093922651933702]]
[[conjunction [[inheritance [var _0 []] (ext-set [pluto saturn])] [inheritance [var _0 []] (ext-set [venus mars pluto])]]] [0.63 0.6400000000000001]]
[[equivalence [inheritance _0 (ext-set [pluto saturn])] [inheritance _0 (ext-set [venus mars pluto])]] [0.6494845360824741 0.38302073050345514]])
[R]
(inference ['(inheritance (ext-set [earth])
(ext-set [venus mars pluto])) [0.9 0.8]]
['(inheritance (ext-set [earth])
(ext-set [pluto saturn])) [0.7 0.8]] R))
(trun*
'([[inheritance (int-set [purple green]) (int-set [red green blue])] [0.7 0.36548223350253817]]
[[similarity (int-set [purple green]) (int-set [red green blue])] [0.6494845360824741 0.38302073050345514]]
[[inheritance [int-set (green)] (int-set [colorful])] [0.63 0.6400000000000001]]
[[inheritance [int-set (red blue purple green)] (int-set [colorful])] [0.9700000000000001 0.6400000000000001]]
[[inheritance [int-set (red blue)] (int-set [colorful])] [0.2700000000000001 0.6400000000000001]]
[[implication [inheritance (int-set [red green blue]) _0] [inheritance (int-set [purple green]) _0]] [0.7 0.36548223350253817]]
[[conjunction [[inheritance (int-set [purple green]) [var _0 []]] [inheritance (int-set [red green blue]) [var _0 []]]]] [0.63 0.6400000000000001]]
[[equivalence [inheritance (int-set [red green blue]) _0] [inheritance (int-set [purple green]) _0]] [0.6494845360824741 0.38302073050345514]])
[R]
(inference ['(inheritance (int-set [red green blue]) (int-set [colorful]))
[0.9 0.8]]
['(inheritance (int-set [purple green]) (int-set [colorful]))
[0.7 0.8]] R)))
-100
View File
@@ -1,100 +0,0 @@
(ns nal.test.nal4
(:refer-clojure :exclude [== reduce replace])
(:require [clojure.test :refer :all]
[nal.core :refer :all]
[clojure.core.logic :refer [run run*]]
[nal.test.test-utils :refer [trun trun*]]))
(deftest test-inference-nal4
;extensional image
(trun*
'([[inheritance reaction (product [acid base])] [1 0.4736842105263158]]
[[inheritance acid [ext-image reaction (nil base)]] [1 0.9]]
[[inheritance base [ext-image reaction (acid nil)]] [1 0.9]])
[C]
(inference ['(inheritance (product [acid base]) reaction)
[1 0.9]] C))
(trun*
'([[inheritance (ext-image reaction [nil base]) acid] [1 0.4736842105263158]]
[[inheritance [product (acid base)] reaction] [1 0.9]])
[C]
(inference ['(inheritance acid (ext-image reaction [nil base]))
[1 0.9]] C))
(trun*
'([[inheritance (ext-image reaction [acid nil]) acid] [1 0.4736842105263158]]
[[inheritance [product (acid acid)] reaction] [1 0.9]])
[C]
(inference ['(inheritance acid (ext-image reaction [acid nil]))
[1 0.9]] C))
;intensional image
(trun*
'([[inheritance (product [acid base]) neutralization] [1 0.4736842105263158]]
[[inheritance [int-image neutralization (nil base)] acid] [1 0.9]]
[[inheritance [int-image neutralization (acid nil)] base] [1 0.9]])
[C]
(inference ['(inheritance neutralization, (product [acid, base])),
[1, 0.9]], C))
(trun*
'([[inheritance acid (int-image neutralization [nil base])]
[1 0.4736842105263158]]
[[inheritance neutralization [product (acid base)]] [1 0.9]])
[C]
(inference ['(inheritance (int-image neutralization, [nil, base]), acid),
[1, 0.9]], C))
(trun*
'([[inheritance base (int-image neutralization [acid nil])]
[1 0.4736842105263158]]
[[inheritance neutralization [product (acid base)]] [1 0.9]])
[C]
(inference ['(inheritance (int-image neutralization, [acid, nil]), base),
[1, 0.9]], C))
;operation on both sides of a relation
(trun* '([0.9 0.8] [0.9 0.8]) [V]
(inference ['(inheritance bird animal) [0.9 0.8]]
['(inheritance (product [bird plant])
(product [animal plant])) V]))
(trun* '([0.9 0.8] [0.9 0.8]) [V]
(inference ['(inheritance (product [plant bird]) (product [plant animal]))
[0.9 0.8]]
['(inheritance bird animal) V]))
(trun* '([0.9 0.7200000000000001]) [V]
(inference ['(inheritance neutralization reaction) [0.9 0.8]]
['(inheritance (ext-image neutralization [acid nil])
(ext-image reaction [acid nil])) V]))
(trun* '([0.9 0.4444444444444445]) [V]
(inference ['(inheritance (ext-image neutralization [acid nil])
(ext-image reaction [acid nil])) [0.9 0.8]]
['(inheritance neutralization reaction) V]))
(trun* '([0.9 0.7200000000000001]) [V]
(inference ['(inheritance neutralization reaction) [0.9 0.8]]
['(inheritance (int-image neutralization [acid nil])
(int-image reaction [acid nil])) V]))
(trun* '([0.9 0.4444444444444445]) [V]
(inference ['(inheritance (int-image neutralization [acid nil])
(int-image reaction [acid nil])) [0.9 0.8]]
['(inheritance neutralization reaction) V]))
(trun* '([0.9 0.7200000000000001]) [V]
(inference ['(inheritance soda base) [0.9 0.8]]
['(inheritance (ext-image reaction [nil base])
(ext-image reaction [nil soda])) V]))
(trun* '([0.9 0.4444444444444445]) [V]
(inference ['(inheritance (ext-image reaction [nil base])
(ext-image reaction [nil soda])) [0.9 0.8]]
['(inheritance soda base) V]))
(trun* '([0.9 0.7200000000000001]) [V]
(inference ['(inheritance soda base) [0.9 0.8]]
['(inheritance (int-image neutralization [nil base])
(int-image neutralization [nil soda])) V]))
(trun* '([0.9 0.4444444444444445]) [V]
(inference ['(inheritance (int-image neutralization [nil base])
(int-image neutralization [nil soda])) [0.9 0.8]]
['(inheritance soda base) V])))
-302
View File
@@ -1,302 +0,0 @@
(ns nal.test.nal5
(:refer-clojure :exclude [== reduce replace])
(:require [clojure.test :refer :all]
[nal.core :refer :all]
[clojure.core.logic :refer [run run*]]
[nal.test.test-utils :refer [trun trun*]]))
(deftest test-inference-nal5
;revision
(trun*
'([(implication (inheritance robin flyer) (inheritance robin bird))
[0.8 0.8333333333333334]])
[R]
(revision ['(implication (inheritance robin, flyer), (inheritance robin, bird)), [1, 0.8]],
['(implication (inheritance robin, flyer), (inheritance robin, bird)), [0, 0.5]], R))
(trun*
'([(equivalence (inheritance robin flyer) inheritance (robin bird))
[0.8 0.8333333333333334]])
[R]
(revision ['(equivalence (inheritance robin, flyer), inheritance (robin, bird)), [1, 0.8]],
['(equivalence (inheritance robin, flyer), inheritance (robin, bird)), [0, 0.5]], R))
;choice
(trun*
'([(implication (inheritance robin flyer) (inheritance robin bird)) [1 0.8]])
[R]
(choice ['(implication (inheritance robin, flyer), (inheritance robin, bird)), [1, 0.8]],
['(implication (inheritance robin, flyer), inheritance (robin, bird)), [0, 0.5]], R))
(trun*
'([(implication (inheritance robin flyer) (inheritance robin bird)) [0.8 0.9]])
[R]
(choice ['(implication (inheritance robin, flyer), (inheritance robin, bird)), [0.8, 0.9]],
['(implication (inheritance robin, swimmer), (inheritance robin, bird)), [1, 0.5]], R))
; deduction
(trun* '([[implication (inheritance robin flyer) (inheritance robin animal)]
[0.9 0.36000000000000004]])
[R]
(inference ['(implication (inheritance robin bird) (inheritance robin animal)) [0.9 0.8]]
['(implication (inheritance robin flyer) (inheritance robin bird)) [1 0.5]] R))
(trun* '([[equivalence (inheritance robin flyer) (inheritance robin animal)]
[0.9 0.39999999999999997]])
[R]
(inference ['(equivalence (inheritance robin bird) (inheritance robin animal)) [0.9 0.8]]
['(equivalence (inheritance robin flyer) (inheritance robin bird)) [1 0.5]] R))
;induction
(trun* '([[implication (inheritance robin flyer) (inheritance robin animal)]
[0.9 0.28571428571428575]]
[[equivalence (inheritance robin flyer) (inheritance robin animal)]
[0.9000000000000001 0.2857142857142857]]
[[implication
(inheritance robin bird)
[conjunction [(inheritance robin animal) (inheritance robin flyer)]]]
[0.9 0.4]]
[[implication
(inheritance robin bird)
[disjunction [(inheritance robin animal) (inheritance robin flyer)]]]
[0.9999999999999999 0.4]])
[R]
(inference ['(implication (inheritance robin bird) (inheritance robin animal)) [0.9 0.8]]
['(implication (inheritance robin bird) (inheritance robin flyer)) [1 0.5]] R))
;abduction
(trun* '([[implication (inheritance robin flyer) (inheritance robin bird)]
[1 0.2647058823529412]]
[[equivalence (inheritance robin flyer) (inheritance robin bird)]
[0.9000000000000001 0.2857142857142857]]
[[implication
[disjunction [(inheritance robin bird) (inheritance robin flyer)]]
(inheritance robin animal)]
[0.9 0.4]]
[[implication
[conjunction [(inheritance robin bird) (inheritance robin flyer)]]
(inheritance robin animal)]
[0.9999999999999999 0.4]])
[R]
(inference ['(implication (inheritance robin bird) (inheritance robin animal)) [0.9 0.8]]
['(implication (inheritance robin flyer) (inheritance robin animal)) [1 0.5]] R))
;examplification
(trun* '([[implication (inheritance robin animal) (inheritance robin flyer)]
[1 0.2647058823529412]])
[R]
(inference ['(implication (inheritance robin flyer) (inheritance robin bird)) [0.9 0.8]]
['(implication (inheritance robin bird) (inheritance robin animal)) [1 0.5]] R))
;convension NAL-5
(trun* '([[implication (inheritance robin animal) (inheritance robin flyer)]
[1 0.4186046511627907]])
[R]
(inference ['(implication (inheritance robin flyer) (inheritance robin animal))
[0.9 0.8]] R))
(trun* '([0.9 0.7200000000000001]) [V]
(inference ['(equivalence (inheritance robin flyer) (inheritance robin bird)) [0.9 0.8]]
['(implication (inheritance robin flyer) (inheritance robin bird)) V]))
(trun* '([[equivalence (inheritance robin flyer) (inheritance robin bird)]
[0.81 0.6400000000000001]]) [R]
(inference ['(implication (inheritance robin flyer) (inheritance robin bird)) [0.9 0.8]]
['(implication (inheritance robin bird) (inheritance robin flyer)) [0.9 0.8]] R))
(trun* '([0.9 0.7200000000000001]) [V]
(inference ['(similarity swan bird) [0.9 0.8]] ['(inheritance swan bird) V]))
(trun* '([0.9 0.4444444444444445]) [V]
(inference ['(inheritance swan bird) [0.9 0.8]] ['(similarity swan bird) V]))
(trun* '([[similarity swan bird] [0.1 0.6400000000000001]]) [R]
(inference ['(inheritance swan bird) [1 0.8]] ['(inheritance bird swan) [0.1 0.8]] R))
;comparison
(trun*
'([(inheritance robin flyer) (inheritance robin animal) [0.8181818181818182 0.3878550440744369]])
[A B V]
(inference ['(implication (inheritance robin bird) (inheritance robin animal)) [0.9 0.8]]
['(implication (inheritance robin bird) (inheritance robin flyer)) [0.9 0.8]]
[['equivalence A B] V]))
(trun*
'([(inheritance robin flyer) (inheritance robin bird) [0.8181818181818182 0.3878550440744369]])
[A B V]
(inference ['(implication (inheritance robin bird) (inheritance robin animal)) [0.9 0.8]]
['(implication (inheritance robin flyer) (inheritance robin animal)) [0.9 0.8]] [['equivalence A B] V]))
; analogy
(trun* '([[implication (inheritance robin bird) (inheritance robin flyer)] [0.81 0.5760000000000001]]) [R]
(inference ['(implication (inheritance robin bird) (inheritance robin animal)) [0.9 0.8]] ['(equivalence (inheritance robin flyer) (inheritance robin animal)) [0.9 0.8]] R))
(trun* '([[implication (inheritance robin flyer) (inheritance robin animal)] [0.81 0.5760000000000001]]) [R]
(inference ['(implication (inheritance robin bird) (inheritance robin animal)) [0.9 0.8]] ['(equivalence (inheritance robin flyer) (inheritance robin bird)) [0.9 0.8]] R))
; compound construction two premises
(trun* '([[implication (inheritance robin flyer) (inheritance robin animal)] [0.9 0.36548223350253817]]
[[equivalence (inheritance robin flyer) (inheritance robin animal)] [0.8181818181818182 0.3878550440744369]]
[[implication (inheritance robin bird) [conjunction [(inheritance robin animal) (inheritance robin flyer)]]] [0.81 0.6400000000000001]]
[[implication (inheritance robin bird) [disjunction [(inheritance robin animal) (inheritance robin flyer)]]] [0.99 0.6400000000000001]])
[R]
(inference ['(implication (inheritance robin bird) (inheritance robin animal)) [0.9 0.8]] ['(implication (inheritance robin bird) (inheritance robin flyer)) [0.9 0.8]] R))
(trun* '([[implication (inheritance robin flyer) (inheritance robin bird)] [0.9 0.36548223350253817]]
[[equivalence (inheritance robin flyer) (inheritance robin bird)] [0.8181818181818182 0.3878550440744369]]
[[implication [disjunction [(inheritance robin bird) (inheritance robin flyer)]] (inheritance robin animal)] [0.81 0.6400000000000001]]
[[implication [conjunction [(inheritance robin bird) (inheritance robin flyer)]] (inheritance robin animal)] [0.99 0.6400000000000001]])
[R]
(inference ['(implication (inheritance robin bird) (inheritance robin animal)) [0.9 0.8]]
['(implication (inheritance robin flyer) (inheritance robin animal)) [0.9 0.8]] R))
(trun* '([0.81 0.81]) [V]
(inference ['(inheritance robin animal) [0.9 0.9]] ['(inheritance robin flyer) [0.9 0.9]]
['(conjunction [(inheritance robin animal) (inheritance robin flyer)]) V]))
(trun* '([0.99 0.6400000000000001]) [V]
(inference ['(inheritance robin animal) [0.9 0.8]] ['(inheritance robin flyer) [0.9 0.8]]
['(disjunction [(inheritance robin animal) (inheritance robin flyer)]) V]))
; compound construction single premise
(trun* '([0.9 0.4444444444444445]) [V]
(inference ['(implication (inheritance robin bird) (inheritance robin animal)) [0.9 0.8]]
['(implication (inheritance robin bird) (conjunction [(inheritance robin animal) (inheritance robin flyer)])) V]))
(trun* '([0.9 0.7200000000000001]) [V]
(inference ['(implication (inheritance robin bird) (inheritance robin animal)) [0.9 0.8]]
['(implication (inheritance robin bird) (disjunction [(inheritance robin animal) (inheritance robin flyer)])) V]))
(trun* '([0.9 0.4444444444444445]) [V]
(inference ['(implication (inheritance robin bird) (inheritance robin animal)) [0.9 0.8]]
['(implication (disjunction [(inheritance robin bird) (inheritance robin flyer)]) (inheritance robin animal)) V]))
(trun* '([0.9 0.7200000000000001]) [V]
(inference ['(implication (inheritance robin bird) (inheritance robin animal)) [0.9 0.8]]
['(implication (conjunction [(inheritance robin bird) (inheritance robin flyer)]) (inheritance robin animal)) V]))
(trun* '([0.9 0.4444444444444445]) [V]
(inference ['(inheritance robin animal) [0.9 0.8]] ['(conjunction [(inheritance robin animal) (inheritance robin flyer)]) V]))
(trun* '([0.9 0.7200000000000001]) [V]
(inference ['(inheritance robin animal) [0.9 0.8]] ['(disjunction [(inheritance robin animal) (inheritance robin flyer)]) V]))
; compound destruction two premises
(trun* '([0 0.6400000000000001]) [T]
(inference ['(implication (inheritance robin bird) (inheritance robin flyer)) [1 0.8]]
['(implication (inheritance robin bird) (conjunction [(inheritance robin animal) (inheritance robin flyer)])) [0 0.8]]
['(implication (inheritance robin bird) (inheritance robin animal)) T]))
(trun* '([1 0.6400000000000001]) [T]
(inference ['(implication (inheritance robin bird) (inheritance robin flyer)) [0 0.8]]
['(implication (inheritance robin bird) (disjunction [(inheritance robin animal) (inheritance robin flyer)])) [1 0.8]]
['(implication (inheritance robin bird) (inheritance robin animal)) T]))
(trun* '([0 0.6400000000000001]) [T]
(inference ['(implication (inheritance robin bird) (inheritance robin animal)) [1 0.8]]
['(implication (disjunction [(inheritance robin bird) (inheritance robin flyer)]) (inheritance robin animal)) [0 0.8]]
['(implication (inheritance robin flyer) (inheritance robin animal)) T]))
(trun* '([1 0.6400000000000001]) [T]
(inference ['(implication (inheritance robin bird) (inheritance robin animal)) [0 0.8]]
['(implication (conjunction [(inheritance robin bird) (inheritance robin flyer)]) (inheritance robin animal)) [1 0.8]]
['(implication (inheritance robin flyer) (inheritance robin animal)) T]))
(trun* '([(inheritance robin flyer) [0 0.6400000000000001]])
[R]
(inference ['(inheritance robin bird) [1 0.8]]
['(conjunction [(inheritance robin bird) (inheritance robin flyer)]) [0 0.8]] R))
(trun* '([(inheritance robin flyer) [1 0.6400000000000001]])
[R]
(inference ['(inheritance robin bird) [0 0.8]]
['(disjunction [(inheritance robin bird) (inheritance robin flyer)]) [1 0.8]] R))
; compound destruction single premise
(trun* '([0.9 0.7200000000000001]) [V]
(inference ['(implication (inheritance robin bird) (conjunction [(inheritance robin animal) (inheritance robin flyer)])) [0.9 0.8]]
['(implication (inheritance robin bird) (inheritance robin animal)) V]))
(trun* '([0.9 0.4444444444444445]) [V]
(inference ['(implication (inheritance robin bird) (disjunction [(inheritance robin animal) (inheritance robin flyer)])) [0.9 0.8]]
['(implication (inheritance robin bird) (inheritance robin animal)) V]))
(trun* '([0.9 0.7200000000000001]) [V]
(inference ['(implication (disjunction [(inheritance robin bird) (inheritance robin flyer)]) (inheritance robin animal)) [0.9 0.8]]
['(implication (inheritance robin bird) (inheritance robin animal)) V]))
(trun* '([0.9 0.4444444444444445]) [V]
(inference ['(implication (conjunction [(inheritance robin bird) (inheritance robin flyer)]) (inheritance robin animal)) [0.9 0.8]]
['(implication (inheritance robin bird) (inheritance robin animal)) V]))
(trun* '([0.9 0.7200000000000001]) [V]
(inference ['(conjunction [(inheritance robin bird) (inheritance robin flyer)]) [0.9 0.8]] ['(inheritance robin bird) V]))
(trun* '([0.9 0.4444444444444445]) [V]
(inference ['(disjunction [(inheritance robin bird) (inheritance robin flyer)]) [0.9 0.8]] ['(inheritance robin bird) V]))
; operation on both sides of a relation
(trun* '([0.9 0.7200000000000001]) [V]
(inference ['(implication p q) [0.9 0.8]] ['(implication (conjunction [p r]) (conjunction [q r])) V]))
(trun* '([0.9 0.4444444444444445]) [V]
(inference ['(implication (conjunction [p r]) (conjunction [q r])) [0.9 0.8]] ['(implication p q) V]))
(trun* '([0.9 0.7200000000000001]) [V]
(inference ['(implication p q) [0.9 0.8]] ['(implication (disjunction [p r]) (disjunction [q r])) V]))
(trun* '([0.9 0.4444444444444445]) [V]
(inference ['(implication (disjunction [p r]) (disjunction [q r])) [0.9 0.8]] ['(implication p q) V]))
(trun* '([0.9 0.7200000000000001]) [V]
(inference ['(equivalence p q) [0.9 0.8]] ['(equivalence (conjunction [p r]) (conjunction [q r])) V]))
(trun* '([0.9 0.4444444444444445]) [V]
(inference ['(equivalence (conjunction [p r]) (conjunction [q r])) [0.9 0.8]] ['(equivalence p q) V]))
(trun* '([0.9 0.7200000000000001]) [V]
(inference ['(equivalence p q) [0.9 0.8]] ['(equivalence (disjunction [p r]) (disjunction [q r])) V]))
(trun* '([0.9 0.4444444444444445]) [V]
(inference ['(equivalence (disjunction [p r]) (disjunction [q r])) [0.9 0.8]] ['(equivalence p q) V]))
; negation
(trun* '([(inheritance robin bird) [0.09999999999999998 0.8]]) [R]
(inference ['(negation (inheritance robin bird)) [0.9 0.8]] R))
(trun* '([0.8 0.8]) [T]
(inference ['(inheritance robin bird) [0.2 0.8]]
['(negation (inheritance robin bird)) T]))
(trun* '([0 0.4186046511627907]) [T]
(inference ['(implication (negation (inheritance penguin flyer)) (inheritance penguin swimmer)) [0.1 0.8]]
['(implication (negation (inheritance penguin swimmer)) (inheritance penguin flyer)) T]))
; conditional inference
(trun* '([(inheritance robin animal) [0.9 0.36000000000000004]]) [R]
(inference ['(implication (inheritance robin bird) (inheritance robin animal)) [0.9 0.8]]
['(inheritance robin bird) [1 0.5]] R))
(trun* '([(inheritance robin bird) [1 0.2647058823529412]]) [R]
(inference ['(implication (inheritance robin bird) (inheritance robin animal)) [0.9 0.8]]
['(inheritance robin animal) [1 0.5]] R))
(trun* '([0.9 0.28571428571428575]
[0.9 0.28571428571428575]) [V]
(inference ['(inheritance robin animal) [0.9 0.8]]
['(inheritance robin flyer) [1 0.5]]
['(implication (inheritance robin flyer) (inheritance robin animal)) V]))
(trun* '([(inheritance robin flyer) [0.9 0.36000000000000004]]) [R]
(inference ['(inheritance robin animal) [1 0.5]]
['(equivalence (inheritance robin flyer) (inheritance robin animal)) [0.9 0.8]] R))
(trun* '([0.9000000000000001 0.2857142857142857]
[0.9000000000000001 0.2857142857142857]) [V]
(inference ['(inheritance robin animal) [0.9 0.8]]
['(inheritance robin flyer) [1 0.5]]
['(equivalence (inheritance robin flyer) (inheritance robin animal)) V]))
(trun* '([[implication [conjunction (a1 a3)] c] [0.81 0.6561000000000001]]) [R]
(inference ['(implication (conjunction [a1 a2 a3]) c) [0.9 0.9]] ['a2 [0.9 0.9]] R))
(trun* '([0.9 0.42163100057836905]) [V]
(inference ['(implication (conjunction [a1 a2 a3]) c) [0.9 0.9]]
['(implication (conjunction [a1 a3]) c) [0.9 0.9]] ['a2 V]))
(trun* '([0.9 0.42163100057836905]) [V]
(inference ['(implication (conjunction [a1 a3]) c) [0.9 0.9]] ['a2 [0.9 0.9]]
['(implication (conjunction [a2 a1 a3]) c) V]))
(trun* '([[implication [conjunction (a1 b2 a3)] c]
[0.81 0.6561000000000001]]) [R]
(inference ['(implication (conjunction [a1 a2 a3]) c) [0.9 0.9]]
['(implication b2 a2) [0.9 0.9]] R))
(trun* '([0.9 0.42163100057836905]) [V]
(inference ['(implication (conjunction [a1 a2 a3]) c) [0.9 0.9]]
['(implication (conjunction [a1 b2 a3]) c) [0.9 0.9]]
['(implication b2 a2) V]))
(trun* '([[implication [conjunction (a1 a2 a3)] c] [0.9 0.42163100057836905]]) [R]
(inference ['(implication (conjunction [a1 b2 a3]) c) [0.9 0.9]]
['(implication b2 a2) [0.9 0.9]] R)))
-134
View File
@@ -1,134 +0,0 @@
(ns nal.test.nal6
(:refer-clojure :exclude [== reduce replace])
(:require [clojure.test :refer :all]
[nal.core :refer :all]
[clojure.core.logic :refer [run run* fresh]]
[nal.test.test-utils :refer [trun trun*]]))
(deftest test-inference-nal6
; variable unification
(trun* '([[implication [inheritance _0 bird] [inheritance _0 flyer]] [0.9200000000000002 0.8333333333333334]])
[R]
(fresh [X Y]
(revision [['implication ['inheritance X 'bird] ['inheritance X 'flyer]] [0.9 0.8]] [['implication ['inheritance Y 'bird] ['inheritance Y 'flyer]] [1 0.5]] R)))
(trun* '([[implication [inheritance _0 robin] [inheritance _0 animal]] [1 0.81]])
[R]
(fresh [X Y]
(inference [['implication ['inheritance X 'bird] ['inheritance X 'animal]] [1 0.9]] [['implication ['inheritance Y 'robin] ['inheritance Y 'bird]] [1 0.9]] R)))
(trun* '([[implication [inheritance _0 robin] [inheritance _0 bird]] [1 0.44751381215469616]]
[[equivalence [inheritance _0 robin] [inheritance _0 bird]] [1 0.44751381215469616]]
[[implication [disjunction [[inheritance _0 bird] [inheritance _0 robin]]] [inheritance _0 animal]] [1 0.81]]
[[implication [conjunction [[inheritance _0 bird] [inheritance _0 robin]]] [inheritance _0 animal]] [1 0.81]])
[R]
(fresh [X Y]
(inference [['implication ['inheritance X 'bird] ['inheritance X 'animal]] [1 0.9]] [['implication ['inheritance Y 'robin] ['inheritance Y 'animal]] [1 0.9]] R)))
(trun* '([[implication [inheritance _0 bird] [inheritance _0 animal]] [1 0.44751381215469616]]
[[equivalence [inheritance _0 bird] [inheritance _0 animal]] [1 0.44751381215469616]]
[[implication [inheritance _0 robin] [conjunction [[inheritance _0 animal] [inheritance _0 bird]]]] [1 0.81]]
[[implication [inheritance _0 robin] [disjunction [[inheritance _0 animal] [inheritance _0 bird]]]] [1 0.81]])
[R]
(fresh [X Y]
(inference [['implication ['inheritance X 'robin] ['inheritance X 'animal]] [1 0.9]] [['implication ['inheritance Y 'robin] ['inheritance Y 'bird]] [1 0.9]] R)))
(trun* '([[implication [inheritance _0 feathered] [inheritance _0 flyer]] [1 0.81]])
[R]
(fresh [X Y]
(inference [['implication ['inheritance X 'feathered] ['inheritance X 'bird]] [1 0.9]] [['equivalence ['inheritance Y 'flyer] ['inheritance Y 'bird]] [1 0.9]] R)))
(trun* '([[implication [inheritance _0 bird] [inheritance _0 flyer]] [1 0.44751381215469616]]
[[equivalence [inheritance _0 bird] [inheritance _0 flyer]] [1 0.44751381215469616]]
[[implication [inheritance _0 feathered] [conjunction [[inheritance _0 flyer] [inheritance _0 bird]]]] [1 0.81]]
[[implication [inheritance _0 feathered] [disjunction [[inheritance _0 flyer] [inheritance _0 bird]]]] [1 0.81]])
[R]
(fresh [X Y]
(inference [['implication ['inheritance X 'feathered] ['inheritance X 'flyer]] [1 0.9]] [['implication ['inheritance Y 'feathered] ['inheritance Y 'bird]] [1 0.9]] R)))
(trun* '([[implication [conjunction ([inheritance _0 swimmer] [inheritance _0 flyer])] [inheritance _0 bird]] [1 0.81]])
[R]
(fresh [X Y]
(inference [['implication ['conjunction [['inheritance X 'feathered] ['inheritance X 'flyer]]] ['inheritance X 'bird]] [1 0.9]] [['implication ['inheritance Y 'swimmer] ['inheritance Y 'feathered]] [1 0.9]] R)))
(trun* '([[implication [conjunction [[inheritance _0 swimmer] [inheritance _0 flyer]]] [conjunction [[inheritance _0 feathered] [inheritance _0 flyer]]]] [1 0.44751381215469616]]
[[equivalence [conjunction [[inheritance _0 swimmer] [inheritance _0 flyer]]] [conjunction [[inheritance _0 feathered] [inheritance _0 flyer]]]] [1 0.44751381215469616]]
[[implication [disjunction [[conjunction [[inheritance _0 feathered] [inheritance _0 flyer]]] [conjunction [[inheritance _0 swimmer] [inheritance _0 flyer]]]]] [inheritance _0 bird]] [1 0.81]]
[[implication [conjunction ([inheritance _0 feathered] [inheritance _0 swimmer] [inheritance _0 flyer])] [inheritance _0 bird]] [1 0.81]]
[[implication [inheritance _0 swimmer] [inheritance _0 feathered]] [1 0.44751381215469616]])
[R]
(fresh [X Y]
(inference [['implication ['conjunction [['inheritance X 'feathered] ['inheritance X 'flyer]]] ['inheritance X 'bird]] [1 0.9]] [['implication ['conjunction [['inheritance X 'swimmer] ['inheritance X 'flyer]]] ['inheritance X 'bird]] [1 0.9]] R)))
(trun* '([[implication [conjunction ([inheritance _0 feathered] [inheritance _0 flyer])] [inheritance _0 bird]] [1 0.44751381215469616]])
[R]
(fresh [X Y]
(inference [['implication ['conjunction [['inheritance X 'swimmer] ['inheritance X 'flyer]]] ['inheritance X 'bird]] [1 0.9]] [['implication ['inheritance Y 'swimmer] ['inheritance Y 'feathered]] [1 0.9]] R)))
(trun* '([[implication [conjunction ([inheritance _0 swimmer] [inheritance _0 flyer])] [inheritance _0 bird]] [1 0.81]])
[R]
(fresh [X Y]
(inference [['implication ['conjunction [['inheritance X 'feathered] ['inheritance X 'flyer]]] ['inheritance X 'bird]] [1 0.9]] [['implication ['inheritance Y 'swimmer] ['inheritance Y 'feathered]] [1 0.9]] R)))
; variable elimination
(trun* '([[inheritance robin animal] [1 0.81]])
[R]
(fresh [X Y]
(inference [['implication ['inheritance X 'bird] ['inheritance X 'animal]] [1 0.9]] [['inheritance 'robin 'bird] [1 0.9]] R)))
(trun* '([[inheritance robin bird] [1 0.44751381215469616]])
[R]
(fresh [X Y]
(inference [['implication ['inheritance X 'bird] ['inheritance X 'animal]] [1 0.9]] [['inheritance 'robin 'animal] [1 0.9]] R)))
(trun* '([[inheritance robin bird] [1 0.81]])
[R]
(fresh [X Y]
(inference [['inheritance 'robin 'animal] [1 0.9]] [['equivalence ['inheritance X 'bird] ['inheritance X 'animal]] [1 0.9]] R)))
(trun* '([[implication [inheritance swan flyer] [inheritance swan bird]] [1 0.81]]) [R]
(fresh [X Y]
(inference [['implication ['conjunction [['inheritance X 'feathered] ['inheritance X 'flyer]]] ['inheritance X 'bird]] [1 0.9]]
[['inheritance 'swan 'feathered] [1 0.9]] R)))
(trun* '([1 0.42631578947368426]) [V]
(fresh [X Y]
(inference [['conjunction [['inheritance ['var X []] 'bird] ['inheritance ['var X []] 'swimmer]]] [1 0.9]] [['inheritance 'swan 'bird] [1 0.9]] [['inheritance 'swan 'swimmer] V])))
(trun* '([[conjunction ([inheritance swan [var _0 []]] [inheritance [var _1 []] [var _0 []]] [inheritance [var _1 []] flyer] [inheritance [var _1 []] swimmer])] [1 0.81]]
[[implication [inheritance swan _0] [conjunction ([inheritance [var _1 (_0)] _0] (inheritance [var _1 (_0)] flyer) (inheritance [var _1 (_0)] swimmer))]] [1 0.44751381215469616]]
[[conjunction ([inheritance swan flyer] [inheritance swan swimmer])] [1 0.42631578947368426]])
[R]
(fresh [X Y]
(inference [['conjunction [['inheritance ['var X []] 'flyer] ['inheritance ['var X []] 'bird] ['inheritance ['var X []] 'swimmer]]] [1 0.9]] [['inheritance 'swan 'bird] [1 0.9]] R)))
; variable introduction
(trun* '([[inheritance bird animal] [1 0.44751381215469616]]
[[similarity bird animal] [1 0.44751381215469616]]
[[inheritance robin [ext-intersection [animal bird]]] [1 0.81]]
[[inheritance robin [int-intersection [animal bird]]] [1 0.81]]
[[inheritance robin [ext-difference animal bird]] [0 0.81]]
[[implication [inheritance _0 bird] [inheritance _0 animal]] [1 0.44751381215469616]]
[[conjunction [[inheritance [var _0 []] bird] [inheritance [var _0 []] animal]]] [1 0.81]]
[[equivalence [inheritance _0 bird] [inheritance _0 animal]] [1 0.44751381215469616]])
[R]
(fresh [X Y]
(inference [['inheritance 'robin 'animal] [1 0.9]] [['inheritance 'robin 'bird] [1 0.9]] R)))
(trun* '([[inheritance chess sport] [1 0.44751381215469616]]
[[similarity chess sport] [1 0.44751381215469616]]
[[inheritance [int-intersection [sport chess]] competition] [1 0.81]]
[[inheritance [ext-intersection [sport chess]] competition] [1 0.81]]
[[inheritance [int-difference sport chess] competition] [0 0.81]]
[[implication [inheritance sport _0] [inheritance chess _0]] [1 0.44751381215469616]]
[[conjunction [[inheritance chess [var _0 []]] [inheritance sport [var _0 []]]]] [1 0.81]]
[[equivalence [inheritance sport _0] [inheritance chess _0]] [1 0.44751381215469616]])
[R]
(fresh [X Y]
(inference [['inheritance 'sport 'competition] [1 0.9]] [['inheritance 'chess 'competition] [1 0.9]] R)))
; multiple variables
(trun* '([[inheritance key [ext-image open [nil lock1]]] [1 0.44751381215469616]]
[[similarity key [ext-image open [nil lock1]]] [1 0.44751381215469616]]
[[inheritance key1 [ext-intersection [[ext-image open [nil lock1]] key]]] [1 0.81]]
[[inheritance key1 [int-intersection [[ext-image open [nil lock1]] key]]] [1 0.81]]
[[inheritance key1 [ext-difference [ext-image open [nil lock1]] key]] [0 0.81]]
[[implication [inheritance _0 key] [inheritance _0 [ext-image open [nil lock1]]]] [1 0.44751381215469616]]
[[conjunction [[inheritance [var _0 []] key] [inheritance [var _0 []] [ext-image open [nil lock1]]]]] [1 0.81]]
[[equivalence [inheritance _0 key] [inheritance _0 [ext-image open [nil lock1]]]] [1 0.44751381215469616]])
[R]
(fresh [X Y]
(inference [['inheritance 'key1 ['ext-image 'open [nil 'lock1]]] [1 0.9]] [['inheritance 'key1 'key] [1 0.9]] R)))
(trun* '([[implication [conjunction [[inheritance _0 key] [inheritance _1 lock]]] [inheritance _1 [ext-image open [_0 nil]]]] [1 0.44751381215469616]]
[[conjunction [[implication [inheritance _0 key] [inheritance [var _1 []] [ext-image open [_0 nil]]]] [inheritance [var _1 []] lock]]] [1 0.81]])
[R]
(fresh [X Y]
(inference [['implication ['inheritance X 'key] ['inheritance 'lock1 ['ext-image 'open [X nil]]]] [1 0.9]] [['inheritance 'lock1 'lock] [1 0.9]] R)))
(trun* '([[conjunction ([inheritance [var _0 []] lock] [inheritance [var _0 []] [ext-image open [[var _1 []] nil]]] [inheritance [var _1 []] key])] [1 0.81]]
[[implication [inheritance _0 lock] [conjunction ([inheritance _0 (ext-image open ([var _1 (_0)] nil))] (inheritance [var _1 (_0)] key))]] [1 0.44751381215469616]])
[R]
(fresh [X Y]
(inference [['conjunction [['inheritance ['var X []] 'key] ['inheritance 'lock1 ['ext-image 'open [['var X []] nil]]]]] [1 0.9]] [['inheritance 'lock1 'lock] [1 0.9]] R))))
+40
View File
@@ -0,0 +1,40 @@
(ns nal.test.reader
(:require [nal.reader :refer :all]
[clojure.test :refer :all]
[nal.test.test-utils :refer [both-equal]]))
(deftest test-replacements
(are [s1 s2] (= s1 (replacements s2))
"(ext-set a b)" "{a b}"
"(int-set a b)" "[a b]"
"(retro-impl a b)" "(=\\> a b)"
"(pred-impl a b)" "(=/> a b)"
"(a int-dif b)" "(a ~ b)"
"(seq-conj a b c)" "(&/ a b c)"
"(ext-inter a b)" "(& a b)"
"(a ext-inter b)" "(a & b)"
"(conj a b)" "(&& a b)"
"(inst a b)" "({-- a b)"
"(prop a b)" "(--] a b)"
"(prop a (int-set b v))" "(--] a [b v])"
"(inst-prop a b)" "({-] a b)"
"(int-image a b)" "(\\ a b)"
"(ext-image a b)" "(/ a b)"
"(ind-var X)" "$X"
"(dep-var X)" "#X"))
(deftest test-read-rule
(are [l s] (= l (read-rule s))
'[A --> B] "A --> B"
'[(A --> (int-set D C)) (A int-dif B)] "(A --> [D C]) (A ~ B)"
'[A --> B] "A --> B"
'[((int-set A) <-> (int-set B)) (A <-> B) |- ((int-set A) <-> (int-set B))
:pre (:question?)
:post (:t/belief-identity :p/judgment)]
"([A] <-> [B]) (A <-> B) |- ([A] <-> [B]) :pre (:question?) :post (:t/belief-identity :p/judgment)"
'[(M retro-impl P) (M retro-impl S) |- ((M retro-impl (P &| S)) :post (:t/intersection)
(M retro-impl (P || S)) :post (:t/union))
:pre ((:!= S P))]
"(M =\\> P) (M =\\> S) |- ((M =\\> (P &| S)) :post (:t/intersection) (M =\\> (P || S)) :post (:t/union)) :pre ((:!= S P))"))
+3 -7
View File
@@ -1,9 +1,5 @@
(ns nal.test.test-utils
(:require [clojure.test :refer [is]]
[clojure.core.logic :refer [run run*]]))
(:require [clojure.test :refer [is are]]))
(defmacro trun [result lvars & body]
`(is (= ~result (run 1 ~lvars ~@body))))
(defmacro trun* [result lvars & body]
`(is (= ~result (run* ~lvars ~@body))))
(defmacro both-equal [& body]
`(are [arg1# arg2#] (= arg1# arg2#) ~@body))
-69
View File
@@ -1,69 +0,0 @@
(ns nal.test.truth-value
(:refer-clojure :exclude [== reduce replace])
(:require [clojure.test :refer :all]
[nal.truth-value :refer :all]
[clojure.core.logic :refer [run run*]]))
(deftest test-f-rev
(is (= '([1.3 0.588235294117647])
(run* [q] (f-rev [1 0.5] [2 0.3] q))))
(is (= '()
(run* [q] (f-rev [1 2] [2 0.6] q))))
(is (= '([1.0138248847926268 0.9601769911504424])
(run* [q] (f-rev [4 0.1] [1 0.96] q)))))
(deftest test-f-exp
(is (= '(2.75) (run* [Q] (f-exp [1.4, 2.5], Q))))
(is (= '(9.5) (run* [Q] (f-exp [2, 6], Q)))))
(deftest test-f-neg
(is '([0 2]) (run* [q] (f-neg [1 2] q))))
(deftest test-f-cnv
(is '([1 2/3]) (run* [Q] (f-cnv [1 2] Q))))
(deftest test-f-cnt
(is '([0 10/9]) (run* [q] (f-cnt [3 5] q))))
(deftest test-f-ded
(is '([3 24]) (run* [q] (f-ded [1 2] [3 4] q))))
(deftest test-f-ana
(is '([18 210]) (run* [q] (f-ana [3 5] [6 7] q))))
(deftest test-f-res
(is '([7 32]) (run* [q] (f-res [7 8] [1 4] q))))
(deftest test-f-abd
(is '([3 8/9]) (run* [q] (f-abd [1 2] [3 4] q))))
(deftest test-f-ind
(is '([3 42/43]) (run* [q] (f-ind [3, 6], [1, 7], q))))
(deftest test-f-exe
(is '([1 126/127]) (run* [q] (f-exe [3, 6], [1, 7], q))))
(deftest test-f-com
(is '() (run* [q] (f-com [7 2] [5 4] q)))
(is '([5 8/9]) (run* [q] (f-com [1, 2], [5, 4] q))))
(deftest test-f-int
(is '([5 8]) (run* [q] (f-int [1 2] [5 4] q))))
(deftest test-f-uni
(is '([-9 16]) (run* [Q] (f-uni [3, 4], [6, 4], Q))))
(deftest test-f-dif
(is '([-15 16]) (run* [Q] (f-dif [3, 4], [6, 4], Q))))
(deftest test-f-pnn
(is '([-5 12]) (run* [Q] (f-dif [1, 6], [6, 2], Q))))
(deftest test-f-npp
(is '([-5 -10]) (run* [Q] (f-npp [2, 1], [5, 2], Q))))
(deftest test-f-pnp
(is '([-1 -10]) (run* [Q] (f-pnp [1, 5], [2, 2], Q))))
(deftest test-f-nnn
(is '([3 -6]) (run* [Q] (f-pnp [-3, 2], [2, -1], Q))))
+64
View File
@@ -0,0 +1,64 @@
(ns nal.test.underiver
(:require nal.reader
[nal.deriver.rules :as r]
[nal.deriver.matching :as m]
[nal.deriver.utils :as u]
[clojure.core.unify :as un]
[clojure.core.match :as omg]
[clojure.set :as cs]
[nal.core :as c]))
(r/defrules rls
#R[(P ==> M) (S ==> M) |- (S ==> P) :post (:t/induction :allow-backward) :pre ((:!= S P))])
(def compiled (r/compile-rules rls))
(def r-map (first (r/rule (first rls))))
(defn sym-map [m p1 p2]
(let [vals (set (vals m))
all (cs/difference (set (remove u/operator? (flatten [p1 p2])))
vals)]
(->> all
(map (fn [el] [`(quote ~el) el]))
(into {})
(merge m))))
(defn underiver [{:keys [p1 p2 conclusions]}]
(let [concl (vec (:conclusion (first conclusions)))
[m pattern] (m/find-and-replace-symbols concl "x")
m (sym-map m p1 p2)
p1 (m/replace-symbols p1 m)
p2 (m/replace-symbols p2 m)]
(eval (m/quote-operators
`(fn [xn#] (omg/match xn#
~pattern [~p1 ~p2]
:else []))))))
(defn deriver [rls]
(let [compiled (r/compile-rules rls)]
(fn [[p1 p2]]
(let [t {:statement p1
:desire [1 0.9]
:task-type :judgement
:occurrence 1}
b {:statement p2
:truth [1 0.9]
:occurrence 0}]
(c/inference compiled t b)))))
(comment
((underiver r-map) '[==> wut ahh?])
=> [[==> ahh? M] [==> wut M]]
((deriver rls) '[[==> ahh? M] [==> wut M]])
(let [[{st :statement}] (c/inference compiled
{:statement '[==> ahh? M]
:truth [1 0.9]
:task-type :judgement
:occurrence 1}
{:statement '[==> wut M]
:truth [1 0.9]
:occurrence 0})]
((underiver r-map) st))
)
-22
View File
@@ -1,22 +0,0 @@
(ns nal.test.utils
(:refer-clojure :exclude [== reduce replace])
(:require [clojure.test :refer :all]
[nal.utils :refer :all]
[clojure.core.logic :refer [run run*]]))
(deftest test-u-not
(is (= '(-2) (run* [q] (u-not 3 q)))))
(deftest test-u-and
(is (= '(6) (run* [q] (u-and [1 2 3] q))))
(is (= '(1) (run* [q] (u-and [1] q)))))
(deftest test-u-or
(is (= '(1) (run* [q] (u-or [7 1] q)))))
(deftest test-u-w2c
(is (= '(3/4) (run* [q] (u-w2c 3 q)))))
(deftest test-subtract
(is (= '([2 4]) (run* [q] (subtracto [1 1 2 3 4] [1 3 5] q))))
(is (= '([]) (run* [q] (subtracto [] [] q)))))
+22
View File
@@ -0,0 +1,22 @@
(ns narjure.test.bag
(:require [clojure.test :refer :all]
[narjure.bag :refer :all]))
(defn abc-bag
([] (abc-bag 3))
([cap] (-> (default-bag cap)
(put-el {:key :a :priority 0.7})
(put-el {:key :b :priority 0.7})
(put-el {:key :c :priority 0.7}))))
(def a-bag (put-el (default-bag) {:key :a :priority 0.7}))
(deftest test-bag
(is (= 3 (count-els (abc-bag))))
(is (= 2 (count (-> (default-bag)
(put-el {:key :a :priority 0.7})
(put-el {:key :b :priority 0.7})
(put-el {:key :a :priority 0.7})
:queue))))
(is (= 2 (count-els (abc-bag 2))))
(is (nil? (get-el a-bag :b))))
+41
View File
@@ -0,0 +1,41 @@
(ns narjure.test.control.buffer
(:require
[clojure.test :refer :all]
[narjure.control.buffer :refer :all]
[clojure.core.async.impl.protocols :refer [full? add! remove! close-buf!]]))
(defmacro throws? [expr]
`(try
~expr
false
(catch Throwable _# true)))
(def warn-cnt (atom 0))
(defn panick! [_]
(swap! warn-cnt inc))
(deftest sliding-buffer-tests
(let [fb (panicking-sliding-buffer 2 panick! 1)]
(reset! warn-cnt 0)
(is (= 0 (count fb)))
(add! fb :1)
(is (= 1 (count fb)))
(add! fb :2)
(is (= 2 (count fb)))
(is (= 1 @warn-cnt))
(is (not (full? fb)))
(is (not (throws? (add! fb :3))))
(is (= 2 (count fb)))
(is (= :2 (remove! fb)))
(is (not (full? fb)))
(is (= 1 (count fb)))
(is (= :3 (remove! fb)))
(is (= 0 (count fb)))
(is (throws? (remove! fb)))))
+65
View File
@@ -0,0 +1,65 @@
(ns narjure.test.control.flow
(:require
[clojure.test :refer :all]
[narjure.control.flow :refer :all]
[clojure.core.async :as as]))
(deftest test-check-element-in-map
(let [m {:a 1 :b 2}]
(is (= 0 (:c (check-element-in-map [:c] m))))
(is (= [] (:c (check-element-in-map [:c] [] m))))
(is (= nil (:k (check-element-in-map [:c] [] m))))))
(deftest test-all
(is (= #{:a :b :c :d :e}
(all [[:a :b]
[:a :c]
[:c :d]
[:c :e]]))))
(defn some-var [])
(deftest test-kw->var
(is (var? (kw->fn :narjure.control.flow/all)))
(is (var? (kw->fn ::some-var)))
(is (nil? (kw->fn ::wrong-var))))
(def wf [[:a :b]
[:a :c]
[:a :d]
[:b :d]])
(deftest test-fn-outputs
(let [outputs (fn-outputs wf 1)]
(is (= 4 (count (keys outputs))))
(is (every? nil? (map outputs [:c :d])))))
(deftest test-fn-inputs
(is (= {[:d :c :b] [:a]
[:d] [:b]}
(fn-inputs wf)))
(is (= {[:d :c :b] [:a]
[:k :d] [:c :b]}
(fn-inputs (concat wf [[:c :d]
[:c :k]
[:b :k]])))))
(def out (as/chan 2))
(defn first-fn [data] (assoc data :fn1 :ok))
(defn second-fn [data] (assoc data :fn2 :ok))
(defn third-fn [data]
(as/>!! out (assoc data :fn3 :ok)))
(def test-flow [[::first-fn ::second-fn]
[::second-fn ::third-fn]
[::first-fn ::third-fn]])
(deftest test-generate-flow
(let [in (generate-flow test-flow {:buffer 2})]
(as/>!! in {})
(let [res [(as/<!! out) (as/<!! out)]]
(is (= #{{:fn1 :ok
:fn3 :ok}
{:fn1 :ok
:fn2 :ok
:fn3 :ok}}
(set res))))))
+16
View File
@@ -0,0 +1,16 @@
(ns narjure.test.cycle
(:require [clojure.test :refer :all]
[narjure.cycle :refer :all]))
(def b1 {:truth [1.0 0.9]
:evidental-base #{1}
:statement '[inheritance bird swimmer]})
(def b2 {:truth [0.1 0.6]
:evidental-base #{2}
:statement '[inheritance bird swimmer]})
(deftest test-local-inference
(is (= [0.8714285714285714 0.9130434782608696]
(:truth (local-inference b1 b2))))
(is (= [1.0 0.9]
(:truth (local-inference b1 (assoc b2 :evidental-base #{1 2}))))))
+50
View File
@@ -0,0 +1,50 @@
(ns narjure.test.memory.redis
(:require [clojure.test :refer :all]
[narjure.memory.redis :as r]
[narjure.memory.api :as m]
[taoensso.carmine :as c]))
(def config
{:pool {}
:spec {:host "127.0.0.1" :port 6379}})
(def mem (r/->RedisMemory config))
(def concept1 '[--> tim cat])
(def concept2 'tim)
(def truth1 {:frequency (float 0.9)
:confidence (float 0.2)
:occurrence 1})
(def truth2 {:frequency (float 0.5)
:confidence (float 0.8)
:occurrence 2})
(defn termlink [c]
{:priority (float 0.1)
:durability (float 0.1)
:quality (float 0.1)
:concept (str (hash c))})
(deftest test-redis
(c/wcar config (c/flushall))
(m/add-term mem concept1)
(is (= concept1 (m/term mem (hash concept1))))
(m/add-truth mem concept1 truth1)
(m/add-truth mem concept1 truth2)
(is (= (set [truth1 truth2])
(set (map #(dissoc % :id) (m/truths mem concept1)))))
(m/remove-truth mem concept1 (:id (last (m/truths mem concept1))))
(is (:id (first (m/truths mem concept1))))
(is ((set [truth1 truth2]) (dissoc (first (m/truths mem concept1)) :id)))
(m/add-term mem concept2)
(m/add-termlink mem concept1 (termlink concept2))
(is (= [(termlink concept2)]
(map #(dissoc % :id) (m/termlinks mem concept1))))
(is (= concept2 (m/term mem (:concept (first (m/termlinks mem concept1)))))))
+17 -6
View File
@@ -3,19 +3,28 @@
[narjure.narsese :refer :all]
[instaparse.core :refer [failure?]]))
(defn narsese->clj [s] (:data (parse s)))
(defn narsese->clj [s] (:statement (parse s)))
(defn get-truth [s] (:truth (parse s)))
(deftest test-parser
(is (= '([[conjunction [inheritance tim fish] [inheritance tom fish]]])
(is (= '[[conjunction [inheritance tim fish] [inheritance tom fish]]]
(narsese->clj "(&&, <tim --> fish>, <tom --> fish>).")))
(is (= '([[conjunction [inheritance tim fish] [inheritance tom fish]]])
(is (= '[[conjunction [inheritance tim fish] [inheritance tom fish]]]
(narsese->clj "(<tim --> fish> && <tom --> fish>).")))
(is (= '([[conjunction [inheritance tim fish] [inheritance tom fish]]])
(is (= '[[conjunction [inheritance tim fish] [inheritance tom fish]]]
(narsese->clj "((tim --> fish) && (tom --> fish)).")))
(is (= '([inheritance bird swimmer])
(is (= '[inheritance bird swimmer]
(narsese->clj "<bird --> swimmer>.")))
(is (= [1.0 0.9] (get-truth "<bird --> swimmer>. %1;0.9%"))))
(is (= [1.0 0.9] (get-truth "<bird --> swimmer>. %1;0.9%")))
(is (= '[[negation bird]] (narsese->clj "--bird.")))
(is (= '[[negation bird]] (narsese->clj "(--,bird).")))
(is (= '[[int-image bird animal]] (narsese->clj "(\\,bird,animal).")))
(is (= '[[conjunction [inheritance d_1 [int-set red]] [inheritance d_1 apple]]]
(narsese->clj "(&&,<#1 --> [red]>,<#1 --> apple>).")))
(is (= '[retrospective-implication
[inheritance [product x room_101] enter]
[inheritance [product x door_101] open]]
(narsese->clj "<<( $x, room_101) --> enter> =\\> <( $x, door_101) --> open>>. %0.9;0.1%"))))
(deftest test-numbers-validation
(is (not (failure? (parse "<bird --> swimmer>. %1;0.9%"))))
@@ -30,5 +39,7 @@
(is (not (failure? (parse "<bird --> swimmer>. %0.1;0.9%"))))
(is (not (failure? (parse "<bird --> swimmer>. %0.0;0.9%"))))
(is (not (failure? (parse "<bird --> swimmer>. %0;0.9%"))))
(is (not (failure? (parse "<bird --> swimmer>. %1.00;0.9%"))))
(is (not (failure? (parse "<bird --> swimmer>. %1.;0.9%"))))
(is (failure? (parse "<bird --> swimmer>. %5;0.9%")))
(is (failure? (parse "<bird --> swimmer>. %-1;0.9%"))))