1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
#include "funcs.hpp"
#include "../level-1/sugar.hpp"
#include "ref.hpp"
#include "concepts.hpp"
#include "habits.hpp"
namespace intellect {
using namespace level1;
namespace level2 {
using namespace concepts;
ref & context()
{
static thread_local auto ctx = a(concepts::context);
return ctx;
}
//ref makehabit(ref name, std::list<ref> argnames, std::any
ref makehabit(ref name, std::initializer_list<ref> argnames, std::function<void(ref)> code)
{
ref habit = level1::a(concepts::habit, name);
ref infn = a(habit-information-needed);
habit.set(information-needed, infn);
//habit.set(concepts::habit, concepts::habit);
ref posinf = infn;
for (auto argname : argnames) {
ref nextinf = a(habit-information);
nextinf.set(information, argname);
posinf.set(next-information, nextinf);
posinf = nextinf;
if (!infn.linked(argname)) {
infn.set(argname, nextinf);
} else {
if (!infn.get(argname).isa(habit-information)) {
throw a(unexpected-concepts::habit-information-concepts::name)
.link(concepts::name, argname)
.link(concepts::habit, habit);
}
}
}
habit.fun(code);
return habit;
}
void habitassume(ref habit, ref information, ref assumption)
{
ref infn = habit.get(concepts::information-needed);
infn.get(information).set(assume, assumption);
}
ref dohabit(ref habit, std::initializer_list<ref> args)
{
using namespace concepts;
ref posinf = habit.get(information-needed);
ref subctx = makeconcept();
subctx.link("outer-context", ref::context());
ref::context() = subctx;
for (ref const & arg : args) {
if (!posinf.linked(next-information)) {
ref::context() = subctx.get("outer-context");
conceptunmake(subctx);
throw an(unexpected-information).link
(concepts::habit, habit,
information-value, arg);
}
posinf = posinf[next-information];
// TODO: subcontexts or call instances
ref::context().set(posinf[information], arg);
}
while (posinf.linked(next-information)) {
posinf = posinf[next-information];
if (!posinf.linked(assume)) {
ref::context() = subctx.get("outer-context");
conceptunmake(subctx);
throw a(information-needed).link
(concepts::habit, habit,
information, posinf);
}
ref::context().set(posinf[information], posinf[assume]);
}
ref::context().set("self", habit);
habit.fun<ref>()(ref::context());
posinf = habit.get(information-needed);
while (posinf.linked(next-information)) {
posinf = posinf[next-information];
ref::context().unlink(posinf[information]);
}
ref ret = nothing;
if (ref::context().linked(result)) {
ret = ref::context().get(result);
ref::context().unlink(result, ret);
}
ref::context() = subctx.get("outer-context");
conceptunmake(subctx);
return ret;
}
ref dohabit(ref habit, std::initializer_list<std::initializer_list<ref>> pairs)
{
using namespace concepts;
// TODO: subcontexts or call instances
ref ctx = makeconcept();
ctx.link("outer-context", ref::context());
ref::context() = ctx;
ref infn = habit.get(information-needed);
std::map<ref, ref> provided;
for (auto pair : pairs) {
auto second = pair.begin(); ++ second;
if (!infn.linked(*pair.begin())) {
ref::context() = ctx.get("outer-context");
conceptunmake(ctx);
throw an(unexpected-information).link
(concepts::habit, habit,
information, *pair.begin(),
information-value, *second);
}
if (provided.count(*pair.begin())) { throw "multiple instances same name not implemented here"; }
provided[*pair.begin()] = *second;
}
ref nextinf = infn;
while (nextinf.linked(next-information)) {
nextinf = nextinf.get(next-information);
ref inf = nextinf.get(information);
if (!provided.count(inf)) {
if (nextinf.get(assume)) {
ctx.link(inf, nextinf.get(assume));
} else {
ref::context() = ctx.get("outer-context");
conceptunmake(ctx);
throw a(information-needed).link
(concepts::habit, habit,
information, inf);
}
} else {
ctx.link(inf, provided[inf]);
}
}
habit.fun<ref>()(ctx);
nextinf = infn;
while (nextinf.linked(next-information)) {
nextinf = nextinf.get(next-information);
ref inf = nextinf.get(information);
if (provided.count(inf)) {
ctx.unlink(inf, provided[inf]);
} else {
ctx.unlink(inf, nextinf.get(assume));
}
}
//for (auto pair : pairs) {
// auto second = pair.begin(); ++ second;
// ctx.unlink(pair.begin(), second);
//}
ref ret = nothing;
if (ctx.linked(result)) {
ret = ctx.get(result);
ctx.unlink(result, ret);
}
ref::context() = ctx.get("outer-context");
conceptunmake(ctx);
return ret;
}
}
}
|