Skip to content

Commit

Permalink
Add assert function to Haskell.Control.Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
jespercockx committed Jan 9, 2025
1 parent 1bf9d3a commit 7cae101
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/Haskell/Control/Exception.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Haskell.Control.Exception where

open import Haskell.Prim

open import Haskell.Extra.Dec
open import Haskell.Extra.Refinement

assert : (@0 b : Set ℓ) {{Dec b}} (@0 {{b}} a) a
assert _ {{True ⟨ p ⟩}} x = x {{p}}
assert _ {{False ⟨ _ ⟩}} x = oops
where postulate oops : _
1 change: 1 addition & 0 deletions src/Agda2Hs/Compile/Name.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ defaultSpecialRules :: SpecialRules
defaultSpecialRules = Map.fromList
[ "Agda.Builtin.Nat.Nat" `to` "Natural" `importing` Just "Numeric.Natural"
, "Haskell.Control.Monad.guard" `to` "guard" `importing` Just "Control.Monad"
, "Haskell.Control.Exception.assert" `to` "assert" `importing` Just "Control.Exception"
, "Haskell.Prelude.coerce" `to` "unsafeCoerce" `importing` Just "Unsafe.Coerce"
, "Agda.Builtin.Int.Int" `to` "Integer" `importing` Nothing
, "Agda.Builtin.Word.Word64" `to` "Word" `importing` Nothing
Expand Down
2 changes: 2 additions & 0 deletions test/AllTests.agda
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import ProjectionLike
import FunCon
import Issue308
import Issue324
import Assert

{-# FOREIGN AGDA2HS
import Issue14
Expand Down Expand Up @@ -183,4 +184,5 @@ import ProjectionLike
import FunCon
import Issue308
import Issue324
import Assert
#-}
10 changes: 10 additions & 0 deletions test/Assert.agda
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

open import Haskell.Prelude
open import Haskell.Control.Exception
open import Haskell.Law.Ord
open import Haskell.Extra.Dec

subtractChecked : Nat Nat Nat
subtractChecked x y = assert (IsFalse (x < y)) (x - y)

{-# COMPILE AGDA2HS subtractChecked #-}
1 change: 1 addition & 0 deletions test/golden/AllTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ import ProjectionLike
import FunCon
import Issue308
import Issue324
import Assert

8 changes: 8 additions & 0 deletions test/golden/Assert.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Assert where

import Control.Exception (assert)
import Numeric.Natural (Natural)

subtractChecked :: Natural -> Natural -> Natural
subtractChecked x y = assert (not (x < y)) (x - y)

0 comments on commit 7cae101

Please sign in to comment.