úÎz¡y     \A wrapper for newForeignPtr that handles nullPtrs, and can be chained to an IO Ptr creator. Example usage:  ; myPtrCreator = (createForeignPtr deallocFunc) . allocFunc .where, allocFunc :: a->b->c->...-> IO (Ptr z) Fails if the ptr is nullPtr Names a failure tested on GHC only experimental#Noam Lewis <jones.noamle@gmail.com> TODO: What's the semantic model for  a?  a b6 is the type of time-dependent processors, such that:  ' [[ 'IOSource' a b ]] = (a, Time) -> b lThus, it is ok to implement a processing action that outputs arbitrary time-dependent values during runtime ^ regardless of input. (Although the more useful case is to calculate something from the input a that is  also time-dependent. The a0 input is often not required and in those cases a = () is used. *Notice that this means that IOSource doesn't qualify as an . However, currently the  implementation does NOT4 enforce this, i.e. IOSource is not a newtype; I don't know how to implement it : correctly. Also, one question is whether primitives like chain will have to disallow placing  H as the second element in a chain. Maybe they should, maybe they shouldn't. The semantic model for  is a function:  " [[ 'IOProcessor' a b ]] = a -> b UTo satisfy this model, the Processor value (the implementation) must obey the rules:   The processing function ( a -> x -> m x>) must act as if purely, so that indeed for a given input the m output is always the same. One particular thing to be careful with is that the output does not depend ( on time (for example, you shouldn'5t use IOProcessor to implement an input device). The IOSource type m is defined exactly for time-dependent processors. For pointer typed inputs and outputs, see next law. ' For processors that work on pointers,  [[ Ptr t ]] = t&. This is guaranteed by the following & implementation constraints for IOProcessor a b:  If a is a pointer type ( a = Ptr pC), then the processor must NOT write (modify) the referenced data.  If b] is a pointer, the memory it points to (and its allocation status) is only allowed to change f by the processor that created it (in the processing and releasing functions). In a way this , generalizes the first constraint. Note, that unlike Yampa8, this model does not allow transformations of the type (Time -> a) -> (Time ->  b)f. The reason is that I want to prevent arbitrary time access (whether causal or not). This limitation & means that everything is essentially  point-wise5 in time. To allow memory-full operations under this  model,  is defined. See  5http://www.ee.bgu.ac.il/~noamle/_downloads/gaccum.pdf for more about  arbitrary time access. The type of Processors  a, b> = the input and output types of the processor (think a -> b) 7 x = type of internal state (existentially quantified) &The arguments to the constructor are:    a -> x ->m xX - Processing function: Takes input and internal state, and returns new internal state.  a -> m x~ - Allocator for internal state (this is run only once): Takes (usually the first) input, and returns initial internal state.  x -> m bT - Convertor from state x to output b: Takes internal state and returns the output.   x -> m ()} - Releaser for internal state (finalizer, run once): Run after processor is done being used, to release the internal state. !TODO: re-define in terms that don' t need the x* existential (and the allocator), using a ) continuation-style processing function. TODO: do we need this? we'Are exporting the data constructor anyway for now, so maybe we don't. 7Chains two processors serially, so one feeds the next. jA processor that represents two sub-processors in parallel (although the current implementation runs them 2 sequentially, but that may change in the future) jConstructs a processor that: given two processors, gives source as input to both processors and runs them S independently, and after both have have finished, outputs their combined outputs. Semantic meaning, using Arrow's (&&& ) operator:  [[ forkJoin ]] = &&& i Or, considering the Applicative instance of functions (which are the semantic meanings of a processor):  [[ forkJoin ]] = liftA2 (,) + Alternative implementation to consider: f &&& g = (,)  & f  * g ?The identity processor: output = input. Semantically, [[ empty ]] = id JSplits (duplicates) the output of a functor, or on this case a processor. 'f --< g'H means: split f and feed it into g. Useful for feeding parallelized (***'d) processors.  For example, a --  (b *** c) = a >> (b &&& c) TRuns the processor once: allocates, processes, converts to output, and deallocates. YKeeps running the processing function in a loop until a predicate on the output is true. \ Useful for processors whose main function is after the allocation and before deallocation. ^Runs the processor once, but passes the processing + conversion action to the given function. >Creates a processor that operates around an inner processor. KUseful for sharing resources between two actions, a pre and a post action. The outer processor has two processing functions, pre: a->b and post: c->d. The last argument is the  inner processor,  Processor b c+. Thus, the resulting processor takes the a, processes it into a b, 1 feeds that through the inner processor to get a c!, and finally post-processes the c into a d. Example scenarioX: A singleton hardware device context, that cannot be duplicated or allocated more than 9 once. You need to both read and write to that device. It'1s not possible to create two processors, one for c reads and one for writes, because they need to use the same allocation (the device context). With _ wrapPrcessor you can have the read as the pre-processing and write as the post-processing. Let' s call the ; result of calling wrapProcessor except the last argument, myDeviceProcessor. Thus, you have:  C [[ myDeviceProcessor innerProc ]] = read >>> innerProc >>> write TODO: Find a more general / elegant solution to the shared resource problem. escanlT provides the primitive for performing memory-full operations on time-dependent processors, as  | described in  5http://www.ee.bgu.ac.il/~noamle/_downloads/gaccum.pdf. Untested, and also doesn't implement the limit as dt -> 0/ part of the model. Currently the precision of k the approximation is set by the samplerate (how many times per second the resulting processor is run, the ! more the better for precision). hscanlT and all its uses are probably most (or only?) useful in the context of Processor IO. However for 9 generality it is defined here on arbitrary Processor m. The Processor m a b? argument should really be time-dependent during runtime, so it' s model can't be a ->  b&. Thus it is most logical to use only # types for the processor argument. .Differentiate of time-dependent values, using  ,Integration of time-dependent values, using ,, implemented by trapezoidal approximation. Running maximum of a processor' s values Running minimum of a processor' s values VHolds a Maybe-valued processor and reports the time passed since last value was seen. Given a B-type processor, reverts back to a default value if no input was ' seen for more than a given time limit Finite impulse response A few tricks by Saizan from #haskell to perhaps use here:  first f = (,)  $ (arr fst >>> f)  * arr snd  arr f = f  $ id  f *** g = (arr fst >>> f) &&& (arr snd >>> g)           !allocated-processor-0.0.2Foreign.ForeignPtrWrapControl.ProcessorcreateForeignPtrcheckPtr errorNameIOSinkIOSource IOProcessor Processor processorchainparallelforkJoinemptysplit--<runrunUntilrunWith wrapProcessortracescanlT differentiate integratemaxPminP nStepsMemory holdMaybe revertAfterT discreteConvfir$fArrowProcessor