Real progress: working knowledge accessor.

Also, better representation of second hand knowledge.
This commit is contained in:
Simon Brooke 2020-05-18 11:50:42 +01:00
parent fefa218298
commit 6b93078763
No known key found for this signature in database
GPG key ID: A7A4F18D1D4DF987
3 changed files with 119 additions and 58 deletions

View file

@ -16,6 +16,5 @@
[lein-codox "0.10.7"] [lein-codox "0.10.7"]
[lein-cucumber "1.0.2"] [lein-cucumber "1.0.2"]
[lein-gorilla "0.4.0"]] [lein-gorilla "0.4.0"]]
:repl-options {:init-ns wildwood.core} :repl-options {:init-ns wildwood.caesar}
:url "https://simon-brooke.github.io/wildwood/" :url "https://simon-brooke.github.io/wildwood/")
)

View file

@ -74,10 +74,10 @@
* `was-unarmed` - true of an entity at a time `t` if they were unarmed at the time. * `was-unarmed` - true of an entity at a time `t` if they were unarmed at the time.
Note that ALL of this is too complex for the simple DTree logic of the Arboretum / Note that ALL of this is too complex for the simple DTree logic of the Arboretum /
KnacqTools generation. They could not unpack propositions as I'm proposing here. KnacqTools generation. They could not unpack propositions as I'm proposing here."
" (:require [wildwood.advocate :refer [Advocate]]
(:require [wildwood.knowledge-accessor :refer [Accessor]] [wildwood.knowledge-accessor :refer [Accessor]]
[wildwood.advocate :refer [Advocate]])) [wildwood.logic :as l]))
(def ides-of-march (def ides-of-march
@ -96,79 +96,102 @@
"The month of April, 44BC, as a range." "The month of April, 44BC, as a range."
(sort [(+ -440000 400 1) (+ -440000 400 30)])) (sort [(+ -440000 400 1) (+ -440000 400 30)]))
(defrecord KnowledgeBase [propositions]
Accessor
(fetch [self id]
(reduce
concat
(filter
(fn [p]
(some?
(fn [k] (= (p k) id))
(keys p))))
(.propositions self)))
(match [self proposition]
(l/match proposition (.propositions self)))
(store [self proposition]
(throw (UnsupportedOperationException. "Not yet implemented"))))
(def anthony-kb (def anthony-kb
"Mark Antony knows that Brutus is honourable, and that Caesar is buried." "Mark Antony knows that Brutus is honourable, and that Caesar is buried."
{:brutus [{:verb :is :subject :brutus :object :honourable}] (KnowledgeBase.
:caesar [{:verb :bury :subject :calpurnia :object :caesar :date eighteenth-march :nth-hand 1}]}) [{:verb :is :subject :brutus :object :honourable :authority :anthony}
{:verb :bury :subject :calpurnia :object :caesar :date eighteenth-march :authority :anthony}]))
(def brutus-kb (def brutus-kb
"Brutus will admit that he and Cassius were in the forum in the Ides of March" "Brutus will admit that he and Cassius were in the forum in the Ides of March"
{:brutus [{:verb :present :subject :brutus :object :forum :location :forum :date ides-of-march :nth-hand 1}] (KnowledgeBase.
:cassius [{:verb :present :subject :cassius :object :forum :location :forum :date ides-of-march :nth-hand 1}] [{:verb :present :subject :brutus :object :forum :location :forum :date ides-of-march :authority :brutus}
:forum [{:verb :present :subject :brutus :object :forum :location :forum :date ides-of-march :nth-hand 1} {:verb :present :subject :cassius :object :forum :location :forum :date ides-of-march :authority :brutus}]))
{:verb :present :subject :cassius :object :forum :location :forum :date ides-of-march :nth-hand 1}]})
(def cassius-kb (def cassius-kb
"Cassius and Longus each bear witness that the other killed Caesar in the "Cassius and Longus each bear witness that the other killed Caesar in the
Forum on the Ides of March." Forum on the Ides of March."
{:caesar [{:verb :kill :subject :longus :object :caesar :location :forum :date ides-of-march :nth-hand 1}] (KnowledgeBase.
:longus [{:verb :kill :subject :longus :object :caesar :location :forum :date ides-of-march :nth-hand 1}] [{:verb :kill :subject :longus :object :caesar :location :forum :date ides-of-march :authority :cassius}]))
:forum [{:verb :kill :subject :longus :object :caesar :location :forum :date ides-of-march :nth-hand 1}]})
(def drusilla-kb (def drusilla-kb
"Drusilla has heard that Brutus killed Caesar in the forum. She keys it on all three, for efficiency "Drusilla has heard that Brutus killed Caesar in the forum. "
of retrieval." (KnowledgeBase.
{:caesar [{:verb :kill :subject :brutus :object :caesar :location :forum :date ides-of-march :nth-hand 2} [{:verb :kill :subject :brutus :object :caesar :location :forum :date ides-of-march
{:verb :bury :subject :calpurnia :object :caesar :date eighteenth-march :nth-hand 1}] ;; Second hand knowledge. The `:data` (maybe should be `:warrant`?)
:brutus [{:verb :kill :subject :brutus :object :caesar :location :forum :date ides-of-march :nth-hand 2}] ;; shows, as two-position propositions, that Drusilla learned this
:forum [{:verb :kill :subject :brutus :object :caesar :location :forum :date ides-of-march :nth-hand 2}]}) ;; from Calpurnia, and the authority is Drusilla since that's who the
;; knowledge comes from. I think.
:data [{:verb :utter :subject :calpurnia
:object {:verb :kill :subject :brutus :object :caesar :location :forum :date ides-of-march :authority :calpurnia}
:authority :drusilla}
{:verb :hear :subject :calpurnia
:object {:verb :kill :subject :brutus :object :caesar :location :forum :date ides-of-march :authority :calpurnia} :authority :drusilla}]
:authority :drusilla}
{:verb :utter :subject :calpurnia :object {:verb :bury :subject :calpurnia :object :caesar :date eighteenth-march :authority :calpurnia}
:authority :drusilla}
{:verb :hear :subject :calpurnia :object {:verb :bury :subject :calpurnia :object :caesar :date eighteenth-march :authority :calpurnia}
:authority :drusilla}
{:verb :bury :subject :calpurnia :object :caesar :date eighteenth-march :authority :drusilla
:data [{:verb :utter :subject :calpurnia :object {:verb :bury :subject :calpurnia :object :caesar :date eighteenth-march :authority :calpurnia}
:authority :drusilla}
{:verb :hear :subject :calpurnia :object {:verb :bury :subject :calpurnia :object :caesar :date eighteenth-march :authority :calpurnia}
:authority :drusilla}]}]))
(def falco-kb (def falco-kb
"Falco believes that Caesar has been killed, but doesn't know by whom or when." "Falco believes that Caesar has been killed, but doesn't know by whom or when."
{:caesar [{:verb :kill :object :caesar :location :forum}] (KnowledgeBase.
:brutus [{:verb :kill :object :caesar :location :forum}] [{:verb :kill :object :caesar :location :forum :authority :falco}]))
:forum [{:verb :kill :object :caesar :location :forum}]})
(def gaius-kb (def gaius-kb
"Gaius has heard that Brutus killed Caesar, but believes it happened in April." "Gaius has heard that Brutus killed Caesar, but believes it happened in April."
{:caesar [{:verb :kill :subject :brutus :object :caesar :location :forum :date april :nth-hand 2}] (KnowledgeBase.
:brutus [{:verb :kill :subject :brutus :object :caesar :location :forum :date april :nth-hand 2}] ;; TODO: replace that nth-hand with an actual warrant. Who did he hear it from?
:forum [{:verb :kill :subject :brutus :object :caesar :location :forum :date april :nth-hand 2}]}) [{:verb :kill :subject :brutus :object :caesar :location :forum :date april :nth-hand 2 :authority :gaius}]))
(def longus-kb (def longus-kb
"Cassius and Longus each bear witness that the other killed Caesar in the "Cassius and Longus each bear witness that the other killed Caesar in the
Forum on the Ides of March." Forum on the Ides of March."
{:caesar [{:verb :kill :subject :cassius :object :caesar :location :forum :date ides-of-march :nth-hand 1}] (KnowledgeBase.
:cassius [{:verb :kill :subject :cassius :object :caesar :location :forum :date ides-of-march :nth-hand 1}] [{:verb :kill :subject :cassius :object :caesar :location :forum :date ides-of-march :authority :longus}]))
:forum [{:verb :kill :subject :cassius :object :caesar :location :forum :date ides-of-march :nth-hand 1}]})
(defn knowledge (defrecord CompoundKnowledgeBase [kbs]
"The way I've encoded propositions in the sample `wildwood.caesar` namespace Accessor
is experimental and probably clumsy. This function, given such knowledge (fetch [self id]
bases, returns a single set of distinct propositions. It also makes it easier (throw (UnsupportedOperationException. "Not yet implemented")))
to keep this namespace working if (as is likely) the underlying encoding (match [self proposition]
changes. Argument: `kbs`: knowledge bases, taken from `wildwood.caesar`." (l/match proposition (reduce concat (map #(.propositions %) (.kbs self)))))
[& kbs] (store [self proposition]
(set (throw (UnsupportedOperationException. "Not yet implemented"))))
(reduce
concat
(map
(fn [kb]
(reduce
concat
(map
#(kb %)
(keys kb))))
kbs))))
;; (knowledge k/brutus-kb k/cassius-kb)
;; (knowledge brutus-kb cassius-kb)
(def all-knowledge (def all-knowledge
(knowledge (CompoundKnowledgeBase.
k/anthony-kb [anthony-kb
k/brutus-kb brutus-kb
k/cassius-kb cassius-kb
k/drusilla-kb drusilla-kb
k/falco-kb falco-kb
k/gaius-kb gaius-kb
k/longus-kb)) longus-kb]))

39
src/wildwood/logic.clj Normal file
View file

@ -0,0 +1,39 @@
(ns wildwood.logic
"Highly experimental work towards basic logic operators on Bialowieza-style
proposition structures"
)
(defn matches?
"True if this `candidate` matches this `pattern`. AT THIS STAGE, a
match is found if for every key in `pattern`, the value of that key in the
candidate is the same as the value in `pattern`. Note that in future the
values of the `:time`, `:location`, `:confidence` and `:data` keys may be
handled specially."
[pattern candidate]
(every?
#(= (pattern %) (candidate %))
(keys pattern)))
(defn match
"Return those of these `candidates` matched by this `pattern`. Both `pattern`
and each candidate in `candidates` are expected to be maps. AT THIS STAGE, a
match is found if for every key in `pattern`, the value of that key in the
candidate is the same as the value in `pattern`. Note that in future the
values of the `:time`, `:location`, `:confidence` and `:data` keys may be
handled specially."
[pattern candidates]
(filter
#(matches? pattern %)
candidates))
(defn fetch
"Return those propositions from among these `propositions` where some key
matches this `id`."
[id propositions]
(remove
(fn [p]
(empty?
(filter
(fn [k] (= id (p k)))
(keys p))))
propositions))