Skip to content

Commit e175f89

Browse files
authored
[ new ] bindings for System.Environment (#1347)
1 parent dab47b9 commit e175f89

File tree

6 files changed

+102
-13
lines changed

6 files changed

+102
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ New modules
123123
Algebra.Properties.CommutativeSemigroup.Divisibility
124124
```
125125

126+
* Added bindings for Haskell's `System.Environment`
127+
126128
Other major changes
127129
-------------------
128130

GenerateEverything.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ unsafeModules = map modToFile
4141
, "IO"
4242
, "IO.Primitive"
4343
, "Relation.Binary.PropositionalEquality.TrustMe"
44+
, "System.Environment"
45+
, "System.Environment.Primitive"
4446
, "Text.Pretty.Core"
4547
, "Text.Pretty"
4648
]

src/Foreign/Haskell/Coerce.agda

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import IO.Primitive as STD
3939
import Data.List.Base as STD
4040
import Data.Maybe.Base as STD
4141
import Data.Product as STD
42-
import Data.Sum as STD
42+
import Data.Sum.Base as STD
4343

4444
import Foreign.Haskell.Maybe as FFI
4545
import Foreign.Haskell.Pair as FFI

src/IO.agda

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ module _ {a b} {A : Set a} {B : Set b} where
6969
mapM′ : (A IO B) Colist A IO ⊤
7070
mapM′ f = sequence′ ∘ map f
7171

72+
ignore : {a} {A : Set a} IO A IO ⊤
73+
ignore io = ♯ io >> ♯ return _
74+
7275
------------------------------------------------------------------------
7376
-- Simple lazy IO
7477

@@ -94,33 +97,25 @@ readFiniteFile : String → IO String
9497
readFiniteFile f = lift (Prim.readFiniteFile f)
9598

9699
writeFile∞ : String Costring IO ⊤
97-
writeFile∞ f s =
98-
♯ lift (Prim.writeFile f s) >>
99-
♯ return _
100+
writeFile∞ f s = ignore (lift (Prim.writeFile f s))
100101

101102
writeFile : String String IO ⊤
102103
writeFile f s = writeFile∞ f (toCostring s)
103104

104105
appendFile∞ : String Costring IO ⊤
105-
appendFile∞ f s =
106-
♯ lift (Prim.appendFile f s) >>
107-
♯ return _
106+
appendFile∞ f s = ignore (lift (Prim.appendFile f s))
108107

109108
appendFile : String String IO ⊤
110109
appendFile f s = appendFile∞ f (toCostring s)
111110

112111
putStr∞ : Costring IO ⊤
113-
putStr∞ s =
114-
♯ lift (Prim.putStr s) >>
115-
♯ return _
112+
putStr∞ s = ignore (lift (Prim.putStr s))
116113

117114
putStr : String IO ⊤
118115
putStr s = putStr∞ (toCostring s)
119116

120117
putStrLn∞ : Costring IO ⊤
121-
putStrLn∞ s =
122-
♯ lift (Prim.putStrLn s) >>
123-
♯ return _
118+
putStrLn∞ s = ignore (lift (Prim.putStrLn s))
124119

125120
putStrLn : String IO ⊤
126121
putStrLn s = putStrLn∞ (toCostring s)

src/System/Environment.agda

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Miscellanous information about the system environment
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --without-K #-}
8+
9+
module System.Environment where
10+
11+
open import IO using (IO; lift; run; ignore)
12+
open import Data.List.Base using (List)
13+
open import Data.Maybe.Base using (Maybe)
14+
open import Data.Product using (_×_)
15+
open import Data.String.Base using (String)
16+
open import Data.Unit.Polymorphic using (⊤)
17+
open import Foreign.Haskell.Coerce
18+
19+
import System.Environment.Primitive as Prim
20+
21+
getArgs : IO (List String)
22+
getArgs = lift Prim.getArgs
23+
24+
getProgName : IO String
25+
getProgName = lift Prim.getProgName
26+
27+
lookupEnv : String IO (Maybe String)
28+
lookupEnv var = lift (coerce (Prim.lookupEnv var))
29+
30+
setEnv : String String IO ⊤
31+
setEnv var val = ignore (lift (Prim.setEnv var val))
32+
33+
unsetEnv : String IO ⊤
34+
unsetEnv var = ignore (lift (Prim.unsetEnv var))
35+
36+
withArgs : {a} {A : Set a} List String IO A IO A
37+
withArgs args io = lift (Prim.withArgs args (run io))
38+
39+
withProgName : {a} {A : Set a} String IO A IO A
40+
withProgName name io = lift (Prim.withProgName name (run io))
41+
42+
getEnvironment : IO (List (String × String))
43+
getEnvironment = lift (coerce Prim.getEnvironment)

src/System/Environment/Primitive.agda

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Primitive System.Environment: simple bindings to Haskell functions
5+
--
6+
-- Note that we currently leave out:
7+
-- * filepath-related functions (until we have a good representation of
8+
-- absolute vs. relative & directory vs. file)
9+
-- * functions that may fail with an exception
10+
-- e.g. we provide `lookupEnv` but not `getEnv`
11+
------------------------------------------------------------------------
12+
13+
{-# OPTIONS --without-K #-}
14+
15+
module System.Environment.Primitive where
16+
17+
open import IO.Primitive using (IO)
18+
open import Data.List.Base using (List)
19+
open import Data.String.Base using (String)
20+
open import Data.Unit using (⊤)
21+
22+
open import Foreign.Haskell.Maybe using (Maybe)
23+
open import Foreign.Haskell.Pair using (Pair)
24+
25+
{-# FOREIGN GHC import qualified System.Environment as SE #-}
26+
{-# FOREIGN GHC import qualified Data.Text as T #-}
27+
{-# FOREIGN GHC import Data.Bifunctor (bimap) #-}
28+
{-# FOREIGN GHC import Data.Function (on) #-}
29+
30+
postulate
31+
getArgs : IO (List String)
32+
getProgName : IO String
33+
lookupEnv : String IO (Maybe String)
34+
setEnv : String String IO ⊤
35+
unsetEnv : String IO ⊤
36+
withArgs : {a} {A : Set a} List String IO A IO A
37+
withProgName : {a} {A : Set a} String IO A IO A
38+
getEnvironment : IO (List (Pair String String))
39+
40+
{-# COMPILE GHC getArgs = fmap (fmap T.pack) SE.getArgs #-}
41+
{-# COMPILE GHC getProgName = fmap T.pack SE.getProgName #-}
42+
{-# COMPILE GHC lookupEnv = fmap (fmap T.pack) . SE.lookupEnv . T.unpack #-}
43+
{-# COMPILE GHC setEnv = SE.setEnv `on` T.unpack #-}
44+
{-# COMPILE GHC unsetEnv = SE.unsetEnv . T.unpack #-}
45+
{-# COMPILE GHC withArgs = \ _ _ -> SE.withArgs . fmap T.unpack #-}
46+
{-# COMPILE GHC withProgName = \ _ _ -> SE.withProgName . T.unpack #-}
47+
{-# COMPILE GHC getEnvironment = fmap (fmap (bimap T.pack T.pack)) SE.getEnvironment #-}

0 commit comments

Comments
 (0)