-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence_processor.clj
1373 lines (1134 loc) · 32.8 KB
/
sequence_processor.clj
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
;;
;; just experiments
;; Left it messy.
;;
(ns sequence-processor
(:require
[bennischwerdtner.hd.binary-sparse-segmented :as hd]
[tech.v3.datatype :as dtype]
[tech.v3.tensor :as dtt]
[tech.v3.parallel.for :as pfor]
[tech.v3.datatype.argops :as dtype-argops]
[tech.v3.datatype.functional :as f]
[clojure.math.combinatorics :as combo]))
;; quick auto associative memory
(defprotocol AutoAssociativeMemory
(lookup [this query-v]
[this query-v threshold])
(lookup* [this query-v]
[this query-v threshold])
(store [this v])
(mem [this]))
(defn auto-associative-lookup
([m query-v] (auto-associative-lookup m query-v 0.09))
([m query-v threshold]
(let [similarities
(into [] (pmap #(hd/similarity % query-v) m))]
(when (seq similarities)
(let [argmax (dtype-argops/argmax similarities)]
(when (<= threshold (similarities argmax)) (m argmax)))))))
(defn auto-associative-lookup*
([m query-v] (auto-associative-lookup* m query-v 0.09))
([m query-v threshold]
(let [similarities
(into [] (pmap #(hd/similarity % query-v) m))]
(map m
(map first
(filter (comp #(< threshold %) second)
(map-indexed vector similarities)))))))
(defn auto-associative-store [m v]
(assert (hd/hv? v))
(conj m v))
;; there is literature on how to make this smarter,
;; in particular in a `sparse distributed memory`, you don't grow the memory with every new item
;;
(defn ->auto-a-memory
[]
(let [m (atom [])]
(reify
AutoAssociativeMemory
(lookup [this query-v]
(auto-associative-lookup @m query-v))
(lookup [this query-v threshold]
(auto-associative-lookup @m query-v threshold))
(lookup* [this query-v]
(auto-associative-lookup* @m query-v))
(lookup* [this query-v threshold]
(auto-associative-lookup* @m query-v threshold))
(store [this v] (swap! m auto-associative-store v) this)
(mem [this] @m))))
(def auto-a-memory (->auto-a-memory))
(defn known
"Cleanup x with the autoassociative memory."
([x] (known x 0.09))
([x threshod]
(lookup auto-a-memory x threshod)))
(defn remember-soft
([x] (remember-soft x 0.9))
([x threshod]
(when-not (known x threshod) (store auto-a-memory x))
x))
(defn remember [x] (store auto-a-memory x) x)
;; Make a quick book keeping implementation:
(def hyper-symbols-symbols
["🐂" "🐛" "🚌" "Ψ" "Ϟ" "🪓" "🌈"])
(let [lut (atom {})]
;; "encountering a symbol" since symbol and value are
;; interchangeable in hdc (Kanerva 2009), why not
;; simply call it `prototype`
;;
(defn ->prototype
"This also stores the symbol in content addressable memeory.
[[known]] will return the cleaned up symbol.
"
[sym]
(or (@lut sym)
(let [v (hd/->hv)
_ (swap! lut assoc sym v)]
;; !
;; (always a new vec, we just created it)
(remember v)
v)))
(defn cleanup-lookup-verbose
([query-v] (cleanup-lookup-verbose query-v 0.09))
([query-v threshold]
(->> (map (fn [[k v]]
{:k k
:similarity (hd/similarity v query-v)
:v v})
@lut)
(filter (comp #(<= threshold %) :similarity))
(sort-by :similarity (fn [a b] (compare b a))))))
(defn cleanup-lookup-value
[query-v]
(some->> (cleanup-lookup-verbose query-v)
first
:k))
(defn cleanup-mem [] @lut))
(defn cleanup*
([query-v] (cleanup* query-v 0.09))
([query-v threshold]
(map :k (cleanup-lookup-verbose query-v threshold))))
(defn mix1 [a b]
(hd/thin (hd/bundle (->prototype a) (->prototype b))))
(defn ->record
[kvps]
(hd/thin (apply hd/bundle
(for [[k v] kvps] (hd/bind k v)))))
(comment
(let [a (hd/->hv)
b (hd/->hv)
ab (hd/thin (hd/bundle a b))
auto-a-memory [a b ab]]
(= a (auto-associative-lookup auto-a-memory a))))
(comment
(known (remember (->prototype :a)))
(known (hd/->hv)))
(defn sequence-marker-1 [k] (hd/->hv))
(def sequence-marker (memoize sequence-marker-1))
(defn ->sequence
[& xs]
;; doesn't allow making lists of noisy sutff ig. Was
;; just an attempt... But in principle showcases that
;; you can represent sequences with random markers as
;; bind (that is equivalent to a random projection
;; for each position in this implementation)
;;
(run! (fn [x] (when-not (known x 0.9) (remember x))) xs)
(hd/thin
(apply hd/bundle
(map-indexed (fn [i x]
(hd/bind x (sequence-marker i)))
xs))))
;; seq is basically a set where the keys correspond to indices
;; retrieving is the same as with a record
(defn h-nth [hsx idx]
(hd/unbind hsx (sequence-marker idx)))
(defn h-seq? [exp]
;; can also be `:nothing`
;; doesn't count as seq here atm
(and
(hd/hv? exp)
(known (h-nth exp 0))))
(defn clj->vsa
[obj]
(cond (map? obj) (->record (map (fn [[k v]] [(clj->vsa k)
(clj->vsa
v)])
obj))
(or
(list? obj)
(vector? obj))
;; you need to make a decision about
;; how to deal with the empty sequence
(if (empty? obj)
(->prototype :nothing)
(apply ->sequence (map clj->vsa obj)))
(hd/hv? obj) obj
;; there would be alternative ways to do this
;; (symbol? obj) (clj->vsa {:symbol
;; (->prototype obj)})
;; (symbol? obj) (->prototype obj)
:else (->prototype obj)))
(defn unroll
[hxs]
(take-while
identity
(map known (map #(h-nth hxs %) (range)))))
(defn unroll-tree
[hsx]
(map (fn [x]
(if (h-seq? x)
(unroll-tree x)
x))
(unroll hsx)))
(defn walk-cleanup
[form]
(letfn
[(f [e]
(if (hd/hv? e) (cleanup-lookup-value e) (map f e)))]
(map f form)))
;;
;; A - Ambiguity primitives
;;
;;
;;
(defn mix
([a] a)
([a b & args] (hd/thin (apply f/+ a b args)))
([a b] (hd/thin (hd/bundle a b))))
(def possibly mix)
(comment
(hd/similarity (mix (->prototype :a)
(->prototype :b)
(->prototype :c)
(->prototype :d))
(->prototype :d))
0.29)
(def neither (fn [a b] (hd/bind a b)))
(def roughly
(fn [a amount-of-a] (hd/weaken a (- 1 amount-of-a))))
(defn mostly
([a b] (mostly a b 0.3))
([a b amount-of-b]
(hd/thin (hd/bundle a (roughly b amount-of-b)))))
(defn never [e b]
(hd/thin (f/- e b)))
(def impossibly never)
(comment
(hd/similarity (impossibly (mix (->prototype :a)
(->prototype :b)
(->prototype :c)
(->prototype :d))
(->prototype :d))
(->prototype :d))
0.0
(let [coin (mix (->prototype :heads) (->prototype :tails))]
[(cleanup* coin)
(cleanup* (never coin (->prototype :tails)))]
;; [(:heads :tails) (:heads)]
(let [coin
(mostly
(->prototype :heads)
(->prototype :tails) 0.05)]
[(cleanup* coin)]))
[(:heads)]
;; now if you use a higher threshold for cleanup:
;; => it would be quite interesting to modify the threshold dynamically
;;
(let [coin
(mostly
(->prototype :heads)
(->prototype :tails) 0.3)]
[(hd/similarity coin (->prototype :heads))
(hd/similarity coin (->prototype :tails))
[(cleanup* coin)
(cleanup* coin 0.2)
(cleanup* (impossibly coin (->prototype :tails)))]])
;; [0.87 0.14 [(:heads :tails) (:heads) (:heads)]]
)
;; a.k.a. a and b's N-space circles (with width threshold) overlap
;;
;; ... or there is a point c in the memory between the 2?
;;
;; (defn necessarily [a b threshold])
(defn non-sense [] (hd/->hv))
;; I think there is something deep about the concept that
;; non-sense and gensym are the same operation
(def create non-sense)
(defn make-hyper [op] (with-meta op {:hyper-fn true}))
(defn mark-hyper [v]
(alter-meta! v assoc :hyper-fn true)
(alter-var-root v make-hyper))
(do
(mark-hyper #'mix)
(mark-hyper #'possibly)
(mark-hyper #'neither)
(mark-hyper #'roughly)
(mark-hyper #'mostly)
(mark-hyper #'never)
(mark-hyper #'impossibly)
(mark-hyper #'non-sense)
(mark-hyper #'create))
;;
;; A II - prototypes
;;
;;
;;
;;
;; B - The means of combination
;;
;;
(defn h-first [hsx]
(hd/unbind hsx (sequence-marker 0)))
;; basically substitute the keys in the record with n - 1
(defn h-rest [hsx]
(let
;; that's what it boils down to anyway I think
[r (rest (unroll hsx))]
(apply ->sequence r)))
(defn pair [a b]
(->sequence a b))
(do
(mark-hyper #'h-first)
(mark-hyper #'h-rest)
(mark-hyper #'pair)
(mark-hyper #'->sequence)
(mark-hyper #'unroll))
;; the primitives of key-value pairs
(def bind hd/bind)
(def inverse hd/inverse)
(def unbind hd/unbind)
(def release unbind)
(defn ->struct [kvps])
(do
(mark-hyper #'bind)
(mark-hyper #'inverse)
(mark-hyper #'unbind)
(mark-hyper #'release)
(mark-hyper #'h-first)
(mark-hyper #'h-rest)
(mark-hyper #'pair))
(comment
(walk-cleanup (unroll (h-rest (pair (->prototype :a) (->prototype :b)))))
;; (:b)
(cleanup* (h-first (h-rest (pair (->prototype :a) (->prototype :b)))))
;; (:b)
)
;;
;; C - analogies / templates / frames
;;
;; the means of abstraction
;;
;; wip ...
;;
(defn substitute [e k v])
;;
;; I
;;
;; ================
;; The Expression
;; ================
;;
;; In hyperlisp, expressions are hypervectors
;;
;; The evaluator
;; -----------------------------------
;;
;; exp is a symbol: lookup in the environment
;;
;; exp is if: Evaluate the condition, lookup the condition in the clj memeory,
;; for each truthy branch, evaluate the consequence
;; fore each falsy branch, evaluate the alternative
;; evaluate to the superposition of the outcomes
;;
;; exp is let: Evaluate the bindings, augment the environment, evaluate the body
;;
;; exp is lambda: Return a hypervector that represents the lambda
;;
;; exp is a sequence: Evaluate the first element and treat it as a function
;; Evaluate the rest of the elements and treat them as arguments
;;
;;
;; if the operator is a primitive, apply the primitive, with the clj values from cleanup memory
;; if the operator is hyper-fn, do not cleanup to clj, else the same
;;
;; Do this with all 'argument branches' (cartisian product of possible arguments in this implementation)
;;
;; if the operator is a lambda, evaluate the lambda
;;
;; To eval a lambda:
;; augment the environment with the parameters and arguments
;; evaluate the body with the new environment
;;
;; The result is the superposition of the outcomes
;;
;;
;;
;; if the exp is anything else, it evaluates to itself
;;
;;
;;
;;
;; the *h-environment* could be a sparse distributed memory ?
;;
;; here, I will make the h-enviroment be hypervector map
;; (a set of key value bound pairs like `->record`)
;;
;;
(def ^:dynamic *h-environment* nil)
(declare h-apply)
(declare h-eval)
(defn start-symbol
[exp]
(and (h-seq? exp)
(cleanup-lookup-value (known (h-nth exp 0)))))
(def special? '#{if let lambda})
(defn let? [exp] (= 'let (start-symbol exp)))
(defn lambda? [exp] (= 'lambda (start-symbol exp)))
(defn if? [exp] (= 'if (start-symbol exp)))
(defn branch? [exp] (= 'branch (start-symbol exp)))
(defn augment-environment
"Returns a new environment with a binding for k->v added."
[env k v]
(hd/bundle env (hd/bind k v)))
(defn eval-let
([exp] (eval-let exp *h-environment*))
([exp env]
(let [bindings (for [[k v] (partition
2
(unroll (known (h-nth exp
1))))]
[(known k) (h-eval v env)])
body (known (h-nth exp 2))
new-env
(if bindings
(hd/thin
(reduce (fn [env [k v]]
(augment-environment env k v))
env
bindings))
env)]
(h-eval body new-env))))
(comment
;; let makes an environment, the evaluator looks up what is bound
(cleanup*
(h-eval
(clj->vsa ['let ['a 100 'b 200] 'a])
(non-sense)))
;; mix primitives work of course
(cleanup*
(h-eval
(clj->vsa ['let ['a [mix 50 20] 'b 200] 'a])
(create)))
;; (20 50)
;; and here is something thought provoking:
(cleanup*
(h-eval
(clj->vsa
['let ['b 5]
['let
['b 200]
'b]])
(create)))
;; (200 5)
;; instead of shadowing, the environment creates a superposition of values
;; and the ambiguity primitives work as expected:
(cleanup*
(h-eval
(clj->vsa
['let ['b 5]
['let
['b 200]
[never 'b 200]]])
(create)))
;; (5)
(cleanup*
(h-eval
(clj->vsa ['let ['b 5] ['lambda [] 'b]])
(create)))
;; this is a lambda that captures the environment
(def thelambda (h-eval (clj->vsa ['let ['b 5] ['lambda [] 'b]]) (create)))
;; hyperlambdas are hypervectors
thelambda
;; #tech.v3.tensor<int8>[10000]
;; [0 0 0 ... 0 0 0]
;; hyperlambdas have a body, environment and parameters
(cleanup* (procedure->body thelambda))
;; (b)
(procedure->environment thelambda)
;; #tech.v3.tensor<int8>[10000]
;; [0 0 0 ... 0 0 0]
;; the env is just a hypervector representing a map (hypermap ?)
(cleanup*
(hd/unbind
(procedure->environment thelambda)
(clj->vsa 'b)))
;; (5)
;; calling a hyperlambda:
(cleanup* (h-eval (clj->vsa [thelambda])))
;; (5)
;; messing with the environment:
;; Lambda objects only take the environment into account
;; that they are created with
;; (no binding of dynamic vars)
(cleanup* (h-eval (clj->vsa ['let ['b 100] [thelambda]])))
;; (5)
;; lol, creating hyperlambda with superposition of environments and calling it:
(cleanup*
(h-eval (clj->vsa [['let ['b 5]
['let ['b 100]
['lambda [] 'b]]]])))
;; (100 5)
;; and now b + 100 means 2 things:
(cleanup*
(h-eval (clj->vsa [['let ['b 5]
['let ['b 100]
['lambda [] [+ 100 'b]]]]])))
;; (200 105)
;; redefine coin:
(def coin-hyper
(h-eval (clj->vsa ['let ['coin [mix :heads :tails]]
['lambda [] 'coin]])))
;; this is a hypervector
coin-hyper
;; #tech.v3.tensor<int8>[10000]
;; [0 0 0 ... 0 0 0]
;; and the evaluator can call it as function,
(h-eval (clj->vsa [coin-hyper]))
;; #tech.v3.tensor<int8>[10000]
;; [0 0 0 ... 0 0 0]
;; the ouctome is a hypervector
;; .. that represents the superposition of multiple symbols:
(cleanup* (h-eval (clj->vsa [coin-hyper]) (create)))
;; (:heads :tails)
;; never heads:
(cleanup*
(h-eval
(clj->vsa
[never [coin-hyper] :heads])
(create)))
;; (:tails)
;; this results in value that is possibly heads or tails or foo
(cleanup*
(h-eval
(clj->vsa [possibly [coin-hyper] :foo])
(create)))
;; (:foo :heads :tails)
;; a mix of lambdas is also a thing:
(cleanup*
(h-eval
(clj->vsa [[mix
['lambda ['a] [* 2 'a]]
['lambda ['a] [* 10 'a]]]
10])))
;; (20 100)
(cleanup*
(h-eval
(clj->vsa
[[mix ['lambda ['a] [* 2 'a]]
['lambda ['a]
[[mix + - *] 10 'a]]] 10])))
;; (20 0 100)
(cleanup*
(h-eval
(clj->vsa
[[mix ['lambda ['a] [* 2 'a]]
['lambda ['a]
[[mix + - *] 5 'a]]] 10])))
;; (20 50 15 -5)
(cleanup* (h-eval (clj->vsa [[mix + - *] 10 10])))
;; (20 100 0)
(cleanup*
(h-eval
(clj->vsa
['let ['outcome
[['lambda []
['if [possibly true false]
:heads :tails]]]]
[impossibly 'outcome :tails]])))
(:heads))
(defn lambda-expr->parameters [exp] (known (h-nth exp 1)))
(defn lambda-expr->body [exp] (known (h-nth exp 2)))
;; Idea 1:
;;
;; eval lambda returns a function, capturing the `environment`
;;
;; Idea 2:
;; eval lambda returns a hypervector
;; that is a record of {:env :parameters :body}
;;
;; -> There is somehow the notion here that 2 hyperlambdas become similar,
;; when their parameters are similar.
;; I feel like there is something we can observe in cognition perhaps.
;; That we find the overlaps between the roles of templates/frames/transformations.
;; E.g. the role of honey on bread, there is something about this honey that is similar
;; to the role of lava on stone.
;;
;;
#_(defn eval-lambda
[exp environment]
(let [parameters (lambda-expr->parameters exp)
body (lambda-expr->body exp)]
(with-meta
(fn [& arguments]
(let [new-env
(hd/thin
(reduce (fn [env [k v]]
(augment-environment env k v))
environment
(map vector parameters arguments)))]
(binding [*h-environment* new-env]
(h-eval body))))
{:hyper-fn true})))
(defn eval-lambda
[exp environment]
;; I need the env in the memory, else it get's to
;; dirty for what I want to do
(remember-soft environment 0.9)
(let [parameters (lambda-expr->parameters exp)
body (lambda-expr->body exp)
lambda (clj->vsa {:body body
:compound-procedure? true
:environment environment
:parameters parameters})]
(remember-soft lambda 0.9)
lambda))
;; reference:
;; https://mitp-content-server.mit.edu/books/content/sectbyfn/books_pres_0/6515/sicp.zip/full-text/book/book-Z-H-26.html#%25_idx_4236
;;
(defn extend-environment
[variables values environment]
(hd/thin
(reduce
(fn [env [k v]] (augment-environment env k v))
environment
(map vector variables values))))
(defn procedure->body [proc] (known (hd/unbind proc (clj->vsa :body))))
(defn procedure->parameters [proc]
(map known (unroll (known (hd/unbind proc (clj->vsa :parameters))))))
(defn procedure->environment [proc]
(known
(hd/unbind proc (clj->vsa :environment))))
(defn eval-compound-procedure
[proc arguments]
(let [new-env (extend-environment
(procedure->parameters proc)
arguments
;; this can be nil, then every thing
;; is free variables
(or (procedure->environment proc)
(non-sense)))]
(h-eval (procedure->body proc) new-env)))
(defn variable?
[exp]
(symbol? (cleanup-lookup-value exp)))
(defn lookup-variable [exp env]
(hd/unbind env exp))
(defn fabricate-environment
[kvps]
(hd/thin
(reduce (fn [env [k v]]
(augment-environment env
(clj->vsa k)
(clj->vsa v)))
(hd/->hv)
kvps)))
;; II
;;
;; ======================
;; Multi expressions and branches
;; ======================
;;
;; In hyperlisp,
;;
;; - expressions can evaluate to more than 1 thing (multi symbols)
;; - the `if` expression returns a superposition of the outcome of branches
;; - the `apply` returns a superposition of possible argument lists
;;
;;
(defn branches [exp]
(cleanup* exp))
(defn condition->branches [condition]
;; everything above threshold comes out of the thing
(branches condition))
(defn if-condition [exp]
(known (h-nth exp 1)))
(defn if-consequence [exp]
(known (h-nth exp 2)))
(defn if-alternative [exp]
(known (h-nth exp 3)))
(defn h-truthy? [o]
;; Alternatively,
;; could be 'known?'
;;
(if
(= :nothing o)
false
(when o true)))
(defn eval-if
([exp] (eval-if exp *h-environment*))
([exp env]
(let [branches (condition->branches
(h-eval (if-condition exp) env))]
;; to thin or not to thin is a question
;; Because you lose precision
;;
(if-not (seq branches)
;; that would be an error?
(non-sense)
(hd/thin (apply hd/bundle
(for [branch branches]
(if (h-truthy? branch)
(h-eval (if-consequence exp) env)
(h-eval (if-alternative exp)
env)))))))))
(defn
branch->antecedent
[exp]
(known (h-nth exp 1)))
(defn
branch->postcedent
[exp]
(known (h-nth exp 2)))
;; do you return a sequence of outcomes?
;; or a superposition of outcomes?
(defn eval-branch
[exp env]
(let [antecedent (h-eval (branch->antecedent exp) env)
postcedent (h-eval (branch->postcedent exp))
collapsed-branches (lookup* auto-a-memory
antecedent
;; dynamic threshold
;; would be
;; interesting
0.1)]
;; [collapsed-branches
;; antecedent
;; postcedent]
(hd/thin (apply hd/bundle
(for [collapsed collapsed-branches]
(h-apply postcedent [collapsed] env))))))
(defn h-eval
([exp] (h-eval exp (or *h-environment* (non-sense))))
([exp env]
(cond
;;
;; possiblity: I. hyper eval looks up
;; hypervectors in the cleanup memeory
;;
;; possiblity: II. hyper eval ruturns hdv, for an
;; hdv
;;
;;
(branch? exp) (eval-branch exp env)
(lambda? exp) (eval-lambda exp env)
(if? exp) (eval-if exp env)
(let? exp) (eval-let exp env)
(h-seq? exp)
(let [lst (unroll exp)]
(h-apply (h-eval (first lst) env)
(into [] (map #(h-eval % env) (rest lst)))
env))
(variable? exp) (lookup-variable exp env)
;; (self-evaluating? exp)
:else exp)))
(def primitive-op? ifn?)
(defn compound-procedure?
[op]
(boolean (known (hd/unbind op (clj->vsa :compound-procedure?)))))
(defn primitive-type
[op]
(cond (:hyper-fn (meta op)) :hyper-fn
(ifn? op) :primitive))
;;
;; A cartesian-product arg-branches implementations
;; Different versions are thinkable
;;
(defn arg-branches [arguments]
(let [arglists (map cleanup* arguments)]
(apply combo/cartesian-product arglists)))
(defn h-apply
([op arguments] (h-apply op arguments *h-environment*))
([op arguments env]
(let [primitive-outcomes
(for [op (branches op)]
(clj->vsa (case (primitive-type op)
:primitive
;; (+ 1 2 3)
(hd/thin
(apply hd/bundle
;; (+ (mix1 1 10) 20)
(let [branches (arg-branches
arguments)]
(if (seq? branches)
(for [branch branches]
(clj->vsa (apply op
branch)))
[(clj->vsa (op))]))))
:hyper-fn (apply op arguments))))
compound-outcomes
(doall (map #(eval-compound-procedure %
arguments)
(filter compound-procedure?
(lookup* auto-a-memory op 0.3))))]
(if-not (seq (concat primitive-outcomes
compound-outcomes))
;; guess that's an error
(non-sense)
(hd/thin (apply hd/bundle
(concat primitive-outcomes
compound-outcomes)))))))
;;
;;
;; III. The reader
;;
;; This is for convinience.
;;
;;
;; - Clojure sets become a sumset (bundle).
;; - Clojure maps become a sumset of bound pairs.
;; - Clojure vectors become a hyper sequence.
;;
(defn set-expr [exp]
(into [#'mix] exp))
(defn map-expr [exp]
(into
[#'mix]
(for [[k v] exp]
[#'bind k v])))
(defn vec-expr [exp]
(into [->sequence] exp))
(defn analyse-expression
[clj-exp]
(cond
(set? clj-exp) (set-expr (map analyse-expression
clj-exp))
(map? clj-exp)
(map-expr (map (fn [[k v]] [(analyse-expression k)
(analyse-expression v)])
clj-exp))
(and (list? clj-exp) (= 'let (first clj-exp)))
(list 'let
(into []
(map analyse-expression (nth clj-exp 1)))
(analyse-expression (nth clj-exp 2)))
(and (list? clj-exp) (= 'lambda (first clj-exp)))
(list 'lambda
(into []
(map analyse-expression (nth clj-exp 1)))
(analyse-expression (nth clj-exp 2)))
(and (list? clj-exp) (= 'fn (first clj-exp))) (eval
clj-exp)
(vector? clj-exp) (vec-expr (map analyse-expression
clj-exp))
(list? clj-exp) (into []
(map analyse-expression clj-exp))
;; guess I'm kludgin it up, but hey clj meta
;; data and namespaces are simply amazing
:else (let [hypersymbols (into {}
;; (map (juxt key
;; key))
(filter (fn [[sym v]]
(when (var? v)
(:hyper-fn
(meta v))))