Skip to content

Commit 5206c05

Browse files
committed
Add test case using exceptions and System.Environment functions
1 parent 06ce5f2 commit 5206c05

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-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 #-}

test/golden/AllTests.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,5 @@ import Issue306
101101
import RelevantDotPattern1
102102
import RelevantDotPattern2
103103
import RelevantDotPattern3
104+
import ParseOrValidate
104105

test/golden/ParseOrValidate.hs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module ParseOrValidate where
2+
3+
import Control.Exception (throwIO)
4+
import System.Environment (getEnv)
5+
6+
split :: Char -> String -> [String]
7+
split c s
8+
= case rest of
9+
[] -> [chunk]
10+
_ : rest -> chunk : split c rest
11+
where
12+
broken :: ([Char], [Char])
13+
broken = break (== c) s
14+
chunk :: [Char]
15+
chunk = fst broken
16+
rest :: [Char]
17+
rest = snd broken
18+
19+
validateNonEmpty :: [a] -> IO ()
20+
validateNonEmpty [] = throwIO (userError "list cannot be empty")
21+
validateNonEmpty (x : xs) = return ()
22+
23+
getConfigurationDirectories :: IO [FilePath]
24+
getConfigurationDirectories
25+
= do configDirsString <- getEnv "CONFIG_DIRS"
26+
validateNonEmpty (split ',' configDirsString)
27+
pure (split ',' configDirsString)
28+
29+
initializeCache :: String -> IO ()
30+
initializeCache = error "postulate: String -> IO ()"
31+
32+
main :: IO ()
33+
main
34+
= do configDirs <- getConfigurationDirectories
35+
initializeCache (head configDirs)
36+

0 commit comments

Comments
 (0)