-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathophanim.lua
More file actions
1629 lines (1550 loc) · 98.1 KB
/
ophanim.lua
File metadata and controls
1629 lines (1550 loc) · 98.1 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--TODOs:
-- 1. Host representation. Lua have quite messy syntax and context, we need to nicely wrap this up inside some Manifest or Frame.
-- 2. Just finish manifests (Especially Error, to check if we getting stuck in halt)
-- 3. Tokens should keep track of current evaluated data by having access to the Host device (like Artifacts do), but here in PoC we just refere to it directly through FISH due to "it's convinient" and "that stuff is depandant on host"
return (function ()
--Frontend: NegI - Negotiation Interface/Negate Identity (the interface, what is developed, that's the front name)
--Backend: OPHANIM - Ontological Polymorphic Host for Authority and Negotiation Interface Management (the substrate, NegI implementation)
local pprint = require("pprint") -- remove after fixing problems
--local ltr = require("luatrace") -- remove after fixing problems
-- This works more or less as ship of thesus, OPHANIM provides common interfaces for other manifests to communicate with each other in platform agnostic way
local newstate = function () -- something similar to lua_newstate but for OPHANIM
local new_conv = function (c, ...) --helps chaining like resolve_chain("salt")(4)(5)(1).r
local h = {c = c,r = {...}}
setmetatable(h, {
__call = function (self, ...)
return new_conv(c, c(...))
end })
return h
end
local table_invert = function (t)
local s={}
for k,v in pairs(t) do
s[v]=k
end
return s
end
local bimap_write = function(bimap, view_key, index, value)
-- Input validation
local reverse_key = (view_key):reverse() -- it's fine because code follows this convention
if type(bimap) ~= "table" or not view_key or not reverse_key then
error("Invalid bimap or keys provided", 2)
end
local fwd_view = bimap[view_key]
local rev_view = bimap[reverse_key]
if not fwd_view or not rev_view then
error("Invalid view keys provided", 2)
end
-- DELETION PATH (value is nil)
if value == nil then
if index == nil then
error("expected index, got nil", 2)
end
local old_value = fwd_view[index]
if old_value ~= nil then
fwd_view[index] = nil
rev_view[old_value] = nil
end
return
end
-- INSERTION / UPDATE PATH
-- 1. Clean up any existing mapping for this index
local old_value = fwd_view[index]
if old_value ~= nil then
rev_view[old_value] = nil
end
-- 2. Clean up any existing mapping for this value (enforce 1-to-1)
local old_index = rev_view[value]
if old_index ~= nil then
fwd_view[old_index] = nil
end
-- 3. Write the new mapping
fwd_view[index] = value
rev_view[value] = index
end
local FLESH -- forward declaration, because for now I just need to make it work
FLESH = { -- stands for "Fixed Local Environment Shared Handles"
FISH = { -- "Fixed Internal State Holder" - used by particular manifests to pass around some data. Could be avoided via KES, but those values are quite commonly used, so it will influence performance.
-- probably reserved for parser tokens, but I might leave that up to Tokens themselves
},
KES = { -- "Knowledge Environment State" (considered finished, until bugs will be found)
layers = {{d = 1,h = {r={},i={}},s = {a={},e={},r={}},c = {ob={},bo={}}}}, -- stack of references, string names ready for free (initial layer is preloaded)
relevance = { -- tracks what currently available in active context
dl = {[1]=1}, -- Array<depth: Integer, layer_id: Integer>
ld = {[1]=1}}, -- Map<layer_id: Integer, depth: Integer> stores which layers are relevent to current context, mostly used as Set<layer_id: Integer>
isolations = { -- tracks to which layer unquotation should latch. basically ordered Set<depth: Integer>
["od"] = {}, -- Array<order: Integer, depth: Integer> - we use this to iterate through them
["do"] = {}}, -- Map<depth: Integer, order: Integer> - that the depths of those, also "do" key is the reason I hate keywords
-- we changed assumptions, now labels will store both numeric and string labels, while bindings won't be acessible directly for the user
labels = { -- tracks public(String)/anonymic(Number) binding names. BiMap<label: String|Number, bind: Number> holds labeled refences, bimap/not in bindings - because it's an extension that exist only for the user
bl = {}, -- Array<bind : Integer, label: String|Integer>
lb = {}}, -- Map<label: String|Integer, bind : Integer>
bindings = {}, -- Array<bind: Integer, {records: Map<layer_id: Integer, entry: Manifest>, order: BiMap<layer_id: Integer, order: Integer>}> holds references to data
--bindings = ltr.trace.table({},nil,--function (c,t,k) print(string.format("READ AT: %s on %s, key %s:%s", c.name or "<ANON>", c.currentline, tostring(k), tostring(type(k)))) end,
-- function (c,t,k,v)
-- print(string.format("WRITE AT: %s on %s, key %s:%s, val %s:%s", c.name or "<ANON>", c.currentline, tostring(k), tostring(type(k)), tostring(v), tostring(type(v))))
-- end
--),
resolve = function (self, ref, framed) -- note that strings are names for indicies, "framed" is used by Frame to get data from current Frame
if (type(ref) ~= "string" and type(ref) ~= "number") then
error("OPHANIM: FLESH.KES:resolve - invalid argument, expected number or string", 2)
end
local rt
if type(ref) == "number" then -- anonymic bindings are always local
framed = true
rt = self.bindings[self.layers[#self.layers].c.ob[ref]]
else
rt = self.bindings[self.labels.lb[ref]]
end
if (rt == nil) then return end -- the environment don't know about this binding so we exit early, this line is optional
local m, fh -- data, from_here
if (#rt.order.ol > #self.relevance.dl) or framed then -- NOTE: records store changes per each layer, so this checks if it's effective to do vertical or horizontal search traversal
for d = #self.relevance.dl, framed and #self.relevance.dl or 1, -1 do -- inverse order, because we pick fresh ones, in order to hit early
local l = self.relevance.dl[d]
if rt.order.lo[l] then
m = rt.records[l] -- we do not use rt.records[e], because it may contain nil, due to "namespace" reservation
fh = (#self.layers == l)
break end
end
else
for o = #rt.order.ol, 1, -1 do -- inverse order, because we pick fresh ones, in order to hit early
local l = rt.order.ol[o]
if self.relevance.ld[l] then
m = rt.records[l]
fh = (#self.layers == l)
break end
end
end
return m, fh -- Manifest(lua table, that represnts it) and if this info from current layer
end,
push_layer = function (self, parent_depth, isolated) -- adds new layer
-- we make an exception for root layer, because there's nothing to isolate against
-- initial layer is preloaded, but I still add it if user decide to pop the root layer and there will be those who would like to do that for the fun of it (hi tsoding)
local not_root = (#self.relevance.dl > 0)
--local parent = self.relevance.dl[parent_depth]
if (not_root) then
if (type(parent_depth) ~= "number") then error("OPHANIM: FLESH.KES:push_layer - parent_depth<number> expected, got "..type(parent), 2) end -- I also think that root layers should be definable if no parent specified
if (parent_depth > self.layers[#self.layers].d or parent_depth < 1) then error("invalid parent depth", 2) end
--if (not parent) then error("can't find parent", 2) end
end
local l = { -- new layer data
d = ((not_root) and parent_depth or 0) + 1, -- new layer depth
h = {r={},i={}}, -- those are hidden layers (r - relevance, i - isolation)
s = {a={},e={},r={}}, -- staged entries data: aliases(aka refs), entries and reserved aliases
c = {ob={},bo={}}} -- `c` is BiMap<order: Integer, bind: Integer> references relvant to this context layer
--if (#self.relevance.dl > l.d) then
for d = #self.relevance.dl, l.d, -1 do -- exclude all layers between parent and new layer depths via depth
l.h.r[#l.h.r+1] = self.relevance.dl[d] -- hide layers (we can ask depth form them directly)
bimap_write(self.relevance, "dl", d, nil) -- removing irrelevant layers
if self.isolations["do"][d] then -- check if there is isolation
l.h.i[#l.h.i+1] = d -- hide isolations (we can ask depth form them directly)
bimap_write(self.isolations, "do", d, nil) end end-- end -- removing irrelevant isolations
self.layers[#self.layers+1] = l
bimap_write(self.relevance, "ld", #self.layers, l.d)
if isolated then bimap_write(self.isolations, "od", #self.isolations.od+1, l.d) end -- isolated (external binding resolving, causes it to use resolving oblivious to effects from here)
return #self.layers -- used if Sequence will define another Sequence
end,
pop_layer = function (self, frame) -- P.S. while push and pop suggest stack structure, this isn't purely just that due to parent detours
if (#self.layers <= 0) then error("FLESH.KES:pop_layer - no layers to pop", 2) end
local l = self.layers[#self.layers]--; print("layer: "..tostring(#self.layers))
local db = self.bindings
local frame_state = {labels = {lb={},bl={}}, bindings = {}}
local frame_entry = frame and function (self, b, rt)
frame_state.bindings[#frame_state.bindings+1] = {delta = rt.records[#self.layers]}
bimap_write(frame_state.labels, "bl", #frame_state.bindings, self.labels.bl[b])
end or function (self, b, rt) end
for _,b in ipairs(l.c.ob) do -- removing references from bindings
local rt = db[b]
frame_entry(self, b, rt)
rt.records[#self.layers] = nil
if (#self.layers == rt.order.ol[#rt.order.ol]) then
bimap_write(rt.order, "lo", #self.layers, nil)
else error("KES:pop_layer - invalid layer in unload transaction query. [Z_Z] Currently I'm thinking to keep it as user error or make code for handling this.", 2) end
if #rt.order.ol <= 0 then db[b] = nil; bimap_write(self.labels, "bl", b, nil) end end
if self.isolations["do"][l.d] then bimap_write(self.isolations, "do", l.d, nil) end -- lift isolation sandbox (if there is any)
bimap_write(self.relevance, "ld", #self.layers, nil) -- removed layer is irrelevant, removing it
local hidden_layers = l.h -- there is always hidden entries
for _,e in ipairs(hidden_layers.r) do -- restoring hidden context relevance
bimap_write(self.relevance, "ld", e, self.layers[e].d) end
for i = #hidden_layers.i, 1, -1 do -- restoring hidden context isolations
bimap_write(self.isolations, "od", #self.isolations.od + 1, hidden_layers.i[i]) end
self.layers[#self.layers] = nil -- removing layer
return frame and frame_state or nil
end,
morph_layer = function (self, parent, isolated, context) -- push_layer, but just rebases current layer. for "pass" use. this is to remove migrate from pop_layer and rework context in push_layer
-- I also not sure if `pass` should keep previous context layer, which this function is implies to do, because `pass` created with explicit data transfer in mind,
-- leaving something in context is implicit data transfer, which should be another explicit thing on it's own (like snapshot into frames?)
end,
unquote_parent = function (self, parent_depth) -- get parent of unquotation
local not_root = (#self.relevance.dl > 0)
--local parent = self.relevance.dl[parent_depth]
if (not_root) then
if (type(parent_depth) ~= "number") then error("OPHANIM: FLESH.KES:push_layer - parent_depth<number> expected, got "..type(parent), 2) end -- I also think that root layers should be definable if no parent specified
if (parent_depth > self.layers[#self.layers].d or parent_depth < 1) then error("invalid parent depth", 2) end
end
parent_depth = parent_depth or 0 -- parent depth
local iso_depth_i, iso_depth = #self.isolations.od, nil
while ((self.isolations.od[iso_depth_i] or 0) > parent_depth) do -- we check if layer definition was outside of isolation. also binary search could be applied here
iso_depth = self.isolations.od[iso_depth_i]
iso_depth_i = iso_depth_i - 1 end
if (iso_depth) then -- if dynamic defined outside isolation, then it shouldn't consider effect of isolation
return iso_depth - 1 -- find parent of layer outside of isolation
else
return self.layers[#self.layers].d
end
end,
get_context = function (self) return #self.layers end, -- will be probably be deprecated
get_depth = function (self) return self.layers[#self.layers].d end, -- used by Membranes to memorize context for later use
write_entry = function (self, ref, m) -- reference : Number|String, [manifest: Any|Nil]
local db = self.bindings
local c = self.layers[#self.layers].c
local b = (#db + 1)
local order = (#c.ob + 1)
if ref then
if (type(ref) ~= "string") then error("on ref expected string or nil, got "..type(ref), 2) end
b = self.labels.lb[ref] or b
bimap_write(self.labels, "lb", ref, b)
else ref = order end
db[b] = db[b] or { records = {}, order = {ol = {}, lo = {}} }
db[b].records[#self.layers] = m -- note that nil will reserve the place on layer
--TODO: unite the check
if not db[b].order.lo[#self.layers] then -- do we need to add new or just update the binding
bimap_write(db[b].order, "ol", #(db[b].order.ol) + 1, #self.layers)
end
if not c.bo[b] then -- if it's new binding for this context
bimap_write(c, "bo", b, order)
end
return ref
end, -- entry writes could only happen in current context
stage_resolve_id = function (self, ref) return self.layers[#self.layers].s.a[ref] end,
stage_entry = function (self, data, stage_id) -- when `ref` is present, it updates info that references existing entry
local stage = self.layers[#self.layers].s
if stage_id and stage_id > #stage.e then error("FLESH.KES:stage_entry - stage_id points to undefined entry.", 2) end
if stage_id then -- explicit abscense of stage_id, will make a new entry
stage.e[stage_id].d = data
if stage.e[stage_id].r then
stage.r[stage.e[stage_id].r] = nil
stage.e[stage_id].r = nil end
else
stage_id = #stage.e+1
stage.e[stage_id] = {a={},r=nil,d=data} end -- a - aliases (holds both anonym bindings and label names), r - reserved (even nil might be the data, so we need to track this separately), d - data
return stage_id
end,
stage_alias = function (self, ref, stage_id) -- when `ref` is present, it updates info that references existing entry
local stage = self.layers[#self.layers].s
if stage_id and stage_id > #stage.e then error("FLESH.KES:stage_alias - stage_id points to undefined entry.", 2) end
if stage_id then -- explicit abscense of stage_id, will make a new entry
stage.e[stage_id].a[#stage.e[stage_id].a +1] = ref
else
stage_id = #stage.e+1
stage.r[#stage.r +1] = stage_id
stage.e[stage_id] = {a={ref},r=#stage.r,d=nil} end -- a - aliases (holds both anonym bindings and label names), r - reserved (even nil might be the data, so we need to track this separately), d - data
stage.a[ref] = stage_id -- label can point only to one place, we don't discourage user from that
return stage_id
end,
stage_staged = function (self)
return #self.layers[#self.layers].s.e
end,
stage_reserved = function (self)
return #self.layers[#self.layers].s.r > 0
end,
stage_fill_reserve = function (self, data)
if (data == FLESH.NegI.Manifests.gap) then data = nil end -- somewhat hacky, but good enough for a `gap` check. Native might not like this
local stage = self.layers[#self.layers].s
for _,i in ipairs(stage.r) do self:stage_entry(data, i) end
stage.r = {}
end,
commit = function (self) -- apply staged changes
--print("==== commit ====")
--print("\tlayer:"..tostring(#self.layers))
--print("\tdepth:"..tostring(self.layers[#self.layers].d))
--print("\tisolated:"..tostring(self.isolations["do"][self.layers[#self.layers].d] ~= nil))
--print("\tcontent:")
--pprint(self.layers[#self.layers].s.a)
local stage = self.layers[#self.layers].s
local wue = true and function (self, e)
--print("\t+ <anonymic>")
e.b[#e.b+1] = self:write_entry(nil, e.d)
end or function (self, e) end
for _, e in ipairs(stage.e) do
e.b = {}
if (#e.a > 0) then for _, name in ipairs(e.a) do
--print("\t+ "..name)
e.b[#e.b+1] = self:write_entry(name, e.d) end
else wue(self, e) end end
local output = self.layers[#self.layers].s
self.layers[#self.layers].s = {a={},e={},r={}} -- a - aliases, e - entries, r - reserve
return output -- should be a map of entry -> binding, but for now it's fine
end,
stage_clear = function (self)
self.layers[#self.layers].s = {a={},e={},r={}} -- a - aliases, e - entries, r - reserve
end,
direct_snapshot = function (self, layer_id, frame_state) -- THIS IS RAW AUTHORITY THAT VOIDS SECURITY GUARANTEES
frame_state = frame_state or {labels = {lb={},bl={}}, bindings = {}} -- when provided, pours effects directly
for _,b in ipairs(self.layers[layer_id].c.ob) do
frame_state.bindings[#frame_state.bindings+1] = {delta = self.bindings[b].records[#self.layers]}
if type(self.labels.bl[b]) == "string" then
bimap_write(frame_state.labels, "bl", #frame_state.bindings, self.labels.bl[b]) end end
return frame_state end,
inner_snapshot = function (self) -- used in Frame, in order to track writes
return self:direct_snapshot(#self.layers)
end,
view_snapshot = function (self, outer)
local frame_state = {labels = {lb={},bl={}}, bindings = {}}
for i = 1, #self.relevance.dl - (outer and 1 or 0) do -- we still will be traversing every layer, so it's recomended that user shouldn't use it
self:direct_snapshot(i,frame_state)
end
return frame_state
end,
env_snapshot = function (self, outer) -- same as view_snapshot, but provides in depth trace of context changes layer by layer
-- table with Frame manifests that inherit from each other
-- though I think I can provide an interface for KES access through special resolves per layers, instead of doing all of this
-- THIS IS AN AUTHORITY FOR DEBUGGER, THIS VIOLATES SECURITY GUARANTEES
end,
log_bindings = function (self)
for b,d in pairs(self.bindings) do
io.write((self.labels.bl[b] or "<binding "..tostring(b)..">")..": "..tostring(b).."| ")
for l,_ in pairs(d.records) do
io.write(tostring(l)..",")
end
io.write("\n")
--pprint(d.records)
end
end,
log_layers = function (self)
print("==== KES State Info ====")
print("\tlayers (or curr layer ID): "..tostring(#self.layers) )
io.write("\trelavance: depth | layer = ")
pprint(self.relevance.dl)
io.write("\tisolations: order | depth = ")
pprint(self.isolations.od)
end,
check_integrity = function (self) -- unit test for KES, I think I'll need to improve it more, so others'll know why they blown off their foot
local db = self.bindings
local ls = self.labels
print("==== KES Integrity Info START ====")
for id,layer in pairs(self.layers) do
for o,b in ipairs(layer.c.ob) do
if (not db[b]) then print(string.format("missing %s binding (aka %s) at %d depth %d", b, tostring(ls.bl[b] or "<ANON>"), id, layer.d))
elseif (not db[b].order.lo[id]) then print(string.format("missing layer link of %s binding (aka %s) at %d depth %d", b, tostring(ls.bl[b] or "<ANON>"), id, layer.d)) end
end
end
local mdc = 0
for id,bind in pairs(db) do
mdc = mdc + 1
for l,e in pairs(bind.records) do
if (not self.layers[l]) then print(string.format("dangling %s binding (aka %s) at %d depth %s", bind, tostring(ls.bl[bind] or "<ANON>"), l, "???")) end
end
end
if (#db ~= mdc) then print(string.format("binding db have gaps")) end
print("==== KES Integrity Info END ====")
end
--binding_label_get = function (self, b) return self.labels.bl[b] end, -- Used by Frame to make label list. probably pe replaced by 'pop_layer' Frame return
},
dispatch = function (self, lterm, rterm, protocol) -- needs debugging
--print("dispatch start")
if lterm == nil then error("FLESH:dispatch - got nil, expected Manifest") return end
protocol = protocol or lterm.protocol -- protocol argument is optional and here only for convinience, so I don't have to recreate manifest with transformed protocols
if protocol then
if rterm then
if protocol.can then -- both "can" and "ask" may not be fulfilled unlike "can" or "get" or abscense of protocol clauses
--print("can?")
local label_p = self.NegI.Manifests.Label -- we use direct access, because this stuff will depend on furst record anyways
if self.capcheck(label_p, rterm) then
local p = protocol.can[rterm.state.name]
if p then return {protocol = p, state = lterm.state} end end end -- if we don't have clause, we should fall down to `ask`
if protocol.ask then -- `ask` clause exist solely for cases when Manifest need to handle arbitrary labels, like vector axis swizzling, field "modification" (the field might be abscent) and etc
--print("ask?")
local artifact_p = self.NegI.Manifests.Artifact -- currently it's tightly coupled, I'll need to slightly rework this
local label_p = self.NegI.Manifests.Label
local clause = protocol.ask
if self.capcheck(label_p, rterm) then if self.capcheck(artifact_p, clause) then return clause.state.artifact(lterm, rterm)
else return self:dispatch(clause, self.make.Frame({self = lterm, arg = rterm})) end end end
if protocol.call then
--print("call?")
if rterm.protocol and rterm.protocol.get then rterm = self:dispatch(rterm, nil) end -- passive evaluation, because caller expect contents
local artifact_p = self.NegI.Manifests.Artifact
local frame_p = self.NegI.Manifests.Frame
local clause = protocol.call
return self.capcheck(artifact_p, clause) and
(clause.state.artifact(lterm, rterm) or self.NegI.Manifests.gap) or
self:dispatch(clause, self.make.Frame({self = lterm, arg = rterm})) -- needs some standartization on how this should be passed around, don't like hardcoded "self" and "arg"
elseif protocol.pass then -- when sole protocol existance is to pass negotiation along with some caveats
--print("pass?")
local artifact_p = self.NegI.Manifests.Artifact -- currently it's tightly coupled, I'll need to slightly rework this
local clause = protocol.pass
return self.capcheck(artifact_p, clause) and
(clause.state.artifact(lterm, rterm) or self.NegI.Manifests.gap) or
self:dispatch(clause, self.make.Frame({self = lterm, arg = rterm}))
elseif protocol.get then -- fallback to underlying manifest for an answer
--print("get.")
local artifact_p = self.NegI.Manifests.Artifact
local frame_p = self.NegI.Manifests.Frame
local clause = protocol.get
--if not clause.state then pprint(clause) end
local fabk = self.capcheck(artifact_p, clause) and
(clause.state.artifact(lterm) or self.NegI.Manifests.gap) or self:dispatch(clause, lterm)
return self:dispatch(fabk, rterm)
else return self.make.Error("OPHANIM: FLESH:dispatch Error: rterm is outside of lterm protocol capability") end
elseif protocol.get then
--print("get explicit.")
local artifact_p = self.NegI.Manifests.Artifact
local clause = protocol.get
if self.capcheck(artifact_p, clause) then
return clause.state.artifact(lterm) or self.NegI.Manifests.gap
else return self:dispatch(clause, lterm) end
else return lterm end
elseif rterm then return self.make.Error("OPHANIM: FLESH:dispatch Error: missing protocol")
else return lterm end
end,
--env methods
reset = function (self) end, -- inits defaults and other stuff
}
-- Artifact assumptions:
-- on call it recieves 2 manifests (tabels, not KES IDs): self, arg
-- on get it's just self (tables, not KES IDs)
-- the return should return Manifest (tables, not KES IDs)
--I need to make things clear:
--Inside Manifest state is opaque host resource.
FLESH.make = {} -- Manifest constructors
FLESH.make.thunks = {}
FLESH.make.unthunk = function ()
for _,e in ipairs(FLESH.make.thunks) do e() end
FLESH.make.thunks = {} end
FLESH.make.Manifest = function (protocol, state) -- protocols are still eager, but it's possible to do lazy clause access via "get" manifests (like quoted labels). here we just doing it via metatable because it's convinient, and I'm making a PoC, not final product yet
local m = {protocol = protocol, state = state}
if type(protocol) == "table" then return m
elseif type(protocol) == "function" then
local ok, result = pcall(protocol) -- lazy dispatch
if ok then -- convinence
return {protocol = result, state = state}
else
local dummy = {}
local undummy = function ()
setmetatable(dummy,nil)
for k,v in pairs(m) do dummy[k] = v end
dummy.protocol = m.protocol()
end
FLESH.make.thunks[#FLESH.make.thunks+1] = undummy
setmetatable(dummy, {
__index = function (t,k) -- lazy fetch, becuase some Manifests rely on that
undummy()
return t[k] end,
__newindex = function (t,k,v) undummy(); t[k] = v end,
__len = function () return #m end })
return dummy end
else error("protocol expected as function or table, got "..type(protocol), 2) end
end
FLESH.NegI = {Manifests = {}} -- the NegI assets
FLESH.NegI.Manifests.Artifact = { -- Artifact for handling external authority
protocol = {
can = {
["in"] = {call = "stub"},
["="] = {call = "stub"}}},
state = {
can = {
reload = {get = "stub"}},
introspect = {get = "stub"}, -- can't decide on a name yet, should return ingridients for cooking the authority in question
call = "stub"}}
local dcopy = function (t)
-- I need to add function for explicit import/deep copy, because currently it's not sandboxed properly
return t
end
local artifact_env = { -- it's an environment centered around debug tools and devlopment environment, so having all that stuff here is fine
-- Lua Language Internals
basic = basic,
assert = assert, error = error, warn = warn,
collectgarbage = collectgarbage,
dofile = dofile, load = load, loadfile = loadfile,
getmetatable = getmetatable, setmetatable = setmetatable,
ipairs = ipairs, pairs = pairs,
next = next, select = select, unpack = unpack or table.unpack,
pcall = pcall, xpcall = xpcall,
print = print,
rawequal = rawequal, rawget = rawget, rawlen = rawlen, rawset = rawset,
require = require,
tonumber = tonumber, tostring = tostring,
type = type,
-- it's unlikely that those get modified. in some cases they might be abscent or not full
table = dcopy(table),
math = dcopy(math),
string = dcopy(string),
utf8 = dcopy(utf8),
debug = dcopy(debug),
coroutine = dcopy(coroutine),
io = dcopy(io),
os = dcopy(os),
package = dcopy(package),
-- The OPHANIM Substrate
-- Artifacts MUST use this to interact with the system.
FLESH = FLESH,
-- temporarely here for debug purposes
pprint = pprint,
}
FLESH.make.Artifact = function (chunk, chunkname, mode)
chunkname = chunkname or "chunk"
local a, e = load(chunk, "OPHANIM:"..chunkname, mode, artifact_env)
if (e) then
local error_p = FLESH.NegI.Manifests.Error
if error_p then return FLESH.make.Manifest(
error_p.state, {desc = "Artifact: Failed to load "..chunkname.." due to host error: "..e})
else print("in ```lua\n"..chunk.."\n```"); error("FLESH.make.Artifact - Artifact construction failed on NegI sys init due to host error:"..tostring(e), 2) end
end
ok, result = pcall(a)
local callable_result = (type(a) == "function") or (getmetatable(a).__call)
if (not callable_result) then
if (not callable_result and ok) then result = "provided code is not callable!" end
local error_p = FLESH.NegI.Manifests.Error
if error_p then return FLESH.make.Manifest(
error_p.state, {desc = "Artifact: Failed to load "..chunkname.." due to host error: "..result})
else print("in ```lua\n"..chunk.."\n```"); error("FLESH.make.Artifact - Artifact construction failed on NegI sys init due to host error:"..tostring(e), 2) end
end
return FLESH.make.Manifest(FLESH.NegI.Manifests.Artifact.state, {
chunk = chunk,
chunkname = chunkname,
mode = mode,
--env = env,
artifact = result})
end
FLESH.intentcheck = function(self, arg) -- State intent of manifests are matching?
if self.state == arg.protocol then return true
elseif self.state and arg.protocol then
for i,e in pairs(self.state) do
if (arg.protocol[i] ~= e) then
return false end end
if self.state.can == arg.protocol.can then return true
elseif self.state.can and arg.protocol.can then
for i,e in pairs(self.state.can) do
if (arg.protocol.can[i] ~= e) then
return false end end
end end
return true end
FLESH.capcheck = function(self, arg) -- Even through fallbacks, is manifest implements this protocol?
-- TODO: check protocol in flat form, we need to make sure clauses are reachable, not that there is inside chain some Manifest that satisfy intent.
-- TODO: rework `pass` clause into argumentless `proxy_start` and `proxy_end`, argument makes it unpredictable.
if self.state == arg.protocol then return true
elseif self.state and arg.protocol then
local protobuff = {}
local fail = function () return arg.protocol.get and FLESH.capcheck(self, FLESH:dispatch(arg, nil)) or false end
for i,e in pairs(self.state) do
if (arg.protocol[i] ~= e) then
return fail() end end
if self.state.can == arg.protocol.can then return true
elseif self.state.can and arg.protocol.can then
for i,e in pairs(self.state.can) do
if (arg.protocol.can[i] ~= e) then
return fail() end end
end end
return true end
--common between protocol manifests
local capability_check = FLESH.make.Artifact([[return function (self, arg)
return FLESH.make.Number(FLESH.capcheck(self, arg)) end]], "`in` aka capcheck")
FLESH.NegI.Manifests.Artifact.protocol.can["in"] = capability_check
FLESH.NegI.Manifests.Artifact.protocol.can["="] = FLESH.make.Artifact([[return function (self, arg) end]], "Artifact can =")
FLESH.NegI.Manifests.Artifact.state.can.reload = FLESH.make.Artifact([[return function (self)
return FLESH.make.Artifact(self.state.chunk, self.state.chunkname, self.state.mode, self.state.env) end]], "Artifact can reload")
FLESH.NegI.Manifests.Artifact.state.can.introspect = FLESH.make.Artifact([[return function (self, arg) end]], "Artifact can intospect")
FLESH.NegI.Manifests.Artifact.state.call = FLESH.make.Artifact([[return function(self, arg)
local tunp = unpack or table.unpack
return self.state.artifact(tunp(arg)) end]], "Artifact call")
FLESH.make.Frame = function (t)
-- WARNING: the nested table is interface, but the content of it must be Manifests
local s = {labels = {lb={},bl={}}, bindings = {}}
--s.labels : BiMap<label: string, binding: number>
--s.bindings : Array<bind: number, {parent: table, delta: number}|{delta: any}> -- for PoC it's enough, but I should later optimize it for memory, because inheriting lots of data would create lots of redundancy
for k,v in pairs(t) do
s.bindings[#s.bindings+1] = {delta = v} -- we adding data not inheriting data, so there's no parent
if type(k) == "string" then -- I don't have plans on introducing numeric keys, because bindings already have these
bimap_write(s.labels, "lb", k, #s.bindings) end end
return FLESH.make.Manifest(FLESH.NegI.Manifests.Frame.state, s)
end
FLESH.make.Error = function (desc) return FLESH.make.Manifest(FLESH.NegI.Manifests.Error, {desc = desc}) end -- this one should hold more info than currently it is. Prefereably it should be able to store an Error chain, this will be a common occurence in NegI.
FLESH.make.Number = function (val)
return (({
number = function (num) return FLESH.make.Manifest(FLESH.NegI.Manifests.Number.state, num) end,
boolean = function (bool) return bool and FLESH.NegI.Manifests["true"] or FLESH.NegI.Manifests["false"] end
})[type(val)] or (function (val) return FLESH.make.Error("invalid type (rework this error)") end))(val)
end
FLESH.make.String = function (val)
return (type(val) == "string") and
FLESH.make.Manifest(FLESH.NegI.Manifests.String.state, val) or
FLESH.make.Error("invalid type (rework this error)")
end
-- no implicit conversions, this is only between this specific implementation
local make_trans_op = function (op)
return FLESH.make.Artifact([[return function (self, arg)
if (FLESH.capcheck({state = self.protocol},arg)) then -- 2nd value might have different protocol, I think I'll need to rework this into lua general value protocol check or something.
return FLESH:import(self.state ]]..op..[[ arg.state)
else
return -- Error manifest
end
end]], op.." operator call")
end
local make_trans = function (trans)
return FLESH.make.Artifact([[return function (self)
return FLESH:import(]]..trans..[[(self.state))
end]], trans.." uniarg call")
end
local make_host_res_init = function (host_type)
return FLESH.make.Artifact([[return function (self, arg)
-- wrap host resource manifest
if (type(arg) == "]]..host_type..[[") then return {protocol = self.state,state = arg} end
if (FLESH.capcheck(self,arg)) then return arg end -- literal uses same protocol, so we just passing
return -- Error manifest
end]])
end
FLESH.NegI.Manifests = { -- this is the core shared interface (or "corelib" if you'd like to call it like that), the abstract foundation for any logic that will come next. TODO: I need to move NegI protocols inside of it
Native = FLESH.make.Manifest({ -- should represent state during introspection, to make it hostile
["in"] = {call = capability_check}},{
can = { -- lua allows importing, but compiled/static languages have a possibility of not working out like that
import = {get = FLESH.make.Artifact("return function (self) return FLESH:import(self.state) end", "Native can import")}}
}),
Artifact = FLESH.NegI.Manifests.Artifact, -- Host authority descriptor, seek definition before this
Error = FLESH.make.Manifest({ -- Error is always as valua
can = {
["in"] = {call = capability_check},
["="] = {call = FLESH.make.Artifact([[return function (self, arg) end]])}
}
},{
can = {
name = {get = FLESH.make.Artifact([[return function (self) end]])},
desc = {get = FLESH.make.Artifact([[return function (self)
return FLESH.make.String(tostring(self.state.desc))
end]])},
caller = {get = FLESH.make.Artifact([[return function (self) end]])},
trace = {get = FLESH.make.Artifact([[return function (self) end]])},
},
}),
Token = FLESH.make.Manifest({["in"] = {call = capability_check}},{ -- adds metainfo that's used by `Error`s, so you can read the exact place of where your code failed
can = {
token = {can = {
root = {get = FLESH.make.Artifact([[return function (self) end]])}, -- references root Token from where it is
parent = {get = FLESH.make.Artifact([[return function (self) end]])}, -- return parent Token (probably won't add this, because I don't store that)
element = {get = FLESH.make.Artifact([[return function (self) end]])}, -- text representation of Token
id = {get = nil}, -- it's id (probably will remove it)
position = {get = nil}, -- position relative to root Token text representation
content = {get = nil}, -- return Frame with it's child Tokens (probably won't add this, because I store that in opaque non-uniform states)
}}
},
get = FLESH.make.Artifact([[return function (self) return self.state.token end]]), -- fallback to standard token operation
}),
Manifest = FLESH.make.Manifest({ -- Protocol for directly constructing Manifests
can = {
["in"] = {call = capability_check}, -- capability_check is shared artifact
["="] = {call = FLESH.make.Artifact([[return function (self, arg) end]])},
},
},{}),-- thats "any" type, there's nothing to check, because everything is a Manifest
ManifestMeta = FLESH.make.Manifest({ -- Protocol for Manifest Inspection/Reflection
can = {
["in"] = {call = capability_check}, -- capability_check is shared artifact
["="] = {call = FLESH.make.Artifact([[return function (self, arg) return FLESH.make.Manifest(self.state, arg) end]])}, -- we don't need checks, everything is a manifest
},
},{
can = {
["=="] = {call = FLESH.make.Artifact([[return function (self, arg)
return FLESH.make.Number(rawequal(self.state.protocol, arg.protocol) == true) and -- we check protocol and state separately, because table that holds these, don't provide the identity
FLESH.make.Number(rawequal(self.state.state, arg.state) == true) -- self.state have arg from `=` with both protocol and state
end]], "ManifestMeta can == call")},
protocol = {get = FLESH.make.Artifact([[return function (self) return FLESH.make.Manifest( FLESH.NegI.Manifests.Protocol.state , self.state.protocol ) end]], "ManifestMeta can protocol get")},
state = {get = FLESH.make.Artifact([[return function (self) return FLESH.make.Manifest(FLESH.NegI.Manifests.Native.state, self.state.state) end]], "ManifestMeta can state get")}, -- it is opaque, so Native it is
back = {get = FLESH.make.Artifact([[return function (self) return self.state end]], "ManifestMeta can back get")}, -- not sure about it
}
}),-- that's more like you pulled up an inspection tool, and this tool gives you this
Protocol = FLESH.make.Manifest({ -- Protocol for new Protocols
can = {
of = {call = FLESH.make.Artifact([[return function (self, arg) end]])}, -- will return the protocol of some Manifest that could be used to check other Manifests
["in"] = {call = capability_check}, -- capability_check is shared artifact
["="] = {call = FLESH.make.Artifact([[return function (self, arg) end]])}
},
call = FLESH.make.Artifact([[return function (self, arg) end]]), -- artifact for creating new protocol manifests
},{
["in"] = capability_check
}),
["//"] = FLESH.make.Manifest({
pass = FLESH.make.Artifact("return function (self, arg) return { protocol = { pass = FLESH.make.Artifact(\"return function (self, arg) return arg end\")}} end")},{}),
pass = FLESH.make.Manifest({ -- TODO: explicitly ends Sequence with appropriate data. monad where first is to where and 2nd is data
call = FLESH.make.Artifact([[return function (self, arg)
end]])
},{}),
["false"] = FLESH.make.Manifest(function () return FLESH.NegI.Manifests.Number.state end,0), -- sugar
["true"] = FLESH.make.Manifest(function () return FLESH.NegI.Manifests.Number.state end,1), -- sugar
gap = FLESH.make.Manifest({},{}), -- it's fine, that's how it should be
Number = FLESH.make.Manifest({
can = {
["in"] = {call = capability_check},
["="] = {call = FLESH.make.Artifact([[return function (self, arg) end]])}
}},{
can = {
["+"] = {call = FLESH.make.Artifact([[return function (self, arg)
local num_p = FLESH.NegI.Manifests.Number
if (FLESH.capcheck(num_p, arg) and arg) then
return FLESH.make.Number(self.state + arg.state) end
return FLESH.make.Error("Number can + call: expected number, got something else")
end]], "Number can + call")},
["-"] = {call = FLESH.make.Artifact([[return function (self, arg)
local num_p = FLESH.NegI.Manifests.Number
if (FLESH.capcheck(num_p, arg) and arg) then
return FLESH.make.Number(self.state - arg.state) end
return FLESH.make.Error("Number can + call: expected number, got something else")
end]], "Number can - call")},
["*"] = {call = FLESH.make.Artifact([[return function (self, arg)
local num_p = FLESH.NegI.Manifests.Number
if (FLESH.capcheck(num_p, arg) and arg) then
return FLESH.make.Number(self.state * arg.state) end
return FLESH.make.Error("Number can + call: expected number, got something else")
end]], "Number can * call")},
["/"] = {call = FLESH.make.Artifact([[return function (self, arg)
local num_p = FLESH.NegI.Manifests.Number
if (FLESH.capcheck(num_p, arg) and arg) then
return FLESH.make.Number(self.state / arg.state) end
return FLESH.make.Error("Number can + call: expected number, got something else")
end]], "Number can / call")},
["<>"] = {call = FLESH.make.Artifact([[return function (self, arg)
local num_p = FLESH.NegI.Manifests.Number
if (FLESH.capcheck(num_p, arg) and arg) then
return FLESH.make.Number(
self.state == arg.state and 0 or
self.state < arg.state and 1 or -1) end
return FLESH.make.Error("Number can + call: expected number, got something else")
end]], "Number can <> call")},
},
}), -- need to make generic host agnostic number representation (maybe even Rational out of 2 BigIntegers or just BigInteger to not conflate these 2 for the compilation process)
String = FLESH.make.Manifest({
can = {
["in"] = {call = capability_check},
["="] = {call = FLESH.make.Artifact([[return function (self, arg) end]])}
}},{
can = {
length = {get = FLESH.make.Artifact("return function (self, arg) return FLESH.make.Number(#self.state) end")}
},
call = FLESH.make.Artifact([[return function (self, arg)
local num_p = FLESH.NegI.Manifests.Number
local frame_p = FLESH.NegI.Manifests.Frame
if (FLESH.capcheck(num_p, arg)) then
return FLESH.make.String(self.state:sub(arg.state,arg.state))
elseif (FLESH.capcheck(frame_p, arg)) then -- slicing in python style
local s_start = FLESH.NegI.Intrinsics.frame_index(arg.state, "start") or FLESH.NegI.Intrinsics.frame_index(arg.state, 1)
local s_end = FLESH.NegI.Intrinsics.frame_index(arg.state, "end") or FLESH.NegI.Intrinsics.frame_index(arg.state, 2)
if (s_start) then if (not FLESH.capcheck(num_p, s_start)) then
return FLESH.make.Error("String call: expected number in frame at `start` or 1, got something else")
end else
return FLESH.make.Error("String call: expected number in frame at `start` or 1, got something else")
end
if (s_end) then if (not FLESH.capcheck(num_p, s_end)) then
return FLESH.make.Error("String call: expected number or gap in frame at `end` or 2, got something else")
end end
return FLESH.make.String(self.state:sub(s_start,s_end or s_start))
end
return FLESH.make.Error("String call: expected frame or number, got something else")
end]], "String call")
}), -- some hosts might have to emulate this
Label = FLESH.make.Manifest({ -- it's job is to represent a get query from KES to load manifests
can = {
["in"] = {call = capability_check},
["="] = {call = FLESH.make.Artifact([[return function (self, arg) end]])}
}},{
can = {
[":"] = {get = FLESH.make.Artifact([[return function (self)
FLESH.KES:stage_alias(self.state.name)
return {
protocol = { pass = FLESH.make.Artifact("return function (self, arg) return arg end")},
state = self.state.name }end]])},
["name"] = {get = FLESH.make.Artifact([[return function (self)
return FLESH.make.String(self.state.name)
end]])},
},
get = FLESH.make.Artifact([[return function (self)
local m, _ = FLESH.KES:resolve(self.state.name)
return m or FLESH.NegI.Manifests.gap
end]])
}),
Frame = FLESH.make.Manifest({
can = {
["in"] = {call = capability_check},
["="] = {call = FLESH.make.Artifact([[return function (self, arg) end]])}
}},{
can = {
--["+"] = {call = FLESH.make.Artifact([[return function (self, arg) end]])},
--["*"] = {call = FLESH.make.Artifact([[return function (self, arg) end]])},
--delta = {get = FLESH.make.Artifact([[return function (self) end]])},
load = {get = FLESH.make.Artifact([[return function (self)
local labels, bindings = self.state.labels, self.state.bindings
for b,e in pairs(bindings) do
local sid = FLESH.KES:stage_entry(e.parent and e.parent[e.delta] or e.delta)
--pprint(self.state)
--print("load: "..(labels.bl[b] or "<anonymic "..b..">"))
if (labels.bl[b]) then FLESH.KES:stage_alias(labels.bl[b], sid) end -- sometimes, user will want to load Frame inside a Frame.
end
return FLESH.NegI.Manifests.gap
end]], "Frame can load get")},
["."] = {ask = FLESH.make.Artifact([[return function (self) --TODO
--self.state.labels
end]])},
map = {pass = FLESH.make.Artifact([[return function (self, arg) end]])},
concetrate = {pass = FLESH.make.Artifact([[return function (self, arg) end]])},
},
call = FLESH.make.Artifact([[return function (self, arg)
local num_p = FLESH.NegI.Manifests.Number
local str_p = FLESH.NegI.Manifests.String
if (FLESH.capcheck(num_p, arg) or FLESH.capcheck(str_p, arg)) then
return FLESH.NegI.Intrinsincs.frame_index(self.state, arg.state) or FLESH.NegI.Manifests.gap
elseif (FLESH.capcheck({state = self.protocol}, arg) and arg) then -- slicing in python style
else
end
return FLESH.make.Error("Frame call: expected string or number, got something else")
end]])
}),
Sequence = FLESH.make.Manifest({
can = {
["in"] = {call = capability_check},
["="] = {call = FLESH.make.Artifact([[return function (self, arg) end]])}
}},{
can = {
prods = {get = FLESH.make.Artifact([[return function (self) end]])}, -- in order to get raw data, @ must be used
creturn = {get = FLESH.make.Artifact([[return function (self) end]])}, -- in order to get raw data, @ must be used
introspect = {get = FLESH.make.Artifact([[return function (self) end]])}
},
call = FLESH.make.Artifact([[return function (self, arg)
local prods = self.state.prods
local frame_p = FLESH.NegI.Manifests.Frame
if (FLESH.capcheck(frame_p, arg)) then FLESH:dispatch(arg,nil,arg.protocol.can.load) end
for i,e in ipairs(prods) do
e = e or FLESH.NegI.Manifests.gap
e = FLESH:dispatch(e); e = e or FLESH.NegI.Manifests.gap -- evaluation
e = FLESH:dispatch(e) -- get
FLESH.KES:stage_fill_reserve(e)
FLESH.KES:commit() end
return self.state.creturn and FLESH:dispatch(self.state.creturn) or FLESH.NegI.Manifests.gap
end]], "Sequence call")
}),
Membrane = FLESH.make.Manifest({ -- represent the layers and how they affect environment
can = {
["in"] = {call = capability_check},
["="] = {call = FLESH.make.Artifact([[return function (self, arg) end]])}
}
},{
get = FLESH.make.Artifact([[return function (self) -- I'm not sure if that's the right way, but anyways, if it's working - it works
local d = (self.state.parent > FLESH.KES:get_depth()) and FLESH.KES:get_depth() or self.state.parent
return FLESH.make.Manifest(FLESH.NegI.Manifests.Membrane.state, {
parent = d,
contain = self.state.contain,
quoted = self.state.quoted,
content = self.state.content})
end]]),
pass = FLESH.make.Artifact([[return function (self, arg)
local d = (self.state.parent > FLESH.KES:get_depth()) and FLESH.KES:get_depth() or self.state.parent
d = self.state.quoted and FLESH.KES:unquote_parent(d) or d
FLESH.KES:push_layer(d, self.state.contain)
local output = FLESH:dispatch(self.state.content or FLESH.NegI.Manifests.gap, arg or FLESH.NegI.Manifests.gap)
FLESH.KES:pop_layer()
return output
end]], "Membrane pass")
}), -- I think I should make distinction between Membranes, though parent Manifest with inherited capabilities will be here
Make = FLESH.make.Manifest({ -- aka [] or grounded (because push_layer will be grounded by default)
can = {
["in"] = {call = capability_check},
["="] = {call = FLESH.make.Artifact([[return function (self, arg) end]])}
}
},{
get = FLESH.make.Artifact([[return function (self)
if self.state then
local d = FLESH.KES:get_depth()
FLESH.KES:push_layer(d)
local e = FLESH:dispatch(self.state)
FLESH.KES:pop_layer()
return FLESH.make.Manifest(FLESH.NegI.Manifests.Membrane.state, {
parent = d,
contain = false,
quoted = false,
content = e})
else return FLESH.NegI.Manifests.gap end
end]], "Make get")
}),
Quote = FLESH.make.Manifest({ -- aka {} or dynamic (because it will shift parent within isolation)
can = {
["in"] = {call = capability_check},
["="] = {call = FLESH.make.Artifact([[return function (self, arg) end]])}
}
},{
get = FLESH.make.Artifact([[return function (self)
return FLESH.make.Manifest(FLESH.NegI.Manifests.Membrane.state, {
parent = FLESH.KES:get_depth(),
contain = false,
quoted = true,
content = self.state})
end]], "Quote get")
}),
Contain = FLESH.make.Manifest({ -- aka () or isolated
can = {
["in"] = {call = capability_check},
["="] = {call = FLESH.make.Artifact([[return function (self, arg) end]])}
}
},{
get = FLESH.make.Artifact([[return function (self)
if self.state then
local d = FLESH.KES:get_depth()
FLESH.KES:push_layer(d, true)
local e = FLESH:dispatch(self.state)
FLESH.KES:pop_layer()
return FLESH.make.Manifest(FLESH.NegI.Manifests.Membrane.state, {
parent = d,
contain = true,
quoted = false,
content = e})
else return FLESH.NegI.Manifests.gap end
end]], "Contain get")
}),
Negotiation = FLESH.make.Manifest({ -- aka jusxtaposition
can = {
["in"] = {call = capability_check},
["="] = {call = FLESH.make.Artifact([[return function (self, arg) end]])}
}},{
get = FLESH.make.Artifact([[return function (self)
-- resolve terms: we hold references in state, not data
local lt = self.state.lterm
local rt = self.state.rterm
return FLESH:dispatch(lt, rt)
end]], "Negotiation get")
}),
Function = FLESH.make.Manifest({},{}),
Structure = FLESH.make.Manifest({},{}),
context = FLESH.make.Manifest({
can = {
inner = {get = FLESH.make.Artifact([[return function (self) return FLESH.make.Manifest(FLESH.NegI.Manifests.Frame.state, FLESH.KES:inner_snapshot()) end]])},
outer = {get = FLESH.make.Artifact([[return function (self) return FLESH.make.Manifest(FLESH.NegI.Manifests.Frame.state, FLESH.KES:view_snapshot(true)) end]])},
full = {get = FLESH.make.Artifact([[return function (self) return FLESH.make.Manifest(FLESH.NegI.Manifests.Frame.state, FLESH.KES:view_snapshot(false)) end]])},
}
},{}),
Parser = FLESH.make.Manifest({
["in"] = {call = capability_check},
["="] = {call = FLESH.make.Artifact([[return function (self, arg) end]])}
},{
can = {
nodes = {get = FLESH.make.Artifact([[return function (self) end]])} -- get Frame with responsible manifests
},
call = FLESH.make.Artifact([[return function (self, arg) end]]) -- evaluate
})
-- I need to understand how should I make it, so the user can modify the axioms it uses
}
FLESH.make.unthunk()
FLESH.Host = {}
FLESH.Host.Types = { -- while it's a mapping table, OPHANIM fundamentally disagree with lua on type existance, so for example userdata can't be capchecked
["nil"] = FLESH.NegI.Manifests.gap,
boolean = FLESH.make.Manifest({
can = {
["in"] = {call = capability_check},
["="] = make_host_res_init("boolean")
},{
can = {
["|"] = {call = make_trans_op("or")},
["&"] = {call = make_trans_op("and")},
["~"] = {get = make_trans("not")},
["=="] = {call = make_trans_op("==")},
["~="] = {call = make_trans_op("~=")},
to = {
can = {
NegIManifest = {get = FLESH.make.Artifact("return function (self) return FLESH.make.Number(self.state) end")},
}}}
}}),
number = FLESH.make.Manifest({
can = {
["in"] = {call = capability_check},
["="] = make_host_res_init("number")
},{
can = {
["+"] = {call = make_trans_op("+")},
["-"] = {call = make_trans_op("-")},
["*"] = {call = make_trans_op("*")},