úÎi$g;     #Bas van Dijk <v.dijk.bas@gmail.com>The  RootRegion is the ancestor of any region. It'6s primary purpose is to tag regional handles which don' t have an = associated finalizer. For example the standard file handles stdin, stdout  and stderr6 which are opened on program startup and which shouldn't be 9 closed when a region terminates. Another example is the nullPtr which is a  memory pointer which doesn'(t point to any allocated memory so doesn' t need to  be freed. The AncestorRegion8 class is used to relate the region in which a resource Pwas opened to the region in which it is used. Take the following operation from the safer-file-handles package as an example:  hFileSize :: (pr `AncestorRegion`> cr, MonadIO cr) => RegionalFileHandle ioMode pr -> cr IntegerThe AncestorRegion class defines the parent /% child relationship between regions. The constraint    pr `AncestorRegion` cr is satisfied if and only if cr is a sequence of zero or more " s" (with varying s ) applied to pr, in other words, if cr is an (improper) nested subregion of pr. The class constraint InternalAncestorRegion pr cr! serves two purposes. First, the  instances of InternalAncestorRegion- do the type-level recursion that implements Qthe relation specified above. Second, since it is not exported, user code cannot add new instances of . (as these would have to be made instances of InternalAncestorRegion&, too), effectively turning it into a  closed class.  Duplicate an ± in the parent region. This ± will usually be some type of 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:  3r1h :: RegionalHandle (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 )r1hDup :: RegionalHandle (RegionT ps ppr) 4Back in the parent region you can safely operate on r1hDup. A handle to a - that allows you to duplicate it to a parent  region using . JDuplicating a finalizer means that instead of it being performed when the N current region terminates it is performed when the parent region terminates. An > computation that closes or finalizes a resource. For example  hClose or free. A > paired with its reference count which defines how many times ( it has been registered in some region. MA monad transformer in which scarce resources can be opened. When the region Aterminates, all opened resources will be closed automatically. It's a type error Eto return an opened resource from the region. The latter ensures no I/O with closed resources is possible.  Register the / in the region. When the region terminates all .registered finalizers will be perfomed if they're not duplicated to a parent region. \Note that finalizers are run in LIFO order (Last In First Out). So executing the following:   runRegionT $ do  _ <- onExit $ putStrLn " finalizer 1"  _ <- onExit $ putStrLn " finalizer 2"  return () yields:   finalizer 2  finalizer 1 *Execute a region inside its parent region pr. HAll resources which have been opened in the given region and which haven't been duplicated using 6, will be closed on exit from this function wether 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.) @Note that it is possible to run a region inside another region.  AReturn a region which executes the given 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 4region can also be referenced in the forked region. &For example the following is allowed:    $ do  regionalHndl <- open resource  threadId < - Region.  $ 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. Like   but internally uses Control.Concurrent.. Like   but internally uses  GHC.Conc. . !" Lift a callCC operation to the new monad. +Transform the computation inside a region. Lift a  catchError operation to the new monad.   #Bas van Dijk <v.dijk.bas@gmail.com>  #Bas van Dijk <v.dijk.bas@gmail.com>#Bas van Dijk <v.dijk.bas@gmail.com>    #Bas van Dijk <v.dijk.bas@gmail.com> +A mutable variable storing a value of type ± which can only be used  in region r, r's children or r'%s parent when you duplicate it using  . #$Transform the region type r of the regional reference to r' s parent so % that it can be used in that region. HYield a regional computation that returns a new regional reference that stores the given value. LNote that the reference is parameterized by the same region in which it was Ocreated. This ensures you can never use this reference outside that region and Eit allows you to use this reference in a child region of that region 0Read the value of the given regional reference. 5Write a new value into the given regional reference. DMutate the contents of the given regional reference using the given  function. KAtomically modifies the contents of the given regional reference using the given function. JThis function is useful for using a regional reference in a safe way in a Kmultithreaded program. If you only have one regional reference, then using atomicModifyRegionRef7 to access and modify it will prevent race conditions. %       !! "#$%$&'()* regions-0.8Control.Monad.Trans.Region!Control.Monad.Trans.Region.OnExit%Control.Monad.Trans.Region.ConcurrentData.RegionRef#Control.Monad.Trans.Region.Internal RootRegionAncestorRegionDupdupFinalizerHandle FinalizerRegionTonExit runRegionTforkIOforkOSforkOnIO liftCallCC mapRegionT liftCatch RegionRefpureDup newRegionRef readRegionRefwriteRegionRefmodifyRegionRefatomicModifyRegionRefInternalAncestorRegionRefCntghc-prim GHC.TypesIORefCountedFinalizer unRegionT runRegionWithbaseControl.Concurrent GHC.Conc.Syncfork increment unRegionRef