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
|
#include "funcs.hpp"
#include "../level-1/sugar.hpp"
#include "ref.hpp"
#include "concepts.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::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);
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);
for (ref const & arg : args) {
if (!posinf.linked(next-information)) {
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)) {
throw a(information-needed).link
(concepts::habit, habit,
information, posinf);
}
ref::context().set(posinf[information], posinf[assume]);
}
habit.fun<ref>()(ref::context());
posinf = habit.get(information-needed);
while (posinf.linked(next-information)) {
posinf = posinf[next-information];
ref::context().unlink(posinf[information]);
}
if (ref::context().linked(result)) {
ref ret = ref::context().get(result);
ref::context().unlink(result, ret);
return ret;
}
return nothing;
}
ref dohabit(ref habit, std::initializer_list<std::initializer_list<ref>> pairs)
{
using namespace concepts;
// TODO: subcontexts or call instances
ref ctx = ref::context();
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())) {
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 {
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);
//}
if (ctx.linked(result)) {
ref ret = ctx.get(result);
ctx.unlink(result, ret);
return ret;
}
return nothing;
}
}
}
|