on master help sign in
Merge pull request #3552 from jfdm/improve-sys-exit

Improve `System.Exit` Fixes #3488.

commit57a3b936782145576052d7fcce576cb88a36a72b authorJan de Muijnck-Hughes <jfdm@users.noreply.github.com> dateTue 6 Dec 2016, 15:38 (10 years ago) parent6fc36274ddda1e
4 files changed+45−8
CHANGELOG.md+8 libs/base/Debug/Error.idr+1−2 libs/base/System.idr+35−4 libs/effects/Effect/Exception.idr+1−2
CHANGELOG.md+8
@@ -1,3 +1,11 @@
1+# New in 0.999
2+
3+## Library Updates
4+
5++ Terminating programs has been improved with more appropriate
6+ functions (`exitWith`, `exitFailure`, and `exitSuccess`) and a data
7+ structure (`ExitCode`) to capture a program's return code.
8+
19# New in 0.99:
210
311## Language updates
libs/base/Debug/Error.idr+1−2
@@ -12,9 +12,8 @@ error : {default (%runElab sourceLocation) loc : SourceLocation} ->
1212 (message : String) ->
1313 a
1414error {loc = FileLoc filename (line, col) _} message =
15− believe_me . unsafePerformIO $
15+ unsafePerformIO $
1616 do let place = filename ++ " line " ++ show line ++ " column " ++ show col
1717 let message' = place ++ ": " ++ message
1818 putStrLn message'
1919 exit 1
20−
libs/base/System.idr+35−4
@@ -1,5 +1,7 @@
11module System
22
3+import Data.So
4+
35%include C "unistd.h"
46%default partial
57%access public export
@@ -52,11 +54,41 @@ getEnvironment = getAllPairs 0 []
5254 then pure $ reverse $ map splitEq acc
5355 else getAllPairs (n + 1) (envPair :: acc)
5456
57+||| Programs can either terminate successfully, or end in a caught
58+||| failure.
59+data ExitCode : Type where
60+ ||| Terminate successfully.
61+ ExitSuccess : ExitCode
62+ ||| Program terminated for some prescribed reason.
63+ |||
64+ ||| @errNo A non-zero numerical value indicating failure.
65+ ||| @prf Proof that the int value is non-zero.
66+ ExitFailure : (errNo : Int)
67+ -> {auto prf : So (not $ errNo == 0)}
68+ -> ExitCode
69+
5570||| Quit with a particular exit code
56−exit : Int -> IO ()
57−exit code = foreign FFI_C "exit" (Int -> IO ()) code
71+exit : Int -> IO a
72+exit code = believe_me $ foreign FFI_C "exit" (Int -> IO ()) code
73+
74+||| Terminate the program with an `ExitCode`. This code indicates the
75+||| success of the program's execution, and returns the success code
76+||| to the program's caller.
77+|||
78+||| @code The `ExitCode` for program.
79+exitWith : (code : ExitCode) -> IO a
80+exitWith ExitSuccess = exit 0
81+exitWith (ExitFailure errNo) = exit errNo
5882
59−||| Get the numbers of seconds since 1st January 1970, 00:00 UTC
83+||| Exit the program indicating failure.
84+exitFailure : IO a
85+exitFailure = exitWith (ExitFailure 1)
86+
87+||| Exit the program after a successful run.
88+exitSuccess : IO a
89+exitSuccess = exitWith ExitSuccess
90+
91+||| Get the numbers of seconds since 1st January 1970, 00:00 UTC
6092time : IO Integer
6193time = do MkRaw t <- foreign FFI_C "idris_time" (IO (Raw Integer))
6294 pure t
@@ -66,4 +98,3 @@ usleep i = foreign FFI_C "usleep" (Int -> IO ()) i
6698
6799system : String -> IO Int
68100system cmd = foreign FFI_C "system" (String -> IO Int) cmd
69−
libs/effects/Effect/Exception.idr+1−2
@@ -17,7 +17,7 @@ implementation Handler (Exception a) List where
1717
1818implementation Show a => Handler (Exception a) IO where
1919 handle _ (Raise e) k = do printLn e
20− believe_me (exit 1)
20+ (exit 1)
2121
2222implementation Handler (Exception a) (IOExcept a) where
2323 handle _ (Raise e) k = ioe_fail e
@@ -30,4 +30,3 @@ EXCEPTION t = MkEff () (Exception t)
3030
3131raise : a -> Eff b [EXCEPTION a]
3232raise err = call $ Raise err
33−