CS321 Functional Programming 2 © JAS Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations that are performed in an order specified by the programmer (a control path). In a data flow system a program is a set of operations performed in an order determined by the data dependencies (and the availability of resources). This latter approach is clearly more appropriate to a functional style of programming than the conventional approach.
CS321 Functional Programming 2 © JAS Data Flow programs may be represented graphically op …………… It can be shown that a maximum of two inputs and two outputs Are required
CS321 Functional Programming 2 © JAS The basic idea is that data values (tokens) flow along the arcs in a graph built up from nodes. Whenever a node has all (necessary) inputs present it is said to be enabled. It then fires by absorbing its inputs and generating output values on its output arcs. Nodes can fire in parallel
CS321 Functional Programming 2 © JAS x Constant generator Copy + Typical operators =
CS321 Functional Programming 2 © JAS Switch control T F T F Merge Gates F control T
CS321 Functional Programming 2 © JAS A conditional expression control T F T F predicate then path else path
CS321 Functional Programming 2 © JAS Applying functions fac APPLY argument definition of function result or closure APPLY (2nd)argument
CS321 Functional Programming 2 © JAS A factorial function 1 >= F 1 fac - 1 T * F APPLY F F F F F
CS321 Functional Programming 2 © JAS A factorial function 1 >= F 1 fac - 1 T * F APPLY 1
CS321 Functional Programming 2 © JAS A factorial function 1 >= F 1 fac - 1 T * F APPLY T T T T T 1 1
CS321 Functional Programming 2 © JAS A factorial function 1 >= F 1 fac - 1 T * F APPLY F F 2 2 2
CS321 Functional Programming 2 © JAS Encoding the graph Each node can have A name (ID) An operation A list of inputs A list of outputs id op inputs outputs a+b1c1d2 -
CS321 Functional Programming 2 © JAS A factorial function 1 >= F 1 fac - 1 T * F APPLY (a,1,-,-,b1,-) a b (b,>=,a1,c1,d1,-) c (c,copy,inp,-,b2,e2) d e f g h i j k m n o p q (d,copy,b1,-,j1,e1) (e,F-gate,d2,c2,f1,-) (f,copy,f1,-,m1,i1) (g,1,-,-,i2,-) (h,fac,-,-,k1,-) (i,-,f2,g1,k2,-) (j,copy,d1,-,o1,p1) (k,apply,h1,i1,m2,-) (m,1,-,-,o2,-) (n,*,f1,k1,p2,-) (o,T-gate,j1,m1,q1,-) (p,F-gate,j2,n1,q2,-) (q,join,o1,p1,out,-)
CS321 Functional Programming 2 © JAS Note that naming both the inputs and the outputs is unnecessary – the node generating the values can determine which node needs them, but the node needs to know how many inputs it needs Also note that in all the examples given we never had 2 inputs and 2 outputs It is therefore possible that the size of these instruction packets could be reduced
CS321 Functional Programming 2 © JAS Execution Cycle (Data-Driven) FOR any instruction DO IF instruction has all its required inputs THEN execute the instruction
CS321 Functional Programming 2 © JAS Executing the apply node IF the first input is a single parameter function THEN generate a copy of the function create a new copy of each instr in the fn assign unique id's and modify output list FOR each output instr in the fn DO set output list to appropriate values from output list of apply node place 2 nd input value into input record of 1 st instr in function ELSE IF 1 st input is closure requiring 1 parameter THEN generate copy of function pass 2 nd input and closure to appropriate input records ELSE generate new closure with 2 nd input value
CS321 Functional Programming 2 © JAS A machine architecture to implement this execution cycle pool of packets processors
CS321 Functional Programming 2 © JAS Processing units Communication network Instruction selection mechanism Program store instructions Executable instructions Values/closures
CS321 Functional Programming 2 © JAS The Manchester Architecture instruction store processing units result queue switch matching store tasks results operand pairs input output interface to real world and other layers route for inputs to single operand instructions memory/control unit
CS321 Functional Programming 2 © JAS The Alice Architecture processors Network – originally slotted ring
CS321 Functional Programming 2 © JAS As well as this data-driven approach it is possible to envisage a demand-driven approach Instead of the arrival of data driving the execution we imagine a request for output being received at the bottom of the graph
CS321 Functional Programming 2 © JAS A factorial function 1 >= F 1 fac - 1 T * F APPLY
CS321 Functional Programming 2 © JAS Execution Cycle (Demand-Driven ) FOR any instruction whose output has been requested DO IF instruction has all required inputs THEN execute instruction ELSE CASE type of instr OF primitive, copy switch: send request to all instrs in source list gate: IF control input is present THEN IF control matches gate THEN send request to 2 nd instr ELSE no action ELSE request 1 st instr in source list apply: send request to 1 st instr in source list
CS321 Functional Programming 2 © JAS Executing the apply node IF the first input is a single parameter fn THEN generate copy of fn adjusting input and output lists ELSE IF first input is closure requiring 1 parameter THEN generate copy of fn ELSE generate closure, remembering id of 2 nd instr
CS321 Functional Programming 2 © JAS Data Flow as implementation of a functional language Every node represents a function output = node (input1, input2) Functional expressions can be represented by networks of nodes Basic property of data flow is no variables – single assignment – or zero assignment This is mirrored in functional languages Demand-driven = Lazy; Data-Driven = Eager/Applicative Order
CS321 Functional Programming 2 © JAS Data Flow Languages ManchesterLapse MITVAL IrivineId JapanValid ( ) Lawrence LivermoreSISAL