úÎwu2     #Bas van Dijk <v.dijk.bas@gmail.com>#The ParentOf class declares the parent/$child relationship between regions. 0A region is the parent of another region if they're either equivalent like:  RegionT ps pr `ParentOf` RegionT ps pr 8or if it is the parent of the parent of the child like:  RegionT ps ppr `ParentOf` RegionT cs + (RegionT pcs . (RegionT ppcs 5 (RegionT ps ppr)))  Duplicate an ± in the parent region. This ± will usually be a ( resource) but it can be any value "derived" from this regional handle. 3For example, suppose you run the following region:  runRegionT $ do $Inside this region you run a nested child region like:   r1hDup <- runRegionT $ do /Now in this child region you open the resource r1:   r1h < - open r1  ...yielding the regional handle r1h . Note that: <r1h :: RegionalHandle resource (RegionT cs (RegionT ps ppr))where cs is bound by the inner (child)  runRegionT and ps is bound by the outer (parent)  runRegionT. 6Suppose you want to use the resulting regional handle r1h in the parent region. You can' t simply  return r1h because then the type variable cs, escapes the inner region. HHowever, if you duplicate the regional handle you can safely return it.   r1hDup < - dup r1h  return r1hDup  Note that 2r1hDup :: RegionalHandle resource (RegionT ps ppr) 4Back in the parent region you can safely operate on r1hDup. 4A handle to an opened resource parameterized by the resource and the  region r in which it was created. A region which has . as its parent region which enables it to be:  directly executed in  by  , * concurrently executed in a new thread by  . An Opened resource4 is an internally used data type which associates a Mhandle to the resource with a reference count. Handles are reference counted because they may be  duplicated to a parent region using . The reference count is:  Initialized at 1 in .  Incremented in .  Decremented on termination of   (which is called by   and  ). NOnly when the reference count reaches 0 will the resource actually be closed. EAn internally used datatype which adds an existential wrapper around  OpenedResource to hide the resource type. If the resource type is not O hidden, regions must be parameterized by it. This is undesirable because then O only one type of resource can be opened in a region. The following allows all L kinds of resources to be opened in a single region as long as they have an  instance for . <A monad transformer in which scarce resources can be opened ;which are automatically closed when the region terminates. !Note that regions can be nested. pr) (for parent region) is a monad which is Pusually the region which is running this region. However when you are running a  the parent region will be .   Class of scarce: resources. A scarce resource is a resource that only one Guser can use at a time. (like a file, memory pointer or USB device for  example). 4Because of the scarcity, these resources need to be opened to grant temporary Psole access to the resource. When the resource is no longer needed it should be closed1 a.s.a.p to grant others access to the resource.  !MInternally used function that atomically decrements the reference count that is stored in the given IORef1. The function returns the decremented reference count. "MInternally used function that atomically increments the reference count that  is stored in the given IORef. *Execute a region inside its parent region pr. ?All resources which have been opened in the given region using  , and which haven't been duplicated using ,, will be closed on exit from this function 9wether by normal termination or by raising an exception. QAlso all resources which have been duplicated to this region from a child region  are closed on exit if they haven't been duplicated themselves. Note the type variable s7 of the region wich is only quantified over the region itself. This ensures that all" values, having a type containing s, can not >be returned from this function. (Note the similarity with the ST monad.)  An example of such a value is a ". Regional handles are created by %opening a resource in a region using (. Regional handles are parameterized by Ethe region in which they were created. So regional handles have this s in their Stype. This ensures that these regional handles, which may have been closed on exit from this function, can'7t be returned from this function. This ensures you can /never do any IO with a closed regional handle. @Note that it is possible to run a region inside another region. "Convenience funtion for running a  top-level region in .  Note that: runTopRegion =   )Return a region which executes the given  top-level region in a new thread. 7Note that the forked region has the same type variable s as the resulting Lregion. This means that all values which can be referenced in the resulting  region (like 4s for example) can also be referenced in the forked region. &For example the following is allowed:  runRegionT $ do  regionalHndl <- open resource  threadId </- forkTopRegion $ doSomethingWith regionalHndl # doSomethingElseWith regionalHndl Note that the  regionalHndl/ and all other resources opened in the current Othread are closed only when the current thread or the forked thread terminates whichever comes last. LInternally used function that actually runs the region on the given list of  opened resources. 2Get the internal handle from the regional handle. Warning:F This function should not be exported to or used by end-users because Cit allows them to close the handle manually, which will defeat the .safety-guarantees that this package provides! Tip: If you enable the  ViewPatterns language extension you can use internalHandle8 as a view-pattern as in the following example from the usb-safe package:  resetDevice :: (pr `ParentOf` cr, MonadIO cr) 5 -> RegionalHandle USB.Device pr -> cr () 9resetDevice (internalHandle -> (DeviceHandle ...)) = ... FOpen the given resource in a region yielding a regional handle to it. ONote that the returned regional handle is parameterized by the region in which Jit was created. This ensures that regional handles can never escape their Nregion. And it also allows operations on regional handles to be executed in a Echild region of the region in which the regional handle was created. Note that if you do; wish to return a regional handle from the region in which it was created you have to  duplicate the handle by applying  to it. IA convenience function which opens the given resource, applies the given Ncontinuation function to the resulting regional handle and runs the resulting region.  Note that:  with dev f =   ( dev # f) $Internally used function to register& the given opened resource by consing 3 it to the list of opened resources of the region. +Transform the computation inside a region. Lift a  catchError operation to the new monad. catch on the argument monad. Computation to attempt. Exception handler.    #Bas van Dijk <v.dijk.bas@gmail.com>  #Bas van Dijk <v.dijk.bas@gmail.com>     %      !" #$%&'()* regions-0.2!Control.Monad.Trans.Region.UnsafeControl.Monad.Trans.Region#Control.Monad.Trans.Region.InternalParentOfDupdupRegionalHandle TopRegionRegionTResourceHandle openResource closeResource runRegionT runTopRegion forkTopRegioninternalHandleopenwith mapRegionT liftCatch TypeCast2'' TypeCast2' TypeCast2unRegionalHandleghc-prim GHC.TypesIOOpened runRegionWith openedHandle refCntIORefSomeOpenedResourceSome unRegionT decrement incrementbaseGHC.Base>>=register