Skip to content

Commit 3de8ef9

Browse files
committed
Add test case using exceptions and System.Environment functions
1 parent 57c6cc6 commit 3de8ef9

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

test/AllTests.agda

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ import Issue306
106106
import RelevantDotPattern1
107107
import RelevantDotPattern2
108108
import RelevantDotPattern3
109+
import ParseOrValidate
109110

110111
{-# FOREIGN AGDA2HS
111112
import Issue14
@@ -209,4 +210,5 @@ import Issue306
209210
import RelevantDotPattern1
210211
import RelevantDotPattern2
211212
import RelevantDotPattern3
213+
import ParseOrValidate
212214
#-}

test/ParseOrValidate.agda

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{-# OPTIONS --erasure #-}
2+
3+
open import Haskell.Prelude
4+
open import Haskell.Control.Exception
5+
open import Haskell.Control.Monad
6+
open import Haskell.Extra.Dec
7+
open import Haskell.Extra.Erase
8+
open import Haskell.Extra.Refinement
9+
open import Haskell.System.Environment
10+
11+
it : {{a}} a
12+
it {{x}} = x
13+
14+
{-# COMPILE AGDA2HS it inline #-}
15+
16+
{-# TERMINATING #-}
17+
split : Char String List String
18+
split c s = case rest of λ where
19+
[] chunk ∷ []
20+
(_ ∷ rest) chunk ∷ split c rest
21+
where
22+
broken = break (_== c) s
23+
chunk = fst broken
24+
rest = snd broken
25+
26+
{-# COMPILE AGDA2HS split #-}
27+
28+
-- The below example is taken from the classic blog post by Alexis King
29+
-- "Parse, don't validate". Here I ignore her advice but instead implement
30+
-- a validation function that returns evidence of the property it checked.
31+
32+
validateNonEmpty : @0 {{MayThrow IOException}} (xs : List a) IO (Erase (NonEmpty xs))
33+
validateNonEmpty [] = throwIO (userError "list cannot be empty")
34+
validateNonEmpty (x ∷ xs) = return it
35+
36+
{-# COMPILE AGDA2HS validateNonEmpty #-}
37+
38+
getConfigurationDirectories : @0 {{MayThrow IOException}} IO (∃ (List FilePath) NonEmpty)
39+
getConfigurationDirectories = do
40+
configDirsString <- getEnv "CONFIG_DIRS"
41+
let configDirsList = split ',' configDirsString
42+
validateNonEmpty configDirsList
43+
pure (configDirsList ⟨⟩)
44+
45+
{-# COMPILE AGDA2HS getConfigurationDirectories #-}
46+
47+
-- TODO: implement this function?
48+
postulate
49+
initializeCache : String IO ⊤
50+
{-# COMPILE AGDA2HS initializeCache #-}
51+
52+
main : @0 {{MayThrow IOException}} IO ⊤
53+
main = do
54+
configDirs ⟨ i ⟩ <- getConfigurationDirectories
55+
initializeCache (head configDirs {{i}})
56+
57+
{-# COMPILE AGDA2HS main #-}

0 commit comments

Comments
 (0)