480 likes | 491 Views
Explore the fundamentals of declarative programming with pointers in compiler design. Learn the concepts of parallel and distributed processing, along with nesting and binding of mutable state. Dive into data structures, functions, circuits, and model-view dataflow in a declarative paradigm.
E N D
Declarative Objects 7/22/10 Jonathan Edwards sdg csail MIT
CMP AX,[BX] JE SKIP MOV AX,2
class Task { int start; int end;
class Task { int start; int end; int length { get {return end - start;} set {end = start + value;} }
class Task { int start; int end; int length { get {return end - start;} set {end = start + value;} } // start after t, increment length void slipAfter(Task t) {
class Task { int start; int end; int length { get {return end - start;} set {end = start + value;} } // start after t, increment length void slipAfter(Task t) { start = t.end; length = length + 1; } }
class Task { int start; int end; int length { get {return end - start;} set {end = start + value;} } // start after t, increment length void slipAfter(Task t) { start = t.end; length = length + 1; } } ✘
class Task { int start; int end; int length { get {return end - start;} set {end = start + value;} } // start after t, increment length void slipAfter(Task t) { length = length + 1; start = t.end; } } ✘
class Task { int start; int end; int length { get {return end - start;} set {end = start + value;} } // start after t, increment length void slipAfter(Task t) { int oldLength = length; start = t.end; length = oldLength + 1; }}
class Task { int start { get {return end - length;} set {end = value + length;} } int end; int length; // start after t, increment length void slipAfter(Task t) { length = length + 1; start = t.end; } }
? class Task { int start; int end; int length { get {return end - start;} set {end = start + value;} } void slipAfter(Task t) { int oldLength = length; start = t.end; length = oldLength + 1; } }
! Pac-Man
t start end length #3 this this start start set get – end end + length + 1 length #1 #2 #4 pre-state post-state
Task =obj { start: int end: int length ::= end – start length trig { end <= start + length} slipAfter =act { t: Task start <= t.end length <=\length + 1}}
Task =obj { start: int end: int length ::= end – start length trig { end <= start + length} slipAfter =act { t: Task start <= t.end length <=\length + 1}} #1 #4 #3 #2
\length := end – start length := \length + 1 start := t.end end := start + length Compiler
Declarative Programming what how Compiler
Parallel Processing what how Compiler
Distributed Processing what how Compiler
t start end length this this start start set get – end end + length + 1 length pre-state post-state
t start end length t == this this this start start set get – end end + length + 1 length pre-state post-state
t start end length t == this this this start start set get – end end + length + 1 length pre-state post-state
pointers ⇒ undecidable dataflow Huh? Compiler
Pick two ✘ Declarative programming Functions Circuits ✘ ✘ Data structures Imperative programming Mutable state
Nesting & Binding Model-View dataflow
Task start Project Project =obj { task1: Task task2: Task } end task1 length task2 Task start end length
Project task1 start Project =obj { task1: Task task2: Task } end length task2 start end length
Project task1 start Project =obj { task1: Task task2: Task task2.start ::=> task1.end } end length task2 start end length bidirectional binding
tasks:dom Task tasks @3F25C start end length @5E820 start end length
quantified binding cursor tasks:dom Task task1 => tasks tasks task1 @3F25C start end start length end length @5E820 start end length
tasks:dom Task task1 => tasks task2 => tasks tasks task1 @3F25C start end start length end length @5E820 task2 start start end end length length
tasks:dom Task task1 => tasks task2 => tasks task2.slipAfter(task1) tasks task1 @3F25C start end start length end length @5E820 task2 start start end end + length length
tasks:dom Task task1 => tasks task2 => tasks task2.slipAfter(task1) tasks task1 @3F25C start end start length end ✘ length @5E820 task2 start start end end + length length
task1 task1 start start end end length length @5E820 task2 @5E820 task2 start start start start end end + end end length length length length
DB update query Model get set View input output User
Model-View dataflow internal state • inputs • setters • triggers • outputs • getters • queries external world
Model-View dataflow internal state • inputs • setters • triggers • outputs • getters • queries ✘ external world
Model-View dataflow internal state • mutating • eager • functional • lazy external world
Model-View dataflow internal state • mutating • eager • callbacks • events • functional • lazy ✘ ✘ external world
New rules, new patterns • Synchronous reactive programming • Input event triggers atomic state transition • Output is pure function of new state • Asynchronicity layered on top • Syntax order irrelevant – no control flow • Pre-state readable throughout transition • Fields can change once per transition • Actions can’t see effects of own changes
Progression prog { t +=> task1 t.slipAfter(task2) step \t.end ?gtr 10 t.end <= 10 } progressive binding step result of prev step guard step
task2 start end length ✘ ✘ prog task1 t t task1 ? start start start start end + end gtr 10 10 end end length + 1 length length length ✘ ✘ pre-state post-state
task2 start end length hyp ✘ task1 t t task1 ? start start start start end + end gtr 10 10 end end length + 1 length length length pre-state post-state
Technical Summary • Nesting gives objects a location in global tree • Bindings propagate changes through relative paths in tree (precise static effects) • Bindings are directed: input, output, or both • Input is change-driven, cascades eagerly • Output is lazy pure functional • Each is statically acyclic based on tree path effects • Outputs do not feedback to inputs • Imperative islands in a declarative sea
Pick two Declarative programming Data structures Mutable state
Declarative Objects Declarative programming Nesting & Binding Model-View dataflow
Imperative Programming Declarative Programming CMP AX,[BX] JE SKIP MOV AX,2