code cleanup

This commit is contained in:
2025-08-06 21:02:44 +02:00
parent 8040c7f7a6
commit d278185ff3
19 changed files with 21 additions and 226 deletions

View File

@@ -3,18 +3,14 @@
Since thoughts are ideas, 'missing' operations here are implemented in [[emptyhead.idea.crud]]."
(:require [emptyhead.idea.protocol :as prtc]
[emptyhead.idea.crud :as idea]
[emptyhead.idea.property :as prop]
[emptyhead.contract.eval :as contract]
[emptyhead.util.magic :as magic]))
;; TODO groups
(defn make-thought
"Helper function to make thought object.
You may want `register-thought!` instead."
[operator & {:keys [data ext-contract ext-stages transient]
[operator & {:keys [data ext-contract ext-stages]
:or {data {} ext-contract {}
ext-stages [[:PRE-EXECUTE] [:EXECUTE] [:POST-EXECUTE]]
transient true}}]
ext-stages [[:PRE-EXECUTE] [:EXECUTE] [:POST-EXECUTE]]}}]
(hash-map :operator operator
:data data
:ext-contract ext-contract
@@ -25,18 +21,12 @@
(defn register-thought!
"Create a thought and register it in the state.
Returns a reference to the created thought."
[operator & {:keys [data ext-contract ext-stages transient group]
[operator & {:keys [data ext-stages]
:as args}]
(idea/have-idea!
:prefix (str "emptyhead.thought#" (magic/symbolize-ns operator))
:properties [magic/thought-ns]
:data (make-thought operator args)))
(defn contract
"Get the extension contract of a `thought`.
Returns the contract."
[thought]
(prtc/val-fn :ext-contract thought))
(idea/have-idea!
:prefix (str "emptyhead.thought#" (magic/symbolize-ns operator))
:properties [magic/thought-ns]
:data (make-thought operator args)))
(defn stages
"Get the extension stages of a `thought`.
@@ -63,8 +53,6 @@
[thought]
[(assoc thought :return (-> thought prtc/copy stack butlast vec)) (-> thought prtc/copy stack peek)])
(def root-thought (make-thought :root))
(defn add-ext-stage!
[thought stage]
(idea/mutate-idea! #(update % :ext-stages conj stage) thought))

View File

@@ -1,7 +1,6 @@
(ns emptyhead.thought.define
"Utilities for defining new thoughts."
(:require [emptyhead.thought.crud :as thought]
[emptyhead.thought.eval :as eval]
[emptyhead.idea.crud :as idea]
[emptyhead.util.magic :as magic]
[emptyhead.idea.memtag :as memtag]
@@ -32,11 +31,10 @@
(merge defaults
(thought/make-thought operator)
{:data (thought/data thought)}
(constr-fn thought parent)
)))])))))
(constr-fn thought parent))))])))))
(defn define!
[operator impl & {:keys [constr-fn defaults]
:as constr-args}]
(register-implementation! operator impl)
(register-constructor! operator constr-args) )
(register-constructor! operator constr-args))

View File

@@ -6,7 +6,6 @@
[emptyhead.util.logging :as logging]
[emptyhead.thought.return :as return]
[emptyhead.idea.property :as prop]
[emptyhead.idea.crud :as idea]
[emptyhead.util.magic :as magic]))
(defn- impl! [thought & [parent]]
@@ -17,8 +16,6 @@
{:thought thought :parent parent :type :unimplemented-thought})
((prtc/copy-fn :implementation impl-idea) thought parent))))
;; FIXME I don't think omitting the parent here is actually valid?
;; might need to use thought.crud/root-thought, but better making parent mandatory tabun
(defn execute!
@@ -26,7 +23,6 @@
Returns (potentially modified) `parent`."
[thought & [parent]]
;; FIXME parent may change mid execution, breaking deep inspection
(loop [th (assoc (prtc/value thought) :_parent (prtc/value parent))
parent parent]
(let [cur (first (thought/stages th))
@@ -38,7 +34,7 @@
;; If it's time for `thought`'s implementation to run, do so,
;; potentially modifying `parent`.
[parent & returns]
(if (= '() (thought/stages th)) ;; NOTE the magic value is now [:EXECUTE] since otherwise nothing could be bound to the global [:emptyhead] propspace
(if (= '() (thought/stages th))
(impl! th parent)
[parent nil])

View File

@@ -37,26 +37,19 @@
Returns a map of {stage => #{thought}}."
[thought]
(let [stages (thought/stages thought)
filter-fn #(contract/evaluate (thought/contract thought) %)
aspects (map
#(->> % get-extensions (filter filter-fn))
stages)]
aspects (map get-extensions stages)]
(zipmap stages aspects)))
(defn pop-stage
"Get aspects of next stage of thought execution.
Returns a tuple of (#{extension-dhammas},
Returns a tuple of (#{extensions},
a copy of `thought` with its first extension-stage removed)."
[thought]
(let [stages (thought/stages thought)
aspects (filter #(contract/evaluate (thought/contract thought) %)
(get-extensions (first stages)))
aspects (get-extensions (first stages))
;; FIXME idk why this was here, but it's wrong--
;; I've commented it out now, probably breaks something elsewhere!
;; aspects (prop/with-property (magic/extension-prop (first stages)))
modified (prtc/copy-fn #(assoc % :ext-stages (rest stages))
thought)]
thought)]
(list aspects modified)))
(defn has-stage?

View File

@@ -1,7 +1,6 @@
(ns emptyhead.thought.return
"Utilities for handling thoughts' return values to their parents."
(:require [emptyhead.thought.crud :as thought]
[emptyhead.idea.protocol :as prtc]))
(:require [emptyhead.idea.protocol :as prtc]))
(defn with-return [thought data]
(if (first data)
@@ -10,7 +9,7 @@
thought))
(defn return-vals [thought namespace]
(prtc/copy-fn #(get-in % [:return namespace]) thought))
(prtc/val-fn #(get-in % [:return namespace]) thought))
(defn clear-returns [thought]
(prtc/copy-fn #(assoc % :return {}) thought))
(prtc/val-fn #(assoc % :return {}) thought))