forked from kennytilton/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
synapse-types.lisp
executable file
·143 lines (121 loc) · 6.01 KB
/
synapse-types.lisp
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
;; -*- mode: Lisp; Syntax: Common-Lisp; Package: cells; -*-
#|
Cells -- Automatic Dataflow Managememnt
(See defpackage.lisp for license and copyright notigification)
|#
(in-package :cells)
(export! f-find)
(defmacro f-find (synapse-id sought where)
`(call-f-find ,synapse-id ,sought ,where))
(defun call-f-find (synapse-id sought where)
(with-synapse synapse-id ()
(bif (k (progn
(find sought where)))
(values k :propagate)
(values nil :no-propagate))))
(defmacro f-sensitivity (synapse-id (sensitivity &optional subtypename) &body body)
`(call-f-sensitivity ,synapse-id ,sensitivity ,subtypename (lambda () ,@body)))
(defun call-f-sensitivity (synapse-id sensitivity subtypename body-fn)
(with-synapse synapse-id (prior-fire-value)
(let ((new-value (funcall body-fn)))
;(trc "f-sensitivity fire-p decides new" new-value :from-prior prior-fire-value :sensi sensitivity)
(let ((prop-code (if (or (xor prior-fire-value new-value)
(eko (nil "sens fire-p decides" new-value prior-fire-value sensitivity)
(delta-greater-or-equal
(delta-abs (delta-diff new-value prior-fire-value subtypename)
subtypename)
(delta-abs sensitivity subtypename)
subtypename)))
:propagate
:no-propagate)))
(values (if (eq prop-code :propagate)
(progn
(trc nil "sense prior fire value now" new-value)
(setf prior-fire-value new-value))
new-value) prop-code)))))
(defmacro f-delta (synapse-id (&key sensitivity (type 'number)) &body body)
`(call-f-delta ,synapse-id ,sensitivity ',type (lambda () ,@body)))
(defun call-f-delta (synapse-id sensitivity type body-fn)
(with-synapse synapse-id (last-relay-basis last-bound-p delta-cum)
(let* ((new-basis (funcall body-fn))
(threshold sensitivity)
(tdelta (delta-diff new-basis
(if last-bound-p
last-relay-basis
(delta-identity new-basis type))
type)))
(trc nil "tdelta, threshhold" tdelta threshold)
(setf delta-cum tdelta)
(let ((propagation-code
(when threshold
(if (delta-exceeds tdelta threshold type)
(progn
(setf last-bound-p t)
(setf last-relay-basis new-basis)
:propagate)
:no-propagate))))
(trc nil "f-delta returns values" delta-cum propagation-code)
(values delta-cum propagation-code)))))
(defmacro f-plusp (key &rest body)
`(with-synapse ,key (prior-fire-value)
(let ((new-basis (progn ,@body)))
(values new-basis (if (xor prior-fire-value (plusp new-basis))
(progn
(setf prior-fire-value (plusp new-basis))
:propagate)
:no-propagate)))))
(defmacro f-zerop (key &rest body)
`(with-synapse ,key (prior-fire-value)
(let ((new-basis (progn ,@body)))
(values new-basis (if (xor prior-fire-value (zerop new-basis))
(progn
(setf prior-fire-value (zerop new-basis))
:propagate)
:no-propagate)))))
;;;(defun f-delta-list (&key (test #'true))
;;; (with-synapse (prior-list)
;;; :fire-p (lambda (syn new-list)
;;; (declare (ignorable syn))
;;; (or (find-if (lambda (new)
;;; ;--- gaining one? ----
;;; (and (not (member new prior-list))
;;; (funcall test new)))
;;; new-list)
;;; (find-if (lambda (old)
;;; ;--- losing one? ----
;;; (not (member old new-list))) ;; all olds have passed test, so skip test here
;;; prior-list)))
;;;
;;; :fire-value (lambda (syn new-list)
;;; (declare (ignorable syn))
;;; ;/// excess consing on long lists
;;; (setf prior-list (remove-if-not test new-list)))))
;;;(defun f-find-once (finder-fn)
;;; (mk-synapse (bingo bingobound)
;;;
;;; :fire-p (lambda (syn new-list)
;;; (declare (ignorable syn))
;;; (unless bingo ;; once found, yer done
;;; (setf bingobound t
;;; bingo (find-if finder-fn new-list))))
;;;
;;; :fire-value (lambda (syn new-list)
;;; (declare (ignorable syn))
;;; (or bingo
;;; (and (not bingobound) ;; don't bother if fire? already looked
;;; (find-if finder-fn new-list))))))
;;;(defun fdifferent ()
;;; (mk-synapse (prior-object)
;;; :fire-p (lambda (syn new-object)
;;; (declare (ignorable syn))
;;; (trc nil "fDiff: prior,new" (not (eql new-object prior-object))
;;; prior-object new-object)
;;; (not (eql new-object prior-object)))
;;;
;;; :fire-value (lambda (syn new-object)
;;; (declare (ignorable syn))
;;; (unless (eql new-object prior-object)
;;; (setf prior-object new-object)))
;;; ))
;;;(defun f-boolean (&optional (sensitivity 't))
;;; (f-delta :sensitivity sensitivity :type 'boolean))