Anonymous avatar Anonymous committed b1d440f

io errors (exn * place) replaced by simple exn

Comments (0)

Files changed (4)

 
 (* OCaml Pervasives IO *)
 
-value res_of_exn ep : res 'a = `Error ep
+value res_of_exn e : res 'a = `Error e
 ;
 
 module Direct_IO
 
     value return : 'a -> m 'a;
     value bind : ('a -> m 'b) -> m 'a -> m 'b;
-    value catch : (unit -> m 'a) -> ((exn * place) -> m 'a) -> m 'a;
+    value catch : (unit -> m 'a) -> (exn -> m 'a) -> m 'a;
 
-    value error : (exn * place) -> m 'a;
+    value error : exn -> m 'a;
 
     type output_channel;
     value stdout : output_channel;
 *)
     value wrap1 place f = fun a ->
       try `Ok (f a)
-      with [ e -> res_of_exn (e, place) ]
+      with [ e -> res_of_exn (EIO (e, place)) ]
     ;
     value wrap2 place f = fun a b ->
       try `Ok (f a b)
-      with [ e -> res_of_exn (e, place) ]
+      with [ e -> res_of_exn (EIO (e, place)) ]
     ;
     value wrap4 place f = fun a b c d ->
       try `Ok (f a b c d)
-      with [ e -> res_of_exn (e, place) ]
+      with [ e -> res_of_exn (EIO (e, place)) ]
     ;
 
     value read_into = wrap4 "read_into" Pervasives.input;
 (* +
    In OCaml, Iteratees' [err_msg] is represented by simple exception.
    [err_msg] can be Iteratees' internal exception, user-defined Iteratees'
-   exception, or an IO exception, wrapped into [EIO] exception.
+   exception, or an IO exception with place, wrapped into [Types.EIO]
+   exception.
 
-   IO errors (see MonadIO signature) carry the tuple of exception and
+   IO exceptions (see MonadIO signature) carry the tuple of exception and
    the place where exception was raised (similar to
    [Unix.Unix_error (error, place, argument)] exception; I find it useful).
 
    it depends on [IO.error] to return the value of [IO.m 'a] type.
 *)
 
-value ierr_of_merr (ep : (exn * place)) : err_msg =
-  match ep with
-  [ (Iteratees_err_msg e, _) -> e
-  | ep -> EIO ep
+value ierr_of_merr (e : exn) : err_msg =
+  match e with
+  [ Iteratees_err_msg e | e -> e
   ]
 ;
 
 value merr_of_ierr (e : err_msg) : IO.m 'a =
   IO.error &
   match e with
-  [ EIO ep -> ep
-  | e -> (Iteratees_err_msg e, "")
+  [ EIO _ -> e
+  | e -> Iteratees_err_msg e
   ]
 ;
 
       | IE_cont opt_err _inner_k2 ->
           match opt_err with
           [ Some e -> merr_of_ierr e
-          | None -> IO.error (Divergent_iteratee "joinI", "")
+          | None -> IO.error (Divergent_iteratee "joinI")
           ]
       ]
   ]
   ]
 in
           IO.error & match opt_exn with
-          [ None -> (Divergent_iteratee "run", "run")
-          | Some e -> (e, "")
+          [ None -> (Divergent_iteratee "run")
+          | Some e -> e
           ]
       ]
   ]
 value (mres : IO.m 'a -> IO.m (res 'a)) m =
   IO.catch
     (fun () -> m >>% fun r -> IO.return & `Ok r)
-    (fun ep -> IO.return & `Error ep)
+    (fun e -> IO.return & `Error e)
 ;
 
 value (_munres : res 'a -> IO.m 'a) r =
   let rec loop k =
     mres (IO.read_into inch buf_str 0 buffer_size) >>% fun read_res ->
     match read_res with
-    [ `Error ep ->
-        k (EOF (some & ierr_of_merr ep)) >>% IO.return % fst
+    [ `Error e ->
+        k (EOF (some & ierr_of_merr e)) >>% IO.return % fst
     | `Ok have_read ->
         mprintf "Read buffer, size %i\n" have_read >>% fun () ->
         let () = assert (have_read >= 0) in
 ;
 
 
-value string_of_ep (e, p) =
+value string_of_e e =
+  let (e, p) =
+    match e with
+    [ Iteratees_err_msg e -> (e, "")
+    | EIO (e, p) -> (e, p)
+    | e -> (e, "")
+    ]
+  in
   sprintf "error: %s%s\n"
-    (match e with [ Iteratees_err_msg e
-                  | e -> Printexc.to_string e ])
+    (Printexc.to_string e)
     (if p = "" then "" else sprintf " (at %S)" p)
 ;
 
+
 value print_res f r =
   match r with
   [ `Ok v -> printf "res: ok: %s\n" (f v)
-  | `Error ep -> printf "%s\n" & string_of_ep ep
+  | `Error e -> printf "%s\n" & string_of_e e
   ]
 ;
 
     enum read_lines_and_one_more_line
   in
   match res with
-  [ `Error ep -> failwith & string_of_ep ep
+  [ `Error e -> failwith & string_of_e e
   | `Ok (lines, rest) ->
        ( assert (lines =
            [ "header1: v1"; "header2: v2"; "header3: v3"; "header4: v4"
       mprintf "Problem: %s\n" (Printexc.to_string e) >>% fun () ->
       mprintf "Incomplete headers.\n" >>% fun () ->
       IO.return ()
-  | `Error ep ->
-      mprintf "enumerator's error: %s\n" (string_of_ep ep) >>% fun () ->
-      IO.error ep
+  | `Error e ->
+      mprintf "enumerator's error: %s\n" (string_of_e e) >>% fun () ->
+      IO.error e
   ]
 ;
 
 value print_unit_res r =
   match r with
   [ `Ok () -> printf "ok.\n"
-  | `Error ep -> printf "%s\n" & string_of_ep ep
+  | `Error e -> printf "%s\n" & string_of_e e
   ]
 ;
 
 
 
 (* +
+   IO exception, carrying the real IO exception and the place
+   (usually function name) where it was raised.
+*)
+
+exception EIO of (exn * place);
+
+
+(* +
    This is a signature for IO monad.  These functions and types are used
    by Iteratees functor.  It's possible that your implementation of IO
    have much more functions than MonadIO, so you should not restrict
     value return : 'a -> m 'a;
     value bind : ('a -> m 'b) -> m 'a -> m 'b;
 
-    value error : (exn * place) -> m 'a;
-    value catch : (unit -> m 'a) -> ((exn * place) -> m 'a) -> m 'a;
+    value error : exn -> m 'a;
+    value catch : (unit -> m 'a) -> (exn -> m 'a) -> m 'a;
 
     type output_channel;
     value stdout : output_channel;
    See function [mres] in functor.
 *)
 
-type res +'a = [= `Ok of 'a | `Error of (exn * place) ]
+type res +'a = [= `Ok of 'a | `Error of exn ]
 ;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.