{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, NoMonomorphismRestriction #-} -- Abstracts "for" loops with I/O streams and state. The main point -- of this representation is to avoid having to use imperative -- language intermediates, and work with functional representations -- exclusively, allowing both function (TML Value) and syntactic (TML -- Code) representations. -- These "for" loops are represented by a fold-like operation with -- output. module SysFold () where import Prelude hiding (id, (.)) import Control.Arrow import Control.Category -- a: arrow (stream abstraction as an arrow between i and o) -- s: recursion state -- i: input stream element -- o: output stream element class Arrow a => SysFold a s i o where sigFold :: s -> ((s,i) -> (s,o)) -> a i o -- Represent as functions on infinite lists. data ListOp i o = ListOp ([i] -> [o]) instance Category ListOp where id = ListOp $ id (ListOp a) . (ListOp b) = ListOp $ a . b instance Arrow ListOp where arr = ListOp . map first = undefined -- ?? instance SysFold ListOp s i o where sigFold s0 f = ListOp $ fa s0 where fa s (i:is) = (o:os) where (s',o) = f (s, i) os = fa s' is intgr (s, i) = (s', s') where s' = s + i intgr' = sigFold 0 intgr -- The point here is of course not to just use jolly function -- implementations like ListOp, but to write instances of SysFold over -- syntax objects.