Debian Codegraph @

src/IO/Primitive/Finite.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Primitive IO: simple bindings to Haskell types and functions
5 -- Everything is assumed to be finite
6 ------------------------------------------------------------------------
7
8 {-# OPTIONS --cubical-compatible #-}
9
10 module IO.Primitive.Finite where
11
12 -- NOTE: the contents of this module should be accessed via `IO` or
13 -- `IO.Finite`.
14
15 open import Agda.Builtin.IO using (IO)
16 open import Agda.Builtin.String using (String)
17 open import Agda.Builtin.Unit using () renaming (⊤ to Unit)
18
19 ------------------------------------------------------------------------
20 -- Simple lazy IO
21
22 -- Note that the functions below produce commands which, when
23 -- executed, may raise exceptions.
24
25 -- Note also that the semantics of these functions depends on the
26 -- version of the Haskell base library. If the version is 4.2.0.0 (or
27 -- later?), then the functions use the character encoding specified by
28 -- the locale. For older versions of the library (going back to at
29 -- least version 3) the functions use ISO-8859-1.
30
31 postulate
32 getLine : IO String
33 readFile : String → IO String
34 writeFile : String → String → IO Unit
35 appendFile : String → String → IO Unit
36 putStr : String → IO Unit
37 putStrLn : String → IO Unit
38
39 {-# FOREIGN GHC import qualified Data.Text as T #-}
40 {-# FOREIGN GHC import qualified Data.Text.IO as TIO #-}
41 {-# FOREIGN GHC import qualified System.IO #-}
42 {-# FOREIGN GHC import qualified Control.Exception #-}
43
44 {-# FOREIGN GHC
45
46 -- Reads a finite file. Raises an exception if the file path refers
47 -- to a non-physical file (like "/dev/zero").
48 readFiniteFile :: T.Text -> IO T.Text
49 readFiniteFile f = do
50 h <- System.IO.openFile (T.unpack f) System.IO.ReadMode
51 Control.Exception.bracketOnError (return ()) (\_ -> System.IO.hClose h)
52 (\_ -> System.IO.hFileSize h)
53 TIO.hGetContents h
54
55 #-}
56
57 {-# COMPILE GHC getLine = TIO.getLine #-}
58 {-# COMPILE GHC readFile = readFiniteFile #-}
59 {-# COMPILE GHC writeFile = TIO.writeFile . T.unpack #-}
60 {-# COMPILE GHC appendFile = TIO.appendFile . T.unpack #-}
61 {-# COMPILE GHC putStr = TIO.putStr #-}
62 {-# COMPILE GHC putStrLn = TIO.putStrLn #-}
63
64 {-# COMPILE UHC readFile = UHC.Agda.Builtins.primReadFiniteFile #-}