Skip to content

Commit

Permalink
Retire raise command (#407)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
xsebek authored Jun 23, 2022
1 parent 8d07b66 commit e85bf80
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 24 deletions.
2 changes: 1 addition & 1 deletion example/list.sw
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion src/Swarm/Game/Exception.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 1 addition & 4 deletions src/Swarm/Game/Step.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/Swarm/Language/Capability.hs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ constCaps =
Base -> []
Setname -> []
Undefined -> []
ErrorStr -> []
Fail -> []
-- Some straightforward ones.
Log -> [CLog]
Selfdestruct -> [CSelfdestruct]
Expand Down Expand Up @@ -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 -> []
2 changes: 1 addition & 1 deletion src/Swarm/Language/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 3 additions & 6 deletions src/Swarm/Language/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/Swarm/Language/Typecheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions test/unit/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -409,20 +409,20 @@ 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)
, testCase
"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)
Expand Down

0 comments on commit e85bf80

Please sign in to comment.