summaryrefslogtreecommitdiff
path: root/intellect-framework-from-internet/starts/meaning-vm/level-0
diff options
context:
space:
mode:
authorolpc user <olpc@xo-5d-f7-86.localdomain>2020-01-10 14:56:27 -0800
committerolpc user <olpc@xo-5d-f7-86.localdomain>2020-01-10 14:56:27 -0800
commit26c980d302adce8e3d802cb8db8ab1c69d58ce1a (patch)
treee296225f17370c9e472660396b3a51539f76ff28 /intellect-framework-from-internet/starts/meaning-vm/level-0
parent2e01fed206e46a669ba56f57b4b943cfe661a0f1 (diff)
parentc8bb547bea279af2bb48c13260f98aa8add07131 (diff)
downloadstandingwithresilience-26c980d302adce8e3d802cb8db8ab1c69d58ce1a.tar.gz
standingwithresilience-26c980d302adce8e3d802cb8db8ab1c69d58ce1a.zip
Merge branch 'intellect-framework-from-internet'
Diffstat (limited to 'intellect-framework-from-internet/starts/meaning-vm/level-0')
-rw-r--r--intellect-framework-from-internet/starts/meaning-vm/level-0/baseref.hpp169
-rw-r--r--intellect-framework-from-internet/starts/meaning-vm/level-0/common.hpp65
-rw-r--r--intellect-framework-from-internet/starts/meaning-vm/level-0/concept.cpp136
-rw-r--r--intellect-framework-from-internet/starts/meaning-vm/level-0/concept.hpp73
-rw-r--r--intellect-framework-from-internet/starts/meaning-vm/level-0/errors.hpp101
-rw-r--r--intellect-framework-from-internet/starts/meaning-vm/level-0/level-0.hpp7
-rw-r--r--intellect-framework-from-internet/starts/meaning-vm/level-0/memorystore.cpp179
-rw-r--r--intellect-framework-from-internet/starts/meaning-vm/level-0/memorystore.hpp63
-rw-r--r--intellect-framework-from-internet/starts/meaning-vm/level-0/ref.cpp33
-rw-r--r--intellect-framework-from-internet/starts/meaning-vm/level-0/ref.hpp19
10 files changed, 845 insertions, 0 deletions
diff --git a/intellect-framework-from-internet/starts/meaning-vm/level-0/baseref.hpp b/intellect-framework-from-internet/starts/meaning-vm/level-0/baseref.hpp
new file mode 100644
index 0000000..5c84279
--- /dev/null
+++ b/intellect-framework-from-internet/starts/meaning-vm/level-0/baseref.hpp
@@ -0,0 +1,169 @@
+#pragma once
+
+#include "common.hpp"
+#include "errors.hpp"
+#include "memorystore.hpp"
+
+#include <map>
+#include <vector>
+
+namespace intellect {
+namespace level0 {
+
+template <typename ref>
+class baseref
+{
+public:
+ struct array; struct links_t;
+ baseref(concept *p)
+ : p(p)
+ {
+ if (p == 0) {
+ throw null_reference();
+ }
+ }
+
+ baseref & operator=(concept *p)
+ {
+ self.p = p;
+ return self;
+ }
+
+ ref link(ref const & type, ref const & target) { p->link(type.p, target.p); return ptr(); }
+ void unlink(ref const & type, ref const & target) { p->unlink(type.p, target.p); }
+ void unlink(ref const & type) { p->unlink(type.p); }
+
+ bool linked(ref const & type) const { return p->linked(type.p); }
+ bool linked(ref const & type, ref const & target) const { return p->linked(type.p, target.p); }
+
+ ref get(ref const & type) const { return p->get(type.p); }
+ void set(ref const & type, ref const & target) { p->set(type.p, target.p); }
+
+ array getAll(ref const & type) const;
+ links_t links() const;
+
+ template <typename... Ref>
+ ref link(Ref... refspack)
+ {
+ std::initializer_list<ref> refs{refspack...};
+ for (auto it = refs.begin(); it != refs.end();) {
+ ref type = *it++;
+ ref target = *it++;
+ link(type, target);
+ }
+ return ptr();
+ }
+
+ template <typename T>
+ T& vget(ref const & type) const { return p->vget<T>(type.p); }
+ template <typename T>
+ void vset(ref const & type, T const & v) { p->set(type.p, level0::alloc(self, v)); }
+
+ template <typename T>
+ T& val() { return p->val<T>(); }
+ template <typename T>
+ void val(T const & v) { p->val<T>(v); }
+ bool hasval() { return p->hasval(); }
+ template <typename T>
+ bool hasvalof() { return p->hasvalof<T>(); }
+
+ operator concept*() const { return p; }
+ concept*& ptr() { return p; }
+ concept* const & ptr() const { return p; }
+
+ operator level0::ref const &() const { return *reinterpret_cast<level0::ref*>(this); }
+ operator level1::ref const &() const { return *reinterpret_cast<level1::ref*>(this); }
+ operator level2::ref const &() const { return *reinterpret_cast<level2::ref*>(this); }
+ operator level3::ref const &() const { return *reinterpret_cast<level3::ref*>(this); }
+ operator level4::ref const &() const { return *reinterpret_cast<level4::ref*>(this); }
+ operator level5::ref const &() const { return *reinterpret_cast<level5::ref*>(this); }
+ operator level6::ref const &() const { return *reinterpret_cast<level6::ref*>(this); }
+ operator level7::ref const &() const { return *reinterpret_cast<level7::ref*>(this); }
+ operator level8::ref const &() const { return *reinterpret_cast<level8::ref*>(this); }
+ operator level9::ref const &() const { return *reinterpret_cast<level9::ref*>(this); }
+
+ operator level0::ref &() { return *reinterpret_cast<level0::ref*>(this); }
+ operator level1::ref &() { return *reinterpret_cast<level1::ref*>(this); }
+ operator level2::ref &() { return *reinterpret_cast<level2::ref*>(this); }
+ operator level3::ref &() { return *reinterpret_cast<level3::ref*>(this); }
+ operator level4::ref &() { return *reinterpret_cast<level4::ref*>(this); }
+ operator level5::ref &() { return *reinterpret_cast<level5::ref*>(this); }
+ operator level6::ref &() { return *reinterpret_cast<level6::ref*>(this); }
+ operator level7::ref &() { return *reinterpret_cast<level7::ref*>(this); }
+ operator level8::ref &() { return *reinterpret_cast<level8::ref*>(this); }
+ operator level9::ref &() { return *reinterpret_cast<level9::ref*>(this); }
+
+ bool operator==(ref const & other) const { return self.p == other.p; }
+ bool operator!=(ref const & other) const { return self.p != other.p; }
+ bool operator<(ref const & other) const { return self.p < other.p; }
+
+ bool crucial() { return self.p->crucial(); }
+ bool crucial(ref type, ref target) { return self.p->crucial(type.p, target.p); }
+
+ void setcrucial() { self.p->setcrucial(); }
+ void setcrucial(ref type, ref target) { self.p->setcrucial(type.p, target.p); }
+
+protected:
+ concept * p;
+
+private:
+ template <typename val, typename It>
+ struct mutated_it
+ {
+ mutated_it(It const & it) : it(it) { }
+
+ using mutit = mutated_it<val, It>;
+
+ mutit & operator++() { ++ self.it; return self; }
+ mutit operator++(int i) { return self.it.operator++(i); }
+ mutit & operator--() { -- self.it; return self; }
+ mutit operator--(int i) { return self.it.operator--(i); }
+ bool operator==(mutit const & other) const { return self.it == other.it; }
+ bool operator!=(mutit const & other) const { return self.it != other.it; }
+
+ val & operator*() { return *(val*)&self.it.operator*(); }
+ val * operator->() { return (val*)self.it.operator->(); }
+
+ It & underlying() { return it; }
+
+ private:
+ It it;
+ };
+
+public:
+ struct array
+ {
+ using iterator = mutated_it<ref,typename concept::array::iterator>;
+ iterator begin() { return array.begin(); }
+ iterator end() { return array.end(); }
+
+ typename concept::array array;
+ };
+
+ struct links_t
+ {
+ using iterator = mutated_it<std::pair<ref,ref>,typename decltype(concept::links)::iterator>;
+ iterator begin() { return links.begin(); }
+ iterator end() { return links.end(); }
+
+ decltype(concept::links) & links;
+ };
+
+ void unlink(typename links_t::iterator it) { p->unlink(it.underlying()); }
+ bool crucial(typename links_t::iterator it) { return self.p->crucial(it.underlying()); }
+ void setcrucial(typename links_t::iterator it) { self.p->setcrucial(it.underlying()); }
+};
+
+template <typename ref>
+typename baseref<ref>::array baseref<ref>::getAll(ref const & type) const
+{
+ return {p->getAll(type.p)};
+}
+template <typename ref>
+typename baseref<ref>::links_t baseref<ref>::links() const
+{
+ return {p->links};
+}
+
+}
+}
diff --git a/intellect-framework-from-internet/starts/meaning-vm/level-0/common.hpp b/intellect-framework-from-internet/starts/meaning-vm/level-0/common.hpp
new file mode 100644
index 0000000..e7df0b7
--- /dev/null
+++ b/intellect-framework-from-internet/starts/meaning-vm/level-0/common.hpp
@@ -0,0 +1,65 @@
+#pragma once
+
+#define self (*this)
+
+// macro tools
+#define _macro_expand_to_args(...) __VA_ARGS__
+#define _macro_expand_to_comma_args(...) ,##__VA_ARGS__
+#define _macro_call(funcormacro, ...) funcormacro(__VA_ARGS__)
+#define _macro_remove_parens(args) _macro_expand_to_args args
+#define _macro_comma_remove_parens(args) _macro_expand_to_comma_args args
+
+#define _macro_expand_to_arg1(arg, ...) arg
+#define _macro_expand_to_arg2(_1, arg, ...) arg
+
+// macro argument iteration, from stackoverflow.com/questions/1872220
+#define _macro_for_each(firstfuncormacro,restfuncormacro,...) \
+ _macro_fe_get(_0,##__VA_ARGS__, \
+ _macro_fe_9,_macro_fe_8,_macro_fe_7,_macro_fe_6,_macro_fe_5,_macro_fe_4,_macro_fe_3,_macro_fe_2,_macro_fe_1,_macro_fe_0 \
+ )(firstfuncormacro,restfuncormacro,__VA_ARGS__)
+ #define _macro_fe_0(first,rest, ...)
+ #define _macro_fe_1(first,rest, x, ...) first(x)_macro_fe_0(rest,rest, __VA_ARGS__)
+ #define _macro_fe_2(first,rest, x, ...) first(x)_macro_fe_1(rest,rest, __VA_ARGS__)
+ #define _macro_fe_3(first,rest, x, ...) first(x)_macro_fe_2(rest,rest, __VA_ARGS__)
+ #define _macro_fe_4(first,rest, x, ...) first(x)_macro_fe_3(rest,rest, __VA_ARGS__)
+ #define _macro_fe_5(first,rest, x, ...) first(x)_macro_fe_4(rest,rest, __VA_ARGS__)
+ #define _macro_fe_6(first,rest, x, ...) first(x)_macro_fe_5(rest,rest, __VA_ARGS__)
+ #define _macro_fe_7(first,rest, x, ...) first(x)_macro_fe_6(rest,rest, __VA_ARGS__)
+ #define _macro_fe_8(first,rest, x, ...) first(x)_macro_fe_7(rest,rest, __VA_ARGS__)
+ #define _macro_fe_9(first,rest, x, ...) first(x)_macro_fe_8(rest,rest, __VA_ARGS__)
+ #define _macro_fe_get( \
+ _0,_1,_2,_3,_4,_5,_6,_7,_8,_9, \
+ name,...) name
+#define _macro_for_each_parens(firstfuncormacro,restfuncormacro,...) \
+ _macro_fe_get(_0,##__VA_ARGS__, \
+ _macro_fep9,_macro_fep8,_macro_fep7,_macro_fep6,_macro_fep5,_macro_fep4,_macro_fep3,_macro_fep2,_macro_fep1,_macro_fep0 \
+ )(firstfuncormacro,restfuncormacro,__VA_ARGS__)
+ #define _macro_fep0(first,rest, ...)
+ #define _macro_fep1(first,rest, x, ...) first x _macro_fep0(rest,rest, __VA_ARGS__)
+ #define _macro_fep2(first,rest, x, ...) first x _macro_fep1(rest,rest, __VA_ARGS__)
+ #define _macro_fep3(first,rest, x, ...) first x _macro_fep2(rest,rest, __VA_ARGS__)
+ #define _macro_fep4(first,rest, x, ...) first x _macro_fep3(rest,rest, __VA_ARGS__)
+ #define _macro_fep5(first,rest, x, ...) first x _macro_fep4(rest,rest, __VA_ARGS__)
+ #define _macro_fep6(first,rest, x, ...) first x _macro_fep5(rest,rest, __VA_ARGS__)
+ #define _macro_fep7(first,rest, x, ...) first x _macro_fep6(rest,rest, __VA_ARGS__)
+ #define _macro_fep8(first,rest, x, ...) first x _macro_fep7(rest,rest, __VA_ARGS__)
+ #define _macro_fep9(first,rest, x, ...) first x _macro_fep8(rest,rest, __VA_ARGS__)
+
+namespace intellect {
+namespace level0 {
+
+struct concept;
+struct ref;
+
+}
+namespace level1 { struct ref; }
+namespace level2 { struct ref; }
+namespace level3 { struct ref; }
+namespace level4 { struct ref; }
+namespace level5 { struct ref; }
+namespace level6 { struct ref; }
+namespace level7 { struct ref; }
+namespace level8 { struct ref; }
+namespace level9 { struct ref; }
+
+}
diff --git a/intellect-framework-from-internet/starts/meaning-vm/level-0/concept.cpp b/intellect-framework-from-internet/starts/meaning-vm/level-0/concept.cpp
new file mode 100644
index 0000000..66e5af1
--- /dev/null
+++ b/intellect-framework-from-internet/starts/meaning-vm/level-0/concept.cpp
@@ -0,0 +1,136 @@
+#include "concept.hpp"
+#include "errors.hpp"
+
+using namespace intellect::level0;
+
+#define selfref const_cast<concept*>(&self)
+
+concept* concept::id()
+{
+ return this;
+}
+
+void concept::link(concept* type, concept* target)
+{
+ if (type == 0 || target == 0) { throw null_reference(); }
+ links.insert({type, target});
+}
+
+bool concept::crucial(concept* type, concept* target)
+{
+ auto ls = links.equal_range(type);
+ bool wascrucial = false;
+ bool wasnotcrucial = false;
+ for (auto l = ls.first; l != ls.second; ++ l) {
+ if (l->second == target) {
+ if (crucialparts.count(l)) { wascrucial = true; }
+ else { wasnotcrucial = true; }
+ }
+ }
+ if (wascrucial && wasnotcrucial) { throw link_type_not_unique(selfref, type); }
+ if ((!wascrucial) && (!wasnotcrucial)) { throw no_such_link_type(selfref, type); }
+ return wascrucial;
+}
+
+void concept::setcrucial(concept* type, concept* target)
+{
+ auto ls = links.equal_range(type);
+ for (auto l = ls.first; l != ls.second; ++ l) {
+ if (l->second == target) {
+ if (!crucialparts.count(l)) {
+ setcrucial(l);
+ return;
+ }
+ }
+ }
+ throw no_such_link_type(selfref, type);
+}
+
+void concept::unlink(concept* type, concept* target)
+{
+ auto ls = links.equal_range(type);
+ bool wascrucial = false;
+ for (auto l = ls.first; l != ls.second; ++ l) {
+ if (l->second == target) {
+ if (crucialparts.count(l)) { wascrucial = true; continue; }
+ links.erase(l);
+ return;
+ }
+ }
+ if (wascrucial) { throw crucial_link_type_target(selfref, type, target); }
+ throw no_such_link_type_target(selfref, type, target);
+}
+
+void concept::unlink(concept* type)
+{
+ auto ls = links.equal_range(type);
+ if (ls.first == ls.second) {
+ throw no_such_link_type(selfref, type);
+ }
+ auto mid = ls.first;
+ ++ mid;
+ if (mid != ls.second) {
+ throw link_type_not_unique(selfref, type);
+ }
+ unlink(ls.first);
+}
+
+void concept::unlink(decltype(links)::iterator it)
+{
+ if (crucialparts.count(it)) {
+ throw crucial_link_type_target(selfref, it->first, it->second);
+ }
+ links.erase(it++);
+}
+
+bool concept::linked(concept* type) const
+{
+ return links.count(type) > 0;
+}
+
+bool concept::linked(concept* type, concept* target) const
+{
+ for (concept* t : getAll(type)) {
+ if (t == target) {
+ return true;
+ }
+ }
+ return false;
+}
+
+concept::array concept::getAll(concept* type) const
+{
+ array ret;
+ for (
+ auto range = links.equal_range(type);
+ range.first != range.second;
+ ++ range.first
+ ) {
+ ret.emplace_back(range.first->second);
+ }
+ return ret;
+}
+
+concept* concept::get(concept* type) const
+{
+ auto result = links.equal_range(type);
+ if (result.first == result.second) {
+ throw no_such_link_type(selfref, type);
+ }
+ auto test = result.first;
+ ++ test;
+ if (test != result.second) {
+ throw link_type_not_unique(selfref, type);
+ }
+ return result.first->second;
+}
+
+void concept::set(concept* type, concept* target)
+{
+ if (linked(type)) {
+ unlink(type);
+ }
+ link(type, target);
+}
+
+
diff --git a/intellect-framework-from-internet/starts/meaning-vm/level-0/concept.hpp b/intellect-framework-from-internet/starts/meaning-vm/level-0/concept.hpp
new file mode 100644
index 0000000..833e417
--- /dev/null
+++ b/intellect-framework-from-internet/starts/meaning-vm/level-0/concept.hpp
@@ -0,0 +1,73 @@
+#pragma once
+
+#include "common.hpp"
+
+#include <any>
+#include <map>
+#include <unordered_set>
+#include <vector>
+
+namespace intellect {
+namespace level0 {
+
+struct concept
+{
+ // a concept is made of concept-typed links to other concepts
+ std::multimap<concept*,concept*> links;
+ // and optional associated arbitrary data
+ std::any data;
+
+ using array = std::vector<concept*>;
+
+ concept* id();
+
+ void link(concept* type, concept* target);
+ void unlink(concept* type, concept* target);
+ void unlink(concept* type);
+ void unlink(decltype(links)::iterator it);
+
+ bool crucial() { return iscrucial || crucialparts.size(); }
+ bool crucial(concept* type, concept* target);
+ bool crucial(decltype(links)::iterator it) { return crucialparts.count(it); }
+ void setcrucial() { iscrucial = true; }
+ void setcrucial(concept* type, concept* target);
+ void setcrucial(decltype(links)::iterator it) { crucialparts.insert(it); }
+
+ bool linked(concept* type) const;
+ bool linked(concept* type, concept* target) const;
+
+ array getAll(concept* type) const;
+
+ // get and set enforce that only 1 link of a given type is present
+ concept* get(concept* type) const;
+ void set(concept* type, concept* target);
+
+ template <typename T>
+ T & vget(concept* type) const { return get(type)->val<T>(); }
+
+ template <typename T>
+ T & val() { return std::any_cast<T&>(data); }
+
+ template <typename T>
+ void val(T const & v) { data = v; }
+
+ bool hasval() { return data.has_value(); }
+
+ template <typename T>
+ bool hasvalof() { return hasval() && data.type() == typeid(T); }
+
+private:
+ // for permanence
+ bool iscrucial;
+ struct linksit_hash
+ {
+ size_t operator()(decltype(links)::iterator const &it) const
+ {
+ return std::hash<decltype(&*it)>()(&*it);
+ }
+ };
+ std::unordered_set<decltype(links)::iterator, linksit_hash> crucialparts;
+};
+
+}
+}
diff --git a/intellect-framework-from-internet/starts/meaning-vm/level-0/errors.hpp b/intellect-framework-from-internet/starts/meaning-vm/level-0/errors.hpp
new file mode 100644
index 0000000..e599261
--- /dev/null
+++ b/intellect-framework-from-internet/starts/meaning-vm/level-0/errors.hpp
@@ -0,0 +1,101 @@
+#pragma once
+#include "concept.hpp"
+
+#include <stdexcept>
+
+namespace intellect {
+namespace level0 {
+
+struct no_such_link_type : public std::out_of_range
+{
+ no_such_link_type(concept* source, concept* type)
+ : std::out_of_range("no such concept link type"),
+ source(source),
+ type(type)
+ { }
+
+ concept* const source;
+ concept* const type;
+};
+
+struct no_such_link_type_target : public std::out_of_range
+{
+ no_such_link_type_target(concept* source, concept* type, concept* target)
+ : std::out_of_range("no such concept link type and target"),
+ source(source),
+ type(type),
+ target(type)
+ { }
+
+ concept* const source;
+ concept* const type;
+ concept* const target;
+};
+
+struct crucial_link_type_target : public std::out_of_range
+{
+ crucial_link_type_target(concept* source, concept* type, concept* target)
+ : std::out_of_range("concept part is crucial"),
+ source(source),
+ type(type),
+ target(type)
+ { }
+
+ concept* const source;
+ concept* const type;
+ concept* const target;
+};
+
+struct crucial_concept : public std::invalid_argument
+{
+ crucial_concept(concept* topic)
+ : std::invalid_argument("concept is crucial"),
+ topic(topic)
+ { }
+
+ concept* const topic;
+};
+
+struct link_type_not_unique : public std::invalid_argument
+{
+ link_type_not_unique(concept* source, concept* type)
+ : std::invalid_argument("more than one such concept link type"),
+ source(source),
+ type(type)
+ { }
+
+ concept* const source;
+ concept* const type;
+};
+
+struct still_referenced_by : public std::invalid_argument
+{
+ still_referenced_by(concept* topic, concept* referrer)
+ : std::invalid_argument("concept is still referenced"),
+ topic(topic),
+ referrer(referrer)
+ { }
+
+ concept* const topic;
+ concept* const referrer;
+};
+
+struct no_such_concept : public std::invalid_argument
+{
+ no_such_concept(concept* topic)
+ : std::invalid_argument("no such concept reference"),
+ topic(topic)
+ { }
+
+ concept* const topic;
+};
+
+struct null_reference : public std::invalid_argument
+{
+ null_reference()
+ : std::invalid_argument("null reference")
+ { }
+};
+
+}
+}
diff --git a/intellect-framework-from-internet/starts/meaning-vm/level-0/level-0.hpp b/intellect-framework-from-internet/starts/meaning-vm/level-0/level-0.hpp
new file mode 100644
index 0000000..56cd7dd
--- /dev/null
+++ b/intellect-framework-from-internet/starts/meaning-vm/level-0/level-0.hpp
@@ -0,0 +1,7 @@
+#pragma once
+
+#include "common.hpp"
+#include "concept.hpp"
+#include "errors.hpp"
+#include "memorystore.hpp"
+#include "ref.hpp"
diff --git a/intellect-framework-from-internet/starts/meaning-vm/level-0/memorystore.cpp b/intellect-framework-from-internet/starts/meaning-vm/level-0/memorystore.cpp
new file mode 100644
index 0000000..24e91b0
--- /dev/null
+++ b/intellect-framework-from-internet/starts/meaning-vm/level-0/memorystore.cpp
@@ -0,0 +1,179 @@
+#include "memorystore.hpp"
+#include "concept.hpp"
+#include "errors.hpp"
+#include "ref.hpp"
+
+#include <memory>
+#include <unordered_map>
+
+namespace intellect {
+namespace level0 {
+
+static auto & index()
+{
+ static std::unordered_map<ref, std::unique_ptr<concept>, std::hash<concept*>> index;
+ return index;
+}
+
+
+namespace concepts {
+ ref allocator() { static ref ret = basic_alloc(); return ret; }
+ ref allocates() { static ref ret = basic_alloc(); return ret; }
+ ref allocations() { static ref ret = basic_alloc(); return ret; }
+ ref level0allocations() { static ref ret = basic_alloc(); return ret; }
+}
+
+struct init { init()
+{
+ concepts::allocator().link(concepts::allocator(), concepts::level0allocations());
+ concepts::level0allocations().link(concepts::allocates(), concepts::allocator());
+
+ concepts::allocates().link(concepts::allocator(), concepts::level0allocations());
+ concepts::level0allocations().link(concepts::allocates(), concepts::allocates());
+
+ concepts::allocations().link(concepts::allocator(), concepts::level0allocations());
+ concepts::level0allocations().link(concepts::allocates(), concepts::allocations());
+
+ concepts::level0allocations().link(concepts::allocator(), concepts::level0allocations());
+ concepts::level0allocations().link(concepts::allocates(), concepts::level0allocations());
+} } _init;
+
+ref basic_alloc(std::any data)
+{
+ ref r = new concept();
+ r.ptr()->data = data;
+ index().emplace(r, r.ptr());
+ return r;
+}
+
+ref alloc(ref source, std::any data)
+{
+ ref r = basic_alloc(data);
+ alloc(r, source);
+ return r;
+}
+
+void alloc(ref r, ref source)
+{
+ r.link(concepts::allocator(), source);
+ source.link(concepts::allocates(), r);
+}
+
+void realloc(ref r, ref newsource)
+{
+ ref oldsource = r.get(concepts::allocator());
+ alloc(r, newsource);
+ dealloc(r, oldsource);
+}
+
+static concept* referenced(ref r, concept* source = 0) {
+ for (auto & r2pair : index()) {
+ ref r2 = r2pair.first;
+ if (r2.ptr() == source) {
+ continue;
+ }
+ if (r2 == r) {
+ continue;
+ }
+ for (auto & l : r2.links()) {
+ if (ref(l.first) == r) {
+ return r2;
+ }
+ if (ref(l.second) == r) {
+ return r2;
+ }
+ }
+ }
+ return 0;
+}
+
+void basic_dealloc(ref r)
+{
+ if (r.crucial()) { throw crucial_concept(r); }
+ auto it = index().find(r);
+ if (it == index().end()) { throw no_such_concept(r); }
+
+ concept * referenced = intellect::level0::referenced(r);
+ if (referenced) {
+ throw still_referenced_by(r, referenced);
+ }
+
+ index().erase(it);
+}
+
+void dealloc_from(ref source)
+{
+ std::remove_reference<decltype(index())>::type forgotten;
+
+ auto ours = source.getAll(concepts::allocates());
+ for (auto allocation : ours) {
+ if (allocation.crucial()) { throw crucial_concept(allocation); }
+ source.unlink(concepts::allocates(), allocation);
+ allocation.unlink(concepts::allocator(), source);
+ if (allocation.linked(concepts::allocator())) { continue; }
+
+ auto it = index().find(allocation);
+ if (it != index().end()) {
+ forgotten.insert(index().extract(it));
+ }
+ }
+ try {
+ for (auto allocation : ours ) {
+ for (auto suballocation : allocation.getAll(concepts::allocates())) {
+ // check for this link to find subgroups
+ throw still_referenced_by(allocation, suballocation);
+ }
+ }
+ for (auto ghost : ours) {
+ concept * referenced = intellect::level0::referenced(ghost, source);
+ if (referenced) {
+ throw still_referenced_by(ghost, referenced);
+ }
+ }
+ } catch(...) {
+ // NOTE: this doesn't rebuild deallocated subgroups, but that could be done
+ // by returning them.
+ index().merge(forgotten);
+ for (auto allocation : ours) {
+ source.link(concepts::allocates(), allocation);
+ allocation.link(concepts::allocator(), source);
+ }
+ throw;
+ }
+
+ // concepts in forgotten will be deallocated when they leave scope
+ // note: scoped allocation is just a plan to forget (at the end of a { } block)
+}
+
+void dealloc(ref r, ref source)
+{
+ auto it = index().find(r);
+ if (it == index().end()) { throw no_such_concept(r); }
+
+ source.unlink(concepts::allocates(), r);
+ r.unlink(concepts::allocator(), source);
+ if (r.linked(concepts::allocator())) { return; }
+
+ try {
+ if (r.crucial()) { throw crucial_concept(r); }
+ dealloc_from(r);
+ concept * referenced = intellect::level0::referenced(r, source);
+ if (referenced) {
+ throw still_referenced_by(r, referenced);
+ }
+
+ index().erase(it);
+ } catch(...) {
+ source.link(concepts::allocates(), r);
+ r.link(concepts::allocator(), source);
+ throw;
+ }
+}
+
+std::size_t allocated()
+{
+ return index().size();
+}
+
+}
+}
diff --git a/intellect-framework-from-internet/starts/meaning-vm/level-0/memorystore.hpp b/intellect-framework-from-internet/starts/meaning-vm/level-0/memorystore.hpp
new file mode 100644
index 0000000..f416540
--- /dev/null
+++ b/intellect-framework-from-internet/starts/meaning-vm/level-0/memorystore.hpp
@@ -0,0 +1,63 @@
+#pragma once
+
+#include "common.hpp"
+
+#include <any>
+
+namespace intellect {
+namespace level0 {
+
+// self-reference loops are real.
+//
+// one person can feel urgent about issue A, and act on this urgency to engage
+// another person around B, who acts in a different way to someone else, eventually
+// cycling back to stress that stimulates the original person to feel more urgent
+// about issue A.
+// human behavior can make arbitrary positive or negative feedback loops.
+//
+// here in memory allocation, i've designed a system intended to reduce such loops
+// by encouraging my usage to be a certain way, but it still readily provides for
+// them.
+//
+// in process expansion / simple thought, we also have the issue of recursion.
+// if we trust a task to complete, and it ends up triggering itself in a subcontext,
+// we could wait forever.
+//
+// the solution to many of these things is to recognize repetition in systems.
+// we also become skeptical as things continue constantly. we expect to develop
+// some level of understanding that they will shrink, or we stop them and try
+// something else.
+
+// A solution to recursion appears to involve emotional expression.
+// Too much recursion maps acceptably to frustration of the process doing the repetitive task.
+// The building unmet need for effectiveness should influence other decision-making processes
+// if nothing else is larger. Notably if the caller needs timeliness, they won't get this
+// if the callee(s) do not have effectiveness.
+// propose: raise frustration[effectiveness] when calling self or repeating same behavior
+// propose: raise frustration[timeliness] if subprocess takes long (say every 400ms)
+// ideally raising an emotional expression should be associated with what caused it
+// and how the the universe might change to fix it.
+// decision-making processes need to judge what is relevent to them: a product of how well
+// they can help something and how strongly it is needed.
+
+namespace concepts {
+
+extern ref allocator(); // link shows what is holding something alive
+extern ref allocates(); // link shows what is being held alive
+
+extern ref allocations(); // to use as a basic allocator for simple things
+extern ref level0allocations(); // allocator for concepts internal to level0
+
+}
+
+ref basic_alloc(std::any data = {});
+void basic_dealloc(ref allocated);
+
+ref alloc(ref allocator, std::any data = {}); // new concept
+void alloc(ref allocated, ref allocator); // extra ownership for concept
+void realloc(ref allocated, ref allocator); // move ownership for concept to allocator
+void dealloc(ref allocated, ref allocator); // remove ownership for concept
+std::size_t allocated();
+
+}
+}
diff --git a/intellect-framework-from-internet/starts/meaning-vm/level-0/ref.cpp b/intellect-framework-from-internet/starts/meaning-vm/level-0/ref.cpp
new file mode 100644
index 0000000..513d3ce
--- /dev/null
+++ b/intellect-framework-from-internet/starts/meaning-vm/level-0/ref.cpp
@@ -0,0 +1,33 @@
+#include "ref.hpp"
+#include "concept.hpp"
+#include "errors.hpp"
+#include "memorystore.hpp"
+
+#include <iomanip>
+#include <ostream>
+#include <sstream>
+
+using namespace intellect::level0;
+using namespace concepts;
+
+std::string ref::dump(ref skipmarkertype, ref skipmarkertarget)
+{
+ if (self.linked(skipmarkertype, skipmarkertarget)) {
+ return {};
+ }
+ std::stringstream ss;
+ ss << std::hex << (size_t)ptr() << ":" << std::endl;
+ for (auto & link : self.links()) {
+ if (link.first.linked(allocator(), level0allocations())) { continue; }
+ ss << " " << (size_t)link.first.ptr() << ": " << (size_t)link.second.ptr() << std::endl;
+ }
+ self.link(skipmarkertype, skipmarkertarget);
+ for (auto & link : self.links()) {
+ if (link.first.linked(allocator(), level0allocations())) { continue; }
+ if (link.first == skipmarkertype && link.second == skipmarkertarget) {
+ continue;
+ }
+ ss << link.second.dump(skipmarkertype, skipmarkertarget);
+ }
+ return ss.str();
+}
diff --git a/intellect-framework-from-internet/starts/meaning-vm/level-0/ref.hpp b/intellect-framework-from-internet/starts/meaning-vm/level-0/ref.hpp
new file mode 100644
index 0000000..ff55355
--- /dev/null
+++ b/intellect-framework-from-internet/starts/meaning-vm/level-0/ref.hpp
@@ -0,0 +1,19 @@
+#pragma once
+
+#include "common.hpp"
+#include "baseref.hpp"
+
+#include <string>
+
+namespace intellect {
+namespace level0 {
+
+struct ref : public baseref<ref>
+{
+ ref(concept *p) : baseref(p) { }
+
+ std::string dump(ref skipmarkertype, ref skipmarkertarget);
+};
+
+}
+}