|
| 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