Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for assert function from Control.Exception #388

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 : _
17 changes: 17 additions & 0 deletions lib/Haskell/Extra/Dec.agda
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,20 @@ mapDec f g (False ⟨ h ⟩) = False ⟨ h ∘ g ⟩
ifDec : Dec a → (@0 {{a}} → b) → (@0 {{a → ⊥}} → b) → b
ifDec (b ⟨ p ⟩) x y = if b then (λ where {{refl}} → x {{p}}) else (λ where {{refl}} → y {{p}})
{-# COMPILE AGDA2HS ifDec inline #-}

instance
iDecIsTrue : {b : Bool} → Dec (IsTrue b)
iDecIsTrue {False} = False ⟨ (λ ()) ⟩
iDecIsTrue {True} = True ⟨ IsTrue.itsTrue ⟩
{-# COMPILE AGDA2HS iDecIsTrue transparent #-}

iDecIsFalse : {b : Bool} → Dec (IsFalse b)
iDecIsFalse {b} = mapDec isTrueNotIsFalse isFalseIsTrueNot (iDecIsTrue {not b})
where
@0 isTrueNotIsFalse : {b : Bool} → IsTrue (not b) → IsFalse b
isTrueNotIsFalse {False} IsTrue.itsTrue = IsFalse.itsFalse

@0 isFalseIsTrueNot : {b : Bool} → IsFalse b → IsTrue (not b)
isFalseIsTrueNot {False} IsFalse.itsFalse = IsTrue.itsTrue
{-# COMPILE AGDA2HS iDecIsFalse inline #-}

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 #-}
7 changes: 0 additions & 7 deletions test/NonClassInstance.agda
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ open import Haskell.Prelude
open import Haskell.Extra.Dec
open import Haskell.Extra.Refinement

instance
iDecIsTrue : {b : Bool} → Dec (IsTrue b)
iDecIsTrue {False} = False ⟨ (λ ()) ⟩
iDecIsTrue {True} = True ⟨ IsTrue.itsTrue ⟩

{-# COMPILE AGDA2HS iDecIsTrue #-}

foo : (b : Bool) → {{Dec (IsTrue b)}} → Bool
foo _ {{b ⟨ _ ⟩}} = not b

Expand Down
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)

6 changes: 1 addition & 5 deletions test/golden/NonClassInstance.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
module NonClassInstance where

iDecIsTrue :: Bool -> Bool
iDecIsTrue False = False
iDecIsTrue True = True

foo :: Bool -> Bool -> Bool
foo _ b = not b

bar :: Bool -> Bool
bar b = foo b (iDecIsTrue b)
bar b = foo b b

Loading