Skip to content
This repository
Browse code

Added Dutch HUG Day 2010 talk

Ignore-this: 3d491fd0a3fc30eeddc11959484e084f

darcs-hash:20100502164615-ae560-ec1a18748b888f41d9fc9680637f9091a06fe049.gz
  • Loading branch information...
commit 0a5cf1c83423625eb025c4af38ca9fca449aa896 1 parent 07a96f1
Bas van Dijk authored May 02, 2010
2  talks/clean.sh
... ...
@@ -0,0 +1,2 @@
  1
+#!/bin/bash
  2
+rm *.aux *.log *.nav *.out *.ptb *.snm *.toc *.tex
7  talks/make.sh
... ...
@@ -0,0 +1,7 @@
  1
+#!/bin/bash
  2
+
  3
+NAME="regions_Dutch-HUG-Day_2010"
  4
+
  5
+lhs2TeX -o ${NAME}.tex ${NAME}.lhs
  6
+
  7
+pdflatex ${NAME}.tex
1,365  talks/regions_Dutch-HUG-Day_2010.lhs
<
... ...
@@ -0,0 +1,1365 @@
  1
+\documentclass[9pt]{beamer}
  2
+
  3
+\usepackage{color}
  4
+\definecolor{rood} {rgb}{0.8, 0.0, 0.0}
  5
+\definecolor{groen}{rgb}{0.0, 0.6, 0.0}
  6
+\definecolor{blauw}{rgb}{0.0, 0.0, 0.8}
  7
+\definecolor{grijs}{rgb}{0.5, 0.5, 0.5}
  8
+\definecolor{geel} {rgb}{0.5, 0.5, 0.0}
  9
+
  10
+
  11
+%include polycode.fmt
  12
+
  13
+%format (ROOD (x))  = "\textcolor{rood}{" x "}"
  14
+%format (GROEN (x)) = "\textcolor{groen}{" x "}"
  15
+%format (BLAUW (x)) = "\textcolor{blauw}{" x "}"
  16
+%format (GRIJS (x)) = "\textcolor{grijs}{" x "}"
  17
+%format (GEEL (x))  = "\textcolor{geel}{" x "}"
  18
+
  19
+\usepackage{alltt}
  20
+
  21
+\usepackage[overlay,absolute]{textpos}
  22
+\setlength{\TPHorizModule}{10mm}
  23
+\setlength{\TPVertModule}{\TPHorizModule}
  24
+
  25
+\mode<presentation>
  26
+{
  27
+  \usetheme{Warsaw}
  28
+  % or ...
  29
+
  30
+  \setbeamercovered{transparent}
  31
+  % or whatever (possibly just delete it)
  32
+}
  33
+
  34
+\usepackage[english]{babel}
  35
+% or whatever
  36
+
  37
+\usepackage[latin1]{inputenc}
  38
+% or whatever
  39
+
  40
+\usepackage{times}
  41
+\usepackage[T1]{fontenc}
  42
+% Or whatever. Note that the encoding and the font should match. If T1
  43
+% does not look nice, try deleting the line with the fontenc.
  44
+
  45
+
  46
+\title{Lightweight Monadic Regions}
  47
+
  48
+\subtitle{Safely using scarce resources}
  49
+
  50
+\author{Bas van Dijk\\\texttt{v.dijk.bas@@gmail.com}}
  51
+
  52
+\date{Dutch HUG Day, 24 April 2010}
  53
+
  54
+% \subject{Theoretical Computer Science}
  55
+% This is only inserted into the PDF information catalog. Can be left
  56
+% out.
  57
+
  58
+% If you have a file called "university-logo-filename.xxx", where xxx
  59
+% is a graphic format that can be processed by latex or pdflatex,
  60
+% resp., then you can add a logo as follows:
  61
+
  62
+% \pgfdeclareimage[height=0.5cm]{university-logo}{university-logo-filename}
  63
+% \logo{\pgfuseimage{university-logo}}
  64
+
  65
+% If you wish to uncover everything in a step-wise fashion, uncomment
  66
+% the following command:
  67
+\beamerdefaultoverlayspecification{<+->}
  68
+
  69
+
  70
+\begin{document}
  71
+
  72
+\begin{frame}
  73
+  \titlepage
  74
+\end{frame}
  75
+
  76
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  77
+\section{Background}
  78
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  79
+
  80
+\begin{frame}{Background}
  81
+  \begin{itemize}
  82
+  \item Invented by: \textbf{Oleg Kiselyov \& Chung-chieh Shan}
  83
+  \item \texttt{cabal install \textbf{regions}}
  84
+  \end{itemize}
  85
+\end{frame}
  86
+
  87
+
  88
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  89
+\section{Guiding Example}
  90
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  91
+
  92
+\begin{frame}{Guiding Example}
  93
+\framesubtitle{Specification}
  94
+  \begin{enumerate}
  95
+  \item open two files for reading, one of them a configuration file;
  96
+  \item read the name of an output file (such as the log file) from the configuration file;
  97
+  \item open the output file and zip the contents of both input files into the output file;
  98
+  \item close the configuration file;
  99
+  \item copy the rest, if any, of the other input file to the output file;
  100
+  \item close both the output and the input file.
  101
+  \end{enumerate}
  102
+\end{frame}
  103
+
  104
+\begin{frame}{Guiding Example}
  105
+\framesubtitle{Example run}
  106
+  \begin{columns}[t]
  107
+    \begin{column}{.3\textwidth}
  108
+      \begin{block}{input.txt}
  109
+\begin{verbatim}
  110
+a
  111
+b
  112
+c
  113
+d
  114
+e
  115
+f
  116
+g
  117
+h
  118
+i
  119
+j
  120
+k
  121
+\end{verbatim}
  122
+      \end{block}
  123
+    \end{column}
  124
+    \begin{column}{.3\textwidth}
  125
+      \begin{block}{config.txt}
  126
+\begin{alltt}
  127
+\textcolor{blauw}{out.txt}\\
  128
+\textcolor{red}{
  129
+1\\
  130
+2\\
  131
+3\\
  132
+4}
  133
+\end{alltt}
  134
+      \end{block}
  135
+    \end{column}
  136
+    \begin{column}{.3\textwidth}
  137
+      \begin{block}{out.txt}
  138
+\begin{alltt}
  139
+\textcolor{red}{1}\\
  140
+a\\
  141
+\textcolor{red}{2}\\
  142
+b\\
  143
+\textcolor{red}{3}\\
  144
+c\\
  145
+\textcolor{red}{4}\\
  146
+d\\
  147
+e\\
  148
+f\\
  149
+g\\
  150
+h\\
  151
+i\\
  152
+j\\
  153
+k
  154
+\end{alltt}
  155
+      \end{block}
  156
+    \end{column}
  157
+  \end{columns}
  158
+\end{frame}
  159
+
  160
+\begin{frame}{Guiding Example}
  161
+\framesubtitle{Implementation}
  162
+\begin{code}
  163
+test :: IO ()
  164
+test = do
  165
+  hIn <- openFile "input.txt" ReadMode
  166
+  hOut <- test_internal hIn
  167
+  till (hIsEOF hIn) $ hGetLine hIn >>= hPutStrLn hOut
  168
+
  169
+test_internal :: Handle -> IO Handle
  170
+test_internal hIn = do
  171
+  hCfg <- openFile "config.txt" ReadMode
  172
+  fname <- hGetLine hCfg
  173
+  hOut <- openFile fname WriteMode
  174
+  till (liftM2 (||) (hIsEOF hCfg) (hIsEOF hIn)) $ do
  175
+    hGetLine hCfg  >>= hPutStrLn hOut
  176
+    hGetLine hIn   >>= hPutStrLn hOut
  177
+  return hOut
  178
+\end{code}
  179
+
  180
+\begin{textblock}{}(6.2,1.8)
  181
+{\color{grijs}
  182
+\begin{code}
  183
+till :: Monad m => m Bool -> m () -> m ()
  184
+till condition iteration = loop
  185
+    where
  186
+      loop = do  b <- condition
  187
+                 if b
  188
+                   then return ()
  189
+                   else iteration >> loop
  190
+\end{code}}
  191
+\end{textblock}
  192
+\end{frame}
  193
+
  194
+\begin{frame}{Guiding Example}
  195
+\framesubtitle{Implementation (with close)}
  196
+\begin{code}
  197
+test :: IO ()
  198
+test = do
  199
+  hIn <- openFile "input.txt" ReadMode
  200
+  hOut <- test_internal hIn
  201
+  till (hIsEOF hIn) $ hGetLine hIn >>= hPutStrLn hOut
  202
+  (ROOD(hClose hOut))
  203
+  (ROOD(hClose hIn))
  204
+
  205
+test_internal :: Handle -> IO Handle
  206
+test_internal hIn = do
  207
+  hCfg <- openFile "config.txt" ReadMode
  208
+  fname <- hGetLine hCfg
  209
+  hOut <- openFile fname WriteMode
  210
+  till (liftM2 (||) (hIsEOF hCfg) (hIsEOF hIn)) $ do
  211
+    hGetLine hCfg  >>= hPutStrLn hOut
  212
+    hGetLine hIn   >>= hPutStrLn hOut
  213
+  (ROOD(hClose hCfg))
  214
+  return hOut
  215
+\end{code}
  216
+\end{frame}
  217
+
  218
+\begin{frame}{Guiding Example}
  219
+\framesubtitle{Implementation (with exception handling)}
  220
+\begin{code}
  221
+test :: IO ()
  222
+test =
  223
+  (ROOD(bracket (openFile "input.txt" ReadMode) hClose)) $ \hIn ->
  224
+    (ROOD(bracket (test_internal hIn) hClose)) $ \hOut ->
  225
+      till (hIsEOF hIn) $ hGetLine hIn >>= hPutStrLn hOut
  226
+
  227
+test_internal :: Handle -> IO Handle
  228
+test_internal hIn = do
  229
+  (ROOD(bracket (openFile "config.txt" ReadMode) hClose)) $ \hCfg -> do
  230
+    fname <- hGetLine hCfg
  231
+    (ROOD(bracketOnError (openFile fname WriteMode) hClose)) $ \hOut -> do
  232
+      till (liftM2 (||) (hIsEOF hCfg) (hIsEOF hIn)) $ do
  233
+        hGetLine hCfg  >>= hPutStrLn hOut
  234
+        hGetLine hIn   >>= hPutStrLn hOut
  235
+      return hOut
  236
+\end{code}
  237
+\end{frame}
  238
+
  239
+\begin{frame}{Guiding Example}
  240
+\framesubtitle{Implementation}
  241
+\begin{code}
  242
+test :: IO ()
  243
+test =
  244
+  bracket (openFile "input.txt" ReadMode) hClose $ \hIn ->
  245
+    bracket (test_internal hIn) hClose $ \hOut ->
  246
+      till (hIsEOF hIn) $ hGetLine hIn >>= hPutStrLn hOut
  247
+
  248
+test_internal :: Handle -> IO Handle
  249
+test_internal hIn = do
  250
+  bracket (openFile "config.txt" ReadMode) hClose $ \hCfg -> do
  251
+    fname <- hGetLine hCfg
  252
+    bracketOnError (openFile fname WriteMode) hClose $ \hOut -> do
  253
+      till (liftM2 (||) (hIsEOF hCfg) (hIsEOF hIn)) $ do
  254
+        hGetLine hCfg  >>= hPutStrLn hOut
  255
+        hGetLine hIn   >>= hPutStrLn hOut
  256
+      return hOut
  257
+\end{code}
  258
+\end{frame}
  259
+
  260
+
  261
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  262
+\section{Explicit IOModes}
  263
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  264
+
  265
+\begin{frame}{IOModes}
  266
+\framesubtitle{Only read from read-only handles!}
  267
+\begin{code}
  268
+test :: IO ()
  269
+test =
  270
+  bracket (openFile "input.txt" (ROOD(ReadMode))) hClose $ \hIn ->
  271
+    bracket (test_internal hIn) hClose $ \hOut ->
  272
+      till (hIsEOF hIn) $ (ROOD(hGetLine hIn)) >>= hPutStrLn hOut
  273
+
  274
+test_internal :: Handle -> IO Handle
  275
+test_internal hIn = do
  276
+  bracket (openFile "config.txt" ReadMode) hClose $ \hCfg -> do
  277
+    fname <- hGetLine hCfg
  278
+    bracketOnError (openFile fname WriteMode) hClose $ \hOut -> do
  279
+      till (liftM2 (||) (hIsEOF hCfg) ((ROOD(hIsEOF hIn)))) $ do
  280
+        hGetLine hCfg         >>= hPutStrLn hOut
  281
+        (ROOD(hGetLine hIn))  >>= hPutStrLn hOut
  282
+      return hOut
  283
+\end{code}
  284
+\end{frame}
  285
+
  286
+\begin{frame}{IOModes}
  287
+\framesubtitle{Only write to write-only handles!}
  288
+\begin{code}
  289
+test :: IO ()
  290
+test =
  291
+  bracket (openFile "input.txt" ReadMode) hClose $ \hIn ->
  292
+    bracket (test_internal hIn) hClose $ \hOut ->
  293
+      till (hIsEOF hIn) $ hGetLine hIn >>= (BLAUW(hPutStrLn hOut))
  294
+
  295
+test_internal :: Handle -> IO Handle
  296
+test_internal hIn = do
  297
+  bracket (openFile "config.txt" ReadMode) hClose $ \hCfg -> do
  298
+    fname <- hGetLine hCfg
  299
+    bracketOnError (openFile fname (BLAUW(WriteMode))) hClose $ \hOut -> do
  300
+      till (liftM2 (||) (hIsEOF hCfg) (hIsEOF hIn)) $ do
  301
+        hGetLine hCfg  >>= (BLAUW(hPutStrLn hOut))
  302
+        hGetLine hIn   >>= (BLAUW(hPutStrLn hOut))
  303
+      return hOut
  304
+\end{code}
  305
+\end{frame}
  306
+
  307
+\begin{frame}{IOModes}
  308
+\framesubtitle{Encode IOModes in types}
  309
+\begin{itemize}
  310
+\item \texttt{cabal install \textbf{explicit-iomodes}}
  311
+\item |newtype Handle (ROOD(ioMode)) = Handle (System.IO.Handle)|
  312
+\item
  313
+\begin{code}
  314
+data ReadMode
  315
+data WriteMode
  316
+data AppendMode
  317
+data ReadWriteMode
  318
+\end{code}
  319
+\item |openFile :: FilePath -> IOMode (ROOD(ioMode)) -> IO (Handle (ROOD(ioMode)))|
  320
+\item
  321
+\begin{code}
  322
+data IOMode ioMode where
  323
+    ReadMode       :: IOMode (ROOD(ReadMode))
  324
+    WriteMode      :: IOMode (ROOD(WriteMode))
  325
+    AppendMode     :: IOMode (ROOD(AppendMode))
  326
+    ReadWriteMode  :: IOMode (ROOD(ReadWriteMode))
  327
+\end{code}
  328
+\end{itemize}
  329
+\end{frame}
  330
+
  331
+\begin{frame}{IOModes}
  332
+\framesubtitle{Constrain operations}
  333
+\begin{itemize}
  334
+\item
  335
+\begin{code}
  336
+hGetLine   :: (ROOD(ReadModes))   (ROOD(ioMode =>)) Handle (ROOD(ioMode)) -> IO String
  337
+hIsEOF     :: (ROOD(ReadModes))   (ROOD(ioMode =>)) Handle (ROOD(ioMode)) -> IO Bool
  338
+hPutStrLn  :: (ROOD(WriteModes))  (ROOD(ioMode =>)) Handle (ROOD(ioMode)) -> String -> IO ()
  339
+\end{code}
  340
+\item
  341
+\begin{code}
  342
+class ReadModes   ioMode
  343
+class WriteModes  ioMode
  344
+
  345
+instance ReadModes   ReadMode
  346
+instance ReadModes   ReadWriteMode
  347
+
  348
+instance WriteModes  WriteMode
  349
+instance WriteModes  AppendMode
  350
+instance WriteModes  ReadWriteMode
  351
+\end{code}
  352
+\end{itemize}
  353
+\end{frame}
  354
+
  355
+\begin{frame}{IOModes}
  356
+\framesubtitle{Only one import!}
  357
+\begin{code}
  358
+import System.IO.ExplicitIOModes
  359
+
  360
+test :: IO ()
  361
+test =
  362
+  bracket (openFile "input.txt" ReadMode) hClose $ \hIn ->
  363
+    bracket (test_internal hIn) hClose $ \hOut ->
  364
+      till (hIsEOF hIn) $ hGetLine hIn >>= hPutStrLn hOut
  365
+
  366
+test_internal ::  ReadModes ioMode =>
  367
+                  Handle ioMode -> IO (Handle WriteMode)
  368
+test_internal hIn = do
  369
+  bracket (openFile "config.txt" ReadMode) hClose $ \hCfg -> do
  370
+    fname <- hGetLine hCfg
  371
+    bracketOnError (openFile fname WriteMode) hClose $ \hOut -> do
  372
+      till (liftM2 (||) (hIsEOF hCfg) (hIsEOF hIn)) $ do
  373
+        hGetLine hCfg  >>= hPutStrLn hOut
  374
+        hGetLine hIn   >>= hPutStrLn hOut
  375
+      return hOut
  376
+\end{code}
  377
+\end{frame}
  378
+
  379
+\begin{frame}{IOModes}
  380
+\framesubtitle{Inferred types change (|hIn|)}
  381
+\begin{code}
  382
+import System.IO.ExplicitIOModes
  383
+
  384
+test :: IO ()
  385
+test =
  386
+  bracket (openFile "input.txt" ReadMode) hClose $ \hIn ->
  387
+    bracket (test_internal hIn) hClose $ \hOut ->
  388
+      till (hIsEOF hIn) $ hGetLine hIn >>= hPutStrLn hOut
  389
+
  390
+test_internal ::  (ROOD(ReadModes ioMode)) =>
  391
+                  Handle (ROOD(ioMode)) -> IO (Handle WriteMode)
  392
+test_internal (ROOD(hIn)) = do
  393
+  bracket (openFile "config.txt" ReadMode) hClose $ \hCfg -> do
  394
+    fname <- hGetLine hCfg
  395
+    bracketOnError (openFile fname WriteMode) hClose $ \hOut -> do
  396
+      till (liftM2 (||) (hIsEOF hCfg) ((ROOD(hIsEOF hIn)))) $ do
  397
+        hGetLine hCfg         >>= hPutStrLn hOut
  398
+        (ROOD(hGetLine hIn))  >>= hPutStrLn hOut
  399
+      return hOut
  400
+\end{code}
  401
+\end{frame}
  402
+
  403
+\begin{frame}{IOModes}
  404
+\framesubtitle{Inferred types change (|hOut|)}
  405
+\begin{code}
  406
+import System.IO.ExplicitIOModes
  407
+
  408
+test :: IO ()
  409
+test =
  410
+  bracket (openFile "input.txt" ReadMode) hClose $ \hIn ->
  411
+    bracket (test_internal hIn) hClose $ \hOut ->
  412
+      till (hIsEOF hIn) $ hGetLine hIn >>= hPutStrLn hOut
  413
+
  414
+test_internal ::  (ROOD(ReadModes ioMode)) =>
  415
+                  Handle (ROOD(ioMode)) -> IO (Handle (BLAUW(WriteMode)))
  416
+test_internal (ROOD(hIn)) = do
  417
+  bracket (openFile "config.txt" ReadMode) hClose $ \hCfg -> do
  418
+    fname <- hGetLine hCfg
  419
+    bracketOnError ((BLAUW(openFile fname WriteMode))) hClose $ \(BLAUW(hOut)) -> do
  420
+      till (liftM2 (||) (hIsEOF hCfg) ((ROOD(hIsEOF hIn)))) $ do
  421
+        hGetLine hCfg         >>= (BLAUW(hPutStrLn hOut))
  422
+        (ROOD(hGetLine hIn))  >>= (BLAUW(hPutStrLn hOut))
  423
+      return (BLAUW(hOut))
  424
+\end{code}
  425
+\end{frame}
  426
+
  427
+
  428
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  429
+\section{Regions}
  430
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  431
+
  432
+\subsection{Close automatically}
  433
+
  434
+\begin{frame}{Regions}
  435
+\framesubtitle{Don't forget to close!}
  436
+\begin{code}
  437
+test :: IO ()
  438
+test =
  439
+  bracket ((GROEN(openFile)) "input.txt" ReadMode) (ROOD(hClose)) $ \hIn ->
  440
+    bracket (test_internal hIn) (ROOD(hClose)) $ \hOut ->
  441
+      till (hIsEOF hIn) $ hGetLine hIn >>= hPutStrLn hOut
  442
+
  443
+test_internal ::  ReadModes ioMode =>
  444
+                  Handle ioMode -> IO (Handle WriteMode)
  445
+test_internal hIn = do
  446
+  bracket ((GROEN(openFile)) "config.txt" ReadMode) (ROOD(hClose)) $ \hCfg -> do
  447
+    fname <- hGetLine hCfg
  448
+    bracketOnError ((GROEN(openFile)) fname WriteMode) (ROOD(hClose)) $ \hOut -> do
  449
+      till (liftM2 (||) (hIsEOF hCfg) (hIsEOF hIn)) $ do
  450
+        hGetLine hCfg  >>= hPutStrLn hOut
  451
+        hGetLine hIn   >>= hPutStrLn hOut
  452
+      return hOut
  453
+\end{code}
  454
+\end{frame}
  455
+
  456
+\begin{frame}{Regions}
  457
+\framesubtitle{|withFile|}
  458
+\begin{code}
  459
+withFile :: FilePath -> IOMode -> (Handle -> IO a) -> IO a
  460
+withFile fp ioMode = bracket ((GROEN(openFile)) fp ioMode) (ROOD(hClose)
  461
+\end{code}
  462
+\end{frame}
  463
+
  464
+\begin{frame}{Regions}
  465
+\framesubtitle{Use |withFile| to abstract close}
  466
+\begin{code}
  467
+test :: IO ()
  468
+test =
  469
+  (BLAUW(withFile)) "input.txt" ReadMode $ \hIn ->
  470
+    bracket (test_internal hIn) (ROOD(hClose)) $ \hOut ->
  471
+      till (hIsEOF hIn) $ hGetLine hIn >>= hPutStrLn hOut
  472
+
  473
+test_internal ::  ReadModes ioMode =>
  474
+                  Handle ioMode -> IO (Handle WriteMode)
  475
+test_internal hIn = do
  476
+  (BLAUW(withFile)) "config.txt" ReadMode $ \hCfg -> do
  477
+    fname <- hGetLine hCfg
  478
+    bracketOnError ((GROEN(openFile)) fname WriteMode) (ROOD(hClose)) $ \hOut -> do
  479
+      till (liftM2 (||) (hIsEOF hCfg) (hIsEOF hIn)) $ do
  480
+        hGetLine hCfg  >>= hPutStrLn hOut
  481
+        hGetLine hIn   >>= hPutStrLn hOut
  482
+      return hOut
  483
+\end{code}
  484
+\end{frame}
  485
+
  486
+\begin{frame}{Regions}
  487
+\framesubtitle{Extend |openFile|}
  488
+\framesubtitle{}
  489
+\begin{code}
  490
+openFile :: FilePath -> IOMode ioMode -> IO (E.Handle ioMode)
  491
+\end{code}
  492
+\end{frame}
  493
+
  494
+\begin{frame}{Regions}
  495
+\begin{itemize}
  496
+\item
  497
+\begin{code}
  498
+openFile ::  (ROOD(MonadCatchIO pr =>))
  499
+             FilePath -> IOMode ioMode -> (ROOD(RegionT pr)) (E.Handle ioMode)
  500
+\end{code}
  501
+\item
  502
+%{
  503
+%format forall = "\forall"
  504
+%format . = ".\ "
  505
+\begin{code}
  506
+newtype RegionT pr a = RegionT
  507
+    {unRegionT :: ReaderT (IORef [AnyHandle]) pr a}
  508
+\end{code}
  509
+\item
  510
+\begin{code}
  511
+    deriving  (  Functor
  512
+              ,  Applicative
  513
+              ,  Alternative
  514
+              ,  Monad
  515
+              ,  MonadPlus
  516
+              ,  MonadFix
  517
+              ,  MonadTrans
  518
+              ,  MonadIO
  519
+              ,  MonadCatchIO
  520
+              )
  521
+\end{code}
  522
+\item
  523
+\begin{code}
  524
+data AnyHandle = forall ioMode. Any (Handle ioMode)
  525
+\end{code}
  526
+%}
  527
+\end{itemize}
  528
+\end{frame}
  529
+
  530
+\begin{frame}{Regions}
  531
+\framesubtitle{Implementation of |openFile|}
  532
+\begin{code}
  533
+openFile ::  MonadCatchIO pr =>
  534
+             FilePath -> IOMode ioMode -> RegionT pr (E.Handle ioMode)
  535
+openFile fp ioMode = block $ do
  536
+  h <- liftIO $ E.openFile fp ioMode
  537
+  register h
  538
+  return h
  539
+
  540
+register :: MonadCatchIO pr => Handle ioMode -> RegionT pr ()
  541
+register h = RegionT $ ask >>= liftIO . flip modifyIORef (Any h:)
  542
+\end{code}
  543
+\end{frame}
  544
+
  545
+\begin{frame}{Regions}
  546
+\framesubtitle{Call native |openFile|}
  547
+\begin{code}
  548
+openFile ::  MonadCatchIO pr =>
  549
+             FilePath -> IOMode ioMode -> RegionT pr (E.Handle ioMode)
  550
+openFile fp ioMode = block $ do
  551
+  h <- liftIO $ (ROOD(E.openFile)) fp ioMode
  552
+  register h
  553
+  return h
  554
+
  555
+register :: MonadCatchIO pr => Handle ioMode -> RegionT pr ()
  556
+register h = RegionT $ ask >>= liftIO . flip modifyIORef (Any h:)
  557
+\end{code}
  558
+\end{frame}
  559
+
  560
+\begin{frame}{Regions}
  561
+\framesubtitle{|register| handle}
  562
+\begin{code}
  563
+openFile ::  MonadCatchIO pr =>
  564
+             FilePath -> IOMode ioMode -> RegionT pr (E.Handle ioMode)
  565
+openFile fp ioMode = block $ do
  566
+  h <- liftIO $ (ROOD(E.openFile)) fp ioMode
  567
+  (BLAUW(register h))
  568
+  return h
  569
+
  570
+register :: MonadCatchIO pr => Handle ioMode -> RegionT pr ()
  571
+register h = RegionT $ ask >>= liftIO . flip modifyIORef (Any h:)
  572
+\end{code}
  573
+\end{frame}
  574
+
  575
+\begin{frame}{Regions}
  576
+\framesubtitle{|register| handle}
  577
+\begin{code}
  578
+openFile ::  MonadCatchIO pr =>
  579
+             FilePath -> IOMode ioMode -> RegionT pr (E.Handle ioMode)
  580
+openFile fp ioMode = block $ do
  581
+  h <- liftIO $ (ROOD(E.openFile)) fp ioMode
  582
+  (BLAUW(register h))
  583
+  return h
  584
+
  585
+(BLAUW(register :: MonadCatchIO pr => Handle ioMode -> RegionT pr ()))
  586
+(BLAUW(register h = RegionT $ ask >>= liftIO . flip modifyIORef (Any h:)))
  587
+\end{code}
  588
+\end{frame}
  589
+
  590
+\begin{frame}{Regions}
  591
+\framesubtitle{|block| asynchronous exceptions}
  592
+\begin{code}
  593
+openFile ::  MonadCatchIO pr =>
  594
+             FilePath -> IOMode ioMode -> RegionT pr (E.Handle ioMode)
  595
+openFile fp ioMode = (GROEN(block)) $ do
  596
+  h <- liftIO $ (ROOD(E.openFile)) fp ioMode
  597
+  (BLAUW(register h))
  598
+  return h
  599
+
  600
+(BLAUW(register :: MonadCatchIO pr => Handle ioMode -> RegionT pr ()))
  601
+(BLAUW(register h = RegionT $ ask >>= liftIO . flip modifyIORef (Any h:)))
  602
+\end{code}
  603
+\end{frame}
  604
+
  605
+\begin{frame}{Regions}
  606
+\framesubtitle{Running regions}
  607
+\begin{code}
  608
+runRegionT :: MonadCatchIO pr => RegionT pr a -> pr a
  609
+runRegionT r = bracket   (liftIO $ newIORef [])
  610
+                         (liftIO . after)
  611
+                         (runReaderT $ unRegionT r)
  612
+    where
  613
+      after ioref = do  hs <- readIORef ioref
  614
+                        forM_ hs $ \(Any h) -> hClose h
  615
+\end{code}
  616
+\end{frame}
  617
+
  618
+\begin{frame}{Regions}
  619
+\framesubtitle{Create initial empty list of handles}
  620
+\begin{code}
  621
+runRegionT :: MonadCatchIO pr => RegionT pr a -> pr a
  622
+runRegionT r = bracket   (liftIO $ (ROOD(newIORef [])))
  623
+                         (liftIO . after)
  624
+                         (runReaderT $ unRegionT r)
  625
+    where
  626
+      after ioref = do  hs <- readIORef ioref
  627
+                        forM_ hs $ \(Any h) -> hClose h
  628
+\end{code}
  629
+\end{frame}
  630
+
  631
+\begin{frame}{Regions}
  632
+\framesubtitle{Run given region}
  633
+\begin{code}
  634
+runRegionT :: MonadCatchIO pr => RegionT pr a -> pr a
  635
+runRegionT r = bracket   (liftIO $ (ROOD(newIORef [])))
  636
+                         (liftIO . after)
  637
+                         ((GROEN(runReaderT $ unRegionT r)))
  638
+    where
  639
+      after ioref = do  hs <- readIORef ioref
  640
+                        forM_ hs $ \(Any h) -> hClose h
  641
+\end{code}
  642
+\end{frame}
  643
+
  644
+\begin{frame}{Regions}
  645
+\framesubtitle{Call finalizer}
  646
+\begin{code}
  647
+runRegionT :: MonadCatchIO pr => RegionT pr a -> pr a
  648
+runRegionT r = bracket   (liftIO $ (ROOD(newIORef [])))
  649
+                         (liftIO . (BLAUW(after)))
  650
+                         ((GROEN(runReaderT $ unRegionT r)))
  651
+    where
  652
+      after ioref = do    hs <- readIORef ioref
  653
+                          forM_ hs $ \(Any h) -> hClose h
  654
+\end{code}
  655
+\end{frame}
  656
+
  657
+\begin{frame}{Regions}
  658
+\framesubtitle{Close all opened handles}
  659
+\begin{code}
  660
+runRegionT :: MonadCatchIO pr => RegionT pr a -> pr a
  661
+runRegionT r = bracket   (liftIO $ (ROOD(newIORef [])))
  662
+                         (liftIO . (BLAUW(after)))
  663
+                         ((GROEN(runReaderT $ unRegionT r)))
  664
+    where
  665
+      BLAUW(after ioref = do)  (BLAUW(hs <- readIORef ioref))
  666
+                               (BLAUW(forM_ hs $ \(Any h) -> hClose h))
  667
+\end{code}
  668
+\end{frame}
  669
+
  670
+\begin{frame}{Regions}
  671
+\framesubtitle{Operations}
  672
+\begin{code}
  673
+hGetLine ::  (ReadModes ioMode, MonadIO r) =>
  674
+             Handle ioMode -> r String
  675
+hGetLine h = liftIO $ E.hGetLine h
  676
+\end{code}
  677
+\begin{code}
  678
+hIsEOF ::  (ReadModes ioMode, MonadIO r) =>
  679
+           Handle ioMode -> r Bool
  680
+hIsEOF h = liftIO $ E.hIsEOF h
  681
+\end{code}
  682
+\begin{code}
  683
+hPutStrLn ::  (WriteModes ioMode, MonadIO r) =>
  684
+              Handle ioMode -> String -> r ()
  685
+hPutStrLn h s = liftIO $ E.hPutStrLn h s
  686
+\end{code}
  687
+\end{frame}
  688
+
  689
+\begin{frame}{Regions}
  690
+\framesubtitle{Operations - Just lift them}
  691
+\begin{code}
  692
+hGetLine ::  (ReadModes ioMode, (ROOD(MonadIO r))) =>
  693
+             Handle ioMode -> (ROOD(r)) String
  694
+hGetLine h = (ROOD(liftIO)) $ E.hGetLine h
  695
+\end{code}
  696
+\begin{code}
  697
+hIsEOF ::  (ReadModes ioMode, (ROOD(MonadIO r))) =>
  698
+           Handle ioMode -> (ROOD(r)) Bool
  699
+hIsEOF h = (ROOD(liftIO)) $ E.hIsEOF h
  700
+\end{code}
  701
+\begin{code}
  702
+hPutStrLn ::  (WriteModes ioMode, (ROOD(MonadIO r))) =>
  703
+              Handle ioMode -> String -> (ROOD(r)) ()
  704
+hPutStrLn h s = (ROOD(liftIO)) $ E.hPutStrLn h s
  705
+\end{code}
  706
+\end{frame}
  707
+
  708
+\begin{frame}{Regions}
  709
+\begin{code}
  710
+(GRIJS(main :: IO ()))
  711
+(GRIJS(main = runRegionT test))
  712
+
  713
+test :: MonadCatchIO pr => RegionT pr ()
  714
+test = do
  715
+  hIn <- openFile "input.txt" ReadMode
  716
+  hOut <- runRegionT $ test_internal hIn
  717
+  till (hIsEOF hIn) $
  718
+    hGetLine hIn >>= hPutStrLn hOut
  719
+
  720
+test_internal  :: (MonadCatchIO pr, ReadModes ioMode)
  721
+               => Handle ioMode -> RegionT (RegionT pr) (Handle WriteMode)
  722
+test_internal hIn = do
  723
+  hCfg <- openFile "config.txt" ReadMode
  724
+  fname <- hGetLine hCfg
  725
+  hOut <- lift $ openFile fname WriteMode
  726
+  till (liftM2 (||) (hIsEOF hCfg) (hIsEOF hIn)) $ do
  727
+    hGetLine hCfg >>= hPutStrLn hOut
  728
+    hGetLine hIn  >>= hPutStrLn hOut
  729
+  return hOut
  730
+\end{code}
  731
+\end{frame}
  732
+
  733
+\begin{frame}{Regions}
  734
+\begin{code}
  735
+(GRIJS(main :: IO ()))
  736
+(GRIJS(main = runRegionT test))
  737
+
  738
+test :: MonadCatchIO pr => RegionT pr ()
  739
+test = do
  740
+  hIn <- (GROEN(openFile "input.txt" ReadMode))
  741
+  hOut <- runRegionT $ test_internal hIn
  742
+  till (hIsEOF hIn) $
  743
+    hGetLine hIn >>= hPutStrLn hOut
  744
+
  745
+test_internal  :: (MonadCatchIO pr, ReadModes ioMode)
  746
+               => Handle ioMode -> RegionT (RegionT pr) (Handle WriteMode)
  747
+test_internal hIn = do
  748
+  hCfg <- (GROEN(openFile "config.txt" ReadMode))
  749
+  fname <- hGetLine hCfg
  750
+  hOut <- lift $ (GROEN(openFile fname WriteMode))
  751
+  till (liftM2 (||) (hIsEOF hCfg) (hIsEOF hIn)) $ do
  752
+    hGetLine hCfg >>= hPutStrLn hOut
  753
+    hGetLine hIn  >>= hPutStrLn hOut
  754
+  return hOut
  755
+\end{code}
  756
+\end{frame}
  757
+
  758
+\begin{frame}{Regions}
  759
+\begin{code}
  760
+(GRIJS(main :: IO ()))
  761
+(GRIJS(main = runRegionT test))
  762
+
  763
+test :: (BLAUW(MonadCatchIO pr => RegionT pr ()))
  764
+test = do
  765
+  hIn <- (GROEN(openFile "input.txt" ReadMode))
  766
+  hOut <- runRegionT $ test_internal hIn
  767
+  till (hIsEOF hIn) $
  768
+    hGetLine hIn >>= hPutStrLn hOut
  769
+
  770
+test_internal  :: ((BLAUW(MonadCatchIO pr)), ReadModes ioMode)
  771
+               => Handle ioMode -> (BLAUW(RegionT (RegionT pr))) (Handle WriteMode)
  772
+test_internal hIn = do
  773
+  hCfg <- (GROEN(openFile "config.txt" ReadMode))
  774
+  fname <- hGetLine hCfg
  775
+  hOut <- lift $ (GROEN(openFile fname WriteMode))
  776
+  till (liftM2 (||) (hIsEOF hCfg) (hIsEOF hIn)) $ do
  777
+    hGetLine hCfg >>= hPutStrLn hOut
  778
+    hGetLine hIn  >>= hPutStrLn hOut
  779
+  return hOut
  780
+\end{code}
  781
+\end{frame}
  782
+
  783
+\begin{frame}{Regions}
  784
+\begin{code}
  785
+(GRIJS(main :: IO ()))
  786
+(GRIJS(main = (GEEL(runRegionT)) test))
  787
+
  788
+test :: (BLAUW(MonadCatchIO pr => RegionT pr ()))
  789
+test = do
  790
+  hIn <- (GROEN(openFile "input.txt" ReadMode))
  791
+  hOut <- (GEEL(runRegionT)) $ test_internal hIn
  792
+  till (hIsEOF hIn) $
  793
+    hGetLine hIn >>= hPutStrLn hOut
  794
+
  795
+test_internal  :: ((BLAUW(MonadCatchIO pr)), ReadModes ioMode)
  796
+               => Handle ioMode -> (BLAUW(RegionT (RegionT pr))) (Handle WriteMode)
  797
+test_internal hIn = do
  798
+  hCfg <- (GROEN(openFile "config.txt" ReadMode))
  799
+  fname <- hGetLine hCfg
  800
+  hOut <- lift $ (GROEN(openFile fname WriteMode))
  801
+  till (liftM2 (||) (hIsEOF hCfg) (hIsEOF hIn)) $ do
  802
+    hGetLine hCfg >>= hPutStrLn hOut
  803
+    hGetLine hIn  >>= hPutStrLn hOut
  804
+  return hOut
  805
+\end{code}
  806
+\end{frame}
  807
+
  808
+\begin{frame}{Regions}
  809
+\begin{code}
  810
+(GRIJS(main :: IO ()))
  811
+(GRIJS(main = (GEEL(runRegionT)) test))
  812
+
  813
+test :: (BLAUW(MonadCatchIO pr => RegionT pr ()))
  814
+test = do
  815
+  hIn <- (GROEN(openFile "input.txt" ReadMode))
  816
+  hOut <- (GEEL(runRegionT)) $ test_internal hIn
  817
+  till (hIsEOF hIn) $
  818
+    hGetLine hIn >>= hPutStrLn hOut
  819
+
  820
+test_internal  :: ((BLAUW(MonadCatchIO pr)), ReadModes ioMode)
  821
+               => Handle ioMode -> (BLAUW(RegionT (RegionT pr))) (Handle WriteMode)
  822
+test_internal hIn = do
  823
+  hCfg <- (GROEN(openFile "config.txt" ReadMode))
  824
+  fname <- hGetLine hCfg
  825
+  hOut <- (ROOD(lift)) $ (GROEN(openFile fname WriteMode))
  826
+  till (liftM2 (||) (hIsEOF hCfg) (hIsEOF hIn)) $ do
  827
+    hGetLine hCfg >>= hPutStrLn hOut
  828
+    hGetLine hIn  >>= hPutStrLn hOut
  829
+  return hOut
  830
+\end{code}
  831
+\end{frame}
  832
+
  833
+
  834
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  835
+\subsection{Scoping}
  836
+
  837
+\begin{frame}{Regions}
  838
+\framesubtitle{Returning handles from regions}
  839
+%{
  840
+%format forall = "\forall"
  841
+%format . = ".\ "
  842
+\begin{code}
  843
+hack :: forall pr. MonadCatchIO pr => pr ()
  844
+hack = do  h <- runRegionT region
  845
+           (ROOD(hPutStrLn h "Don't do this at home!"))
  846
+  where
  847
+    region :: RegionT pr ((ROOD(Handle WriteMode)))
  848
+    region = do  h <- openFile "output.txt" WriteMode
  849
+                 (ROOD(return h))
  850
+\end{code}
  851
+%}
  852
+\end{frame}
  853
+
  854
+\begin{frame}{Regions}
  855
+\framesubtitle{Associate handle with region}
  856
+\begin{itemize}
  857
+\item
  858
+\begin{code}
  859
+newtype RegionalHandle ioMode (ROOD((r :: * -> *))) = RegionalHandle
  860
+    {internalHandle :: Handle ioMode}
  861
+\end{code}
  862
+\item
  863
+\begin{code}
  864
+openFile ::  MonadCatchIO pr =>
  865
+             FilePath -> IOMode ioMode ->
  866
+               RegionT (ROOD(s)) pr (ROOD((RegionalHandle ioMode (RegionT s pr))))
  867
+\end{code}
  868
+\end{itemize}
  869
+\end{frame}
  870
+
  871
+\begin{frame}{Regions}
  872
+\framesubtitle{Restrict handles to their region}
  873
+\begin{itemize}
  874
+\item
  875
+\begin{code}
  876
+newtype RegionT (ROOD(s)) pr a = ...
  877
+\end{code}
  878
+\item
  879
+%{
  880
+%format forall = "\forall"
  881
+%format . = ".\ "
  882
+\begin{code}
  883
+runRegionT :: MonadCatchIO pr => ((ROOD(forall s.)) RegionT (ROOD(s)) pr (BLAUW(a))) -> pr (BLAUW(a))
  884
+\end{code}
  885
+\item
  886
+\begin{code}
  887
+hack :: forall pr. MonadCatchIO pr => pr ()
  888
+hack = do  h <- runRegionT region
  889
+           hPutStrLn h "Don't do this at home!"
  890
+  where
  891
+    region :: RegionT (ROOD(s)) pr ((BLAUW(RegionalHandle WriteMode (RegionT (ROOD(s)) pr))))
  892
+    region = do  h <- openFile "output.txt" WriteMode
  893
+                 return h
  894
+\end{code}
  895
+%}
  896
+\item \texttt{Inferred type is less polymorphic than expected.\\
  897
+      Quantified type variable `\textcolor{rood}{s}' escapes}
  898
+\end{itemize}
  899
+\end{frame}
  900
+
  901
+\begin{frame}{Regions}
  902
+\framesubtitle{Operations}
  903
+\begin{code}
  904
+hGetLine ::  (ReadModes ioMode , MonadIO cr) =>
  905
+             (ROOD(RegionalHandle)) ioMode (ROOD(pr)) -> cr String
  906
+hGetLine h = liftIO $ E.hGetLine ((BLAUW(internalHandle)) h)
  907
+\end{code}
  908
+\begin{code}
  909
+hIsEOF ::  (ReadModes ioMode, MonadIO cr) =>
  910
+           (ROOD(RegionalHandle)) ioMode (ROOD(pr)) -> cr Bool
  911
+hIsEOF h = liftIO $ E.hIsEOF ((BLAUW(internalHandle)) h)
  912
+\end{code}
  913
+\begin{code}
  914
+hPutStrLn ::  (WriteModes ioMode, MonadIO cr) =>
  915
+              (ROOD(RegionalHandle)) ioMode (ROOD(pr)) -> String -> cr ()
  916
+hPutStrLn h s = liftIO $ E.hPutStrLn ((BLAUW(internalHandle)) h) s
  917
+\end{code}
  918
+\end{frame}
  919
+
  920
+
  921
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  922
+\subsection{ParentOf}
  923
+
  924
+\begin{frame}{Regions}
  925
+\framesubtitle{Return computation}
  926
+\begin{itemize}
  927
+\item
  928
+%{
  929
+%format forall = "\forall"
  930
+%format . = ".\ "
  931
+\begin{code}
  932
+hack :: forall pr. MonadCatchIO pr => pr ()
  933
+hack = do  h <- runRegionT region
  934
+           hPutStrLn h "Don't do this at home!"
  935
+  where
  936
+    region :: RegionT (ROOD(s)) pr ((BLAUW(RegionalHandle WriteMode (RegionT (ROOD(s)) pr))))
  937
+    region = do  h <- openFile "output.txt" WriteMode
  938
+                 return h
  939
+\end{code}
  940
+\item
  941
+\begin{code}
  942
+hack2 :: forall pr. MonadCatchIO pr => pr ()
  943
+hack2 = do  (ROOD(m)) <- runRegionT region
  944
+            (ROOD(m))
  945
+  where
  946
+    region :: RegionT s pr ((ROOD(pr ())))
  947
+    region = do  h <- openFile "output.txt" WriteMode
  948
+                 return $ (ROOD(hPutStrLn h "Don't do this at home!"))
  949
+\end{code}
  950
+%}
  951
+\end{itemize}
  952
+\end{frame}
  953
+
  954
+\begin{frame}{Regions}
  955
+\framesubtitle{Operations are too general!}
  956
+\begin{code}
  957
+hGetLine ::  (ReadModes ioMode , MonadIO cr) =>
  958
+             RegionalHandle ioMode (ROOD(pr)) -> (ROOD(cr)) String
  959
+\end{code}
  960
+\begin{code}
  961
+hIsEOF ::  (ReadModes ioMode, MonadIO cr) =>
  962
+           RegionalHandle ioMode (ROOD(pr)) -> (ROOD(cr)) Bool
  963
+\end{code}
  964
+\begin{code}
  965
+hPutStrLn ::  (WriteModes ioMode, MonadIO cr) =>
  966
+              RegionalHandle ioMode (ROOD(pr)) -> String -> (ROOD(cr)) ()
  967
+\end{code}
  968
+\end{frame}
  969
+
  970
+\begin{frame}{Regions}
  971
+\framesubtitle{Restrict}
  972
+\begin{code}
  973
+hGetLine ::  (ReadModes ioMode, MonadIO cr, (ROOD(pr `ParentOf` cr))) =>
  974
+             RegionalHandle ioMode (ROOD(pr)) -> (ROOD(cr)) String
  975
+\end{code}
  976
+\begin{code}
  977
+hIsEOF ::  (ReadModes ioMode, MonadIO cr, (ROOD(pr `ParentOf` cr))) =>
  978
+           RegionalHandle ioMode (ROOD(pr)) -> (ROOD(cr)) Bool
  979
+\end{code}
  980
+\begin{code}
  981
+hPutStrLn ::  (WriteModes ioMode, MonadIO cr, (ROOD(pr `ParentOf` cr))) =>
  982
+              RegionalHandle ioMode (ROOD(pr)) -> String -> (ROOD(cr)) ()
  983
+\end{code}
  984
+\end{frame}
  985
+
  986
+
  987
+\begin{frame}{Regions}
  988
+\framesubtitle{|ParentOf|}
  989
+\begin{itemize}
  990
+\item
  991
+\begin{code}
  992
+class (Monad pr, Monad cr) => pr `ParentOf` cr
  993
+\end{code}
  994
+\item
  995
+\begin{code}
  996
+instance Monad r => r `ParentOf` r
  997
+\end{code}
  998
+\item
  999
+\begin{code}
  1000
+instance  ( Monad cr
  1001
+          , cr (ROOD(`TypeCast2`)) RegionT s pcr
  1002
+          , pr `ParentOf` pcr
  1003
+          )
  1004
+          => pr `ParentOf` cr
  1005
+\end{code}
  1006
+\item
  1007
+\begin{code}
  1008
+(GROEN(RegionT ps ppr)) `ParentOf`  RegionT cs
  1009
+                                      (RegionT pcs
  1010
+                                        (RegionT ppcs
  1011
+                                          ...
  1012
+                                            ((GROEN(RegionT ps ppr)))))
  1013
+\end{code}
  1014
+\end{itemize}
  1015
+\end{frame}
  1016
+
  1017
+\begin{frame}{Regions}
  1018
+\framesubtitle{Type casting}
  1019
+\begin{code}
  1020
+instance  ( Monad cr
  1021
+          , cr (ROOD(`TypeCast2`)) RegionT s pcr
  1022
+          , pr `ParentOf` pcr
  1023
+          )
  1024
+          => pr `ParentOf` cr
  1025
+\end{code}
  1026
+\begin{code}
  1027
+class TypeCast2       (a :: * -> *) (b :: * -> *)  |     a -> b
  1028
+                                                   ,     b -> a
  1029
+class TypeCast2'   t  (a :: * -> *) (b :: * -> *)  |  t  a -> b
  1030
+                                                   ,  t  b -> a
  1031
+class TypeCast2''  t  (a :: * -> *) (b :: * -> *)  |  t  a -> b
  1032
+                                                   ,  t  b -> a
  1033
+
  1034
+instance TypeCast2'    ()  a b => TypeCast2     a b
  1035
+instance TypeCast2''   t   a b => TypeCast2' t  a b
  1036
+instance TypeCast2''   ()  a a
  1037
+\end{code}
  1038
+\end{frame}
  1039
+
  1040
+\begin{frame}{Regions}
  1041
+\framesubtitle{|hack2| now fails}
  1042
+\begin{itemize}
  1043
+\item
  1044
+%{
  1045
+%format forall = "\forall"
  1046
+%format . = ".\ "
  1047
+\begin{code}
  1048
+hack2 :: forall sp pr. MonadCatchIO pr => RegionT sp pr ()
  1049
+hack2 = do  m <- runRegionT region
  1050
+            m
  1051
+  where
  1052
+    region :: forall (ROOD(s)). RegionT (ROOD(s)) (RegionT sp pr) (RegionT sp pr ())
  1053
+    region = do  h <- openFile "output.txt" WriteMode
  1054
+                 return $ hPutStrLn h "Don't do this at home!"
  1055
+\end{code}
  1056
+%}
  1057
+\item
  1058
+\texttt{Inferred type is less polymorphic than expected\\
  1059
+        Quantified type variable `\textcolor{rood}{s}' is mentioned in the environment:...}
  1060
+ \item |RegionT (ROOD(s)) (RegionT sp pr) `ParentOf` (RegionT sp pr)|
  1061
+\end{itemize}
  1062
+\end{frame}
  1063
+
  1064
+
  1065
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1066
+\subsection{Leaking Handles via IOError}
  1067
+
  1068
+\begin{frame}{Regions}
  1069
+\framesubtitle{Leaking handles via |IOErrors|}
  1070
+\begin{itemize}
  1071
+\item
  1072
+\begin{code}
  1073
+throwsException = runRegionT $ do
  1074
+                    h <- openFile "foo.txt" ReadMode
  1075
+                    hSeek h AbsoluteSeek ((ROOD(-1)))
  1076
+\end{code}
  1077
+\item
  1078
+\begin{code}
  1079
+leak =  throwsException `catch` \(e :: IOError) ->
  1080
+          case (ROOD(ioeGetHandle e)) of
  1081
+            Nothing  -> return ()
  1082
+            Just h   -> System.IO.hGetLine h
  1083
+\end{code}
  1084
+\item
  1085
+\begin{code}
  1086
+ioeGetHandle :: IOError -> Maybe (ROOD(Handle))
  1087
+\end{code}
  1088
+\end{itemize}
  1089
+\end{frame}
  1090
+
  1091
+\begin{frame}{Regions}
  1092
+\framesubtitle{Remove handles from |IOErrors|}
  1093
+\begin{itemize}
  1094
+\item
  1095
+\begin{code}
  1096
+sanitize :: IO a -> IO a
  1097
+sanitize = modifyIOError $ \e -> e { ioe_handle = Nothing }
  1098
+\end{code}
  1099
+\item
  1100
+\begin{code}
  1101
+hIsEOF     h       = liftIO $ (ROOD(sanitize)) $ E.hIsEOF     (internalHandle h)
  1102
+hPutStrLn  h s     = liftIO $ (ROOD(sanitize)) $ E.hPutStrLn  (internalHandle h) s
  1103
+hSeek      h sm n  = liftIO $ (ROOD(sanitize)) $ E.hSeek      (internalHandle h) sm n
  1104
+\end{code}
  1105
+\end{itemize}
  1106
+\end{frame}
  1107
+
  1108
+
  1109
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1110
+\subsection{Extension: handle duplication}
  1111
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1112
+
  1113
+\begin{frame}{Regions}
  1114
+\framesubtitle{Extension: dynamically extending the lifetime of handles}
  1115
+\begin{code}
  1116
+test2 = runRegionT $ do
  1117
+  h <- runRegionT $ test5_internal "conf2.txt"
  1118
+  l <- hGetLine h
  1119
+
  1120
+test2_internal conf_fname = do