From e85bf806c2d0e7d6d24e1691b87edf3c21ee339a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C5=A0ebek?= <44544735+xsebek@users.noreply.github.com> Date: Thu, 23 Jun 2022 13:25:23 +0200 Subject: [PATCH] Retire raise command (#407) - remove `raise` command - rename `error` to `fail` - should suggest both that it is: - recoverable - can be caught with `try` - pure - `fail: string -> a` - so it can be used outside `cmd` --- The problem with `raise` was that it was merely a specialized version of `error` (which was not limited to `cmd a`): ``` let raised: string -> cmd a = error in raised "ha" ``` I noticed this while making the list for #26. On a meta-level, it also conflicts with the `raise` Haskell function we use to throw an error when a command (like `build`) fails. --- example/list.sw | 2 +- src/Swarm/Game/Exception.hs | 2 +- src/Swarm/Game/Step.hs | 5 +---- src/Swarm/Language/Capability.hs | 5 ++--- src/Swarm/Language/Parse.hs | 2 +- src/Swarm/Language/Syntax.hs | 9 +++------ src/Swarm/Language/Typecheck.hs | 3 +-- test/unit/Main.hs | 12 ++++++------ 8 files changed, 16 insertions(+), 24 deletions(-) diff --git a/example/list.sw b/example/list.sw index 597982eec..2ebb59c24 100644 --- a/example/list.sw +++ b/example/list.sw @@ -203,7 +203,7 @@ end // TODO: show values when #248 is implemented def assert = \b.\m. - if b {} {log "FAIL:"; raise m} + if b {} {log "FAIL:"; fail m} end def testLIST = diff --git a/src/Swarm/Game/Exception.hs b/src/Swarm/Game/Exception.hs index 35fae4cd5..e6172a97d 100644 --- a/src/Swarm/Game/Exception.hs +++ b/src/Swarm/Game/Exception.hs @@ -71,7 +71,7 @@ data Exn -- command could not move, or a 'Grab' command found nothing to -- grab, /etc./). CmdFailed Const Text - | -- | The user program explicitly called 'Raise', 'Undefined', or 'ErrorStr'. + | -- | The user program explicitly called 'Undefined' or 'Fail'. User Text deriving (Eq, Show) diff --git a/src/Swarm/Game/Step.hs b/src/Swarm/Game/Step.hs index 16be4b510..992bdfc78 100644 --- a/src/Swarm/Game/Step.hs +++ b/src/Swarm/Game/Step.hs @@ -1171,11 +1171,8 @@ execConst c vs s k = do Try -> case vs of [c1, c2] -> return $ Out c1 s (FApp (VCApp Force []) : FExec : FTry c2 : k) _ -> badConst - Raise -> case vs of - [VString msg] -> return $ Up (User msg) s k - _ -> badConst Undefined -> return $ Up (User "undefined") s k - ErrorStr -> case vs of + Fail -> case vs of [VString msg] -> return $ Up (User msg) s k _ -> badConst Reprogram -> case vs of diff --git a/src/Swarm/Language/Capability.hs b/src/Swarm/Language/Capability.hs index f07bb17ee..337bbc935 100644 --- a/src/Swarm/Language/Capability.hs +++ b/src/Swarm/Language/Capability.hs @@ -263,7 +263,7 @@ constCaps = Base -> [] Setname -> [] Undefined -> [] - ErrorStr -> [] + Fail -> [] -- Some straightforward ones. Log -> [CLog] Selfdestruct -> [CSelfdestruct] @@ -330,6 +330,5 @@ constCaps = Case -> [] Fst -> [] -- XXX should require cap for pairs Snd -> [] - Try -> [] -- XXX these definitely need to require - Raise -> [] -- something. + Try -> [] -- XXX these definitely need to require something. Knows -> [] diff --git a/src/Swarm/Language/Parse.hs b/src/Swarm/Language/Parse.hs index 42f8f8cba..d61ed63ef 100644 --- a/src/Swarm/Language/Parse.hs +++ b/src/Swarm/Language/Parse.hs @@ -213,7 +213,7 @@ parseDirection = asum (map alternative allDirs) "direction constant" where alternative d = d <$ (reserved . dirSyntax . dirInfo) d --- | Parse Const as reserved words (e.g. @Raise <$ reserved "raise"@) +-- | Parse Const as reserved words (e.g. @Fail <$ reserved "fail"@) parseConst :: Parser Const parseConst = asum (map alternative consts) "built-in user function" where diff --git a/src/Swarm/Language/Syntax.hs b/src/Swarm/Language/Syntax.hs index adf1e259c..a4f3cdc4a 100644 --- a/src/Swarm/Language/Syntax.hs +++ b/src/Swarm/Language/Syntax.hs @@ -294,12 +294,10 @@ data Const Return | -- | Try/catch block Try - | -- | Raise an exception - Raise | -- | Undefined Undefined - | -- | Error - ErrorStr + | -- | User error + Fail | -- Arithmetic unary operators -- | Logical negation. @@ -467,9 +465,8 @@ constInfo c = case c of Run -> commandLow 1 Return -> commandLow 1 Try -> commandLow 2 - Raise -> commandLow 1 Undefined -> functionLow 0 - ErrorStr -> function "error" 1 + Fail -> functionLow 1 If -> functionLow 3 Inl -> functionLow 1 Inr -> functionLow 1 diff --git a/src/Swarm/Language/Typecheck.hs b/src/Swarm/Language/Typecheck.hs index db2b9562c..bb45de68f 100644 --- a/src/Swarm/Language/Typecheck.hs +++ b/src/Swarm/Language/Typecheck.hs @@ -476,9 +476,8 @@ inferConst c = toU $ case c of Force -> [tyQ| {a} -> a |] Return -> [tyQ| a -> cmd a |] Try -> [tyQ| {cmd a} -> {cmd a} -> cmd a |] - Raise -> [tyQ| string -> cmd a |] Undefined -> [tyQ| a |] - ErrorStr -> [tyQ| string -> a |] + Fail -> [tyQ| string -> a |] Not -> [tyQ| bool -> bool |] Neg -> [tyQ| int -> int |] Eq -> cmpBinT diff --git a/test/unit/Main.hs b/test/unit/Main.hs index 4449a279c..0e322b955 100644 --- a/test/unit/Main.hs +++ b/test/unit/Main.hs @@ -409,8 +409,8 @@ eval g = , testGroup "exceptions" [ testCase - "raise" - ("raise \"foo\"" `throwsError` ("foo" `T.isInfixOf`)) + "fail" + ("fail \"foo\"" `throwsError` ("foo" `T.isInfixOf`)) , testCase "try / no exception 1" ("try {return 1} {return 2}" `evaluatesTo` VInt 1) @@ -418,11 +418,11 @@ eval g = "try / no exception 2" ("try {return 1} {let x = x in x}" `evaluatesTo` VInt 1) , testCase - "try / raise" - ("try {raise \"foo\"} {return 3}" `evaluatesTo` VInt 3) + "try / fail" + ("try {fail \"foo\"} {return 3}" `evaluatesTo` VInt 3) , testCase - "try / raise / raise" - ("try {raise \"foo\"} {raise \"bar\"}" `throwsError` ("bar" `T.isInfixOf`)) + "try / fail / fail" + ("try {fail \"foo\"} {fail \"bar\"}" `throwsError` ("bar" `T.isInfixOf`)) , testCase "try / div by 0" ("try {return (1/0)} {return 3}" `evaluatesTo` VInt 3)