Debian Codegraph @

src/System/FilePath/Posix/Primitive.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Primitive System.FilePath.Posix simple bindings to Haskell functions
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --guardedness #-}
8
9 module System.FilePath.Posix.Primitive where
10
11 open import Agda.Builtin.Bool using (Bool)
12 open import Agda.Builtin.Char using (Char)
13 open import Agda.Builtin.List using (List)
14 open import Agda.Builtin.Maybe using (Maybe)
15 open import Agda.Builtin.String using (String)
16 open import Foreign.Haskell as FFI using (Pair; Either)
17 open import IO.Primitive.Core using (IO)
18
19 -- A filepath has a nature: it can be either relative or absolute.
20 -- We postulate this nature rather than defining it as an inductive
21 -- type so that the user cannot inspect it. The only way to cast an
22 -- arbitrary filepath nature @n@ to either @relative@ or @absolute@
23 -- is to use @checkFilePath@.
24
25 module Nature where
26
27 postulate
28 Nature : Set
29 relative absolute unknown : Nature
30
31 -- In the Haskell backend these @natures@ are simply erased as the
32 -- libraries represent all filepaths in the same way.
33
34 {-# FOREIGN GHC import Data.Kind #-}
35 {-# COMPILE GHC Nature = type Type #-}
36 {-# COMPILE GHC relative = type () #-}
37 {-# COMPILE GHC absolute = type () #-}
38 {-# COMPILE GHC unknown = type () #-}
39
40 open Nature using (Nature) public
41
42 private
43 variable
44 m n : Nature
45
46 postulate
47
48 FilePath : Nature → Set
49 getFilePath : FilePath n → String
50
51 Extension : Set
52 mkExtension : String → Extension
53 getExtension : Extension → String
54
55 {-# FOREIGN GHC import Data.Text #-}
56 {-# FOREIGN GHC import System.FilePath.Posix #-}
57 {-# FOREIGN GHC type AgdaFilePath n = FilePath #-}
58 {-# COMPILE GHC FilePath = type AgdaFilePath #-}
59 {-# COMPILE GHC getFilePath = const pack #-}
60 {-# COMPILE GHC Extension = type String #-}
61 {-# COMPILE GHC mkExtension = unpack #-}
62 {-# COMPILE GHC getExtension = pack #-}
63
64 -- We provide convenient short names for the two types of filepaths
65
66 AbsolutePath = FilePath Nature.absolute
67 RelativePath = FilePath Nature.relative
68 SomePath = FilePath Nature.unknown
69
70 -- In order to prevent users from picking whether a string gets
71 -- converted to a @relative@ or an @absolute@ path we have:
72 -- * a postulated @unknown@ nature
73 -- * a function @mkFilePath@ producing filepaths of this postulated nature
74
75 postulate
76 mkFilePath : String → SomePath
77
78 {-# COMPILE GHC mkFilePath = unpack #-}
79
80 postulate
81
82 -- Separator predicates
83
84 pathSeparator : Char
85 pathSeparators : List Char
86 isPathSeparator : Char → Bool
87 searchPathSeparator : Char
88 isSearchPathSeparator : Char → Bool
89 extSeparator : Char
90 isExtSeparator : Char → Bool
91
92 -- $PATH methods
93
94 splitSearchPath : String → List SomePath
95 getSearchPath : IO (List SomePath)
96
97 -- Extension functions
98
99 splitExtension : FilePath n → Pair (FilePath n) Extension
100 takeExtension : FilePath n → Extension
101 replaceExtension : FilePath n → Extension → FilePath n
102 dropExtension : FilePath n → FilePath n
103 addExtension : FilePath n → Extension → FilePath n
104 hasExtension : FilePath n → Bool
105 splitExtensions : FilePath n → Pair (FilePath n) Extension
106 takeExtensions : FilePath n → Extension
107 replaceExtensions : FilePath n → Extension → FilePath n
108 dropExtensions : FilePath n → FilePath n
109 isExtensionOf : Extension → FilePath n → Bool
110 stripExtension : Extension → FilePath n → Maybe (FilePath n)
111
112 -- Filename/directory functions
113
114 splitFileName : FilePath n → Pair (FilePath n) RelativePath
115 takeFileName : FilePath n → String
116 replaceFileName : FilePath n → String → FilePath n
117 dropFileName : FilePath n → FilePath n
118 takeBaseName : FilePath n → String
119 replaceBaseName : FilePath n → String → FilePath n
120 takeDirectory : FilePath n → FilePath n
121 replaceDirectory : FilePath m → FilePath n → FilePath n
122 combine : FilePath n → RelativePath → FilePath n
123 splitPath : FilePath n → List RelativePath
124 joinPath : List RelativePath → RelativePath
125 splitDirectories : FilePath n → List RelativePath
126
127 -- Drive functions
128
129 splitDrive : FilePath n → Pair (FilePath n) RelativePath
130 joinDrive : FilePath n → RelativePath → FilePath n
131 takeDrive : FilePath n → FilePath n
132 hasDrive : FilePath n → Bool
133 dropDrive : FilePath n → RelativePath
134 isDrive : FilePath n → Bool
135
136 -- Trailing slash functions
137
138 hasTrailingPathSeparator : FilePath n → Bool
139 addTrailingPathSeparator : FilePath n → FilePath n
140 dropTrailingPathSeparator : FilePath n → FilePath n
141
142 -- File name manipulations
143
144 normalise : FilePath n → FilePath n
145 equalFilePath : FilePath m → FilePath n → Bool
146 makeRelative : FilePath m → FilePath n → RelativePath
147 checkFilePath : FilePath n → Either RelativePath AbsolutePath
148 isRelative : FilePath n → Bool
149 isAbsolute : FilePath n → Bool
150 isValid : FilePath n → Bool
151 makeValid : FilePath n → FilePath n
152
153
154 {-# FOREIGN GHC
155 checkFilePath fp
156 | isRelative fp = Left fp
157 | otherwise = Right fp
158 #-}
159
160 {-# COMPILE GHC pathSeparator = pathSeparator #-}
161 {-# COMPILE GHC pathSeparators = pathSeparators #-}
162 {-# COMPILE GHC isPathSeparator = isPathSeparator #-}
163 {-# COMPILE GHC searchPathSeparator = searchPathSeparator #-}
164 {-# COMPILE GHC isSearchPathSeparator = isSearchPathSeparator #-}
165 {-# COMPILE GHC extSeparator = extSeparator #-}
166 {-# COMPILE GHC isExtSeparator = isExtSeparator #-}
167
168 {-# COMPILE GHC splitSearchPath = splitSearchPath . unpack #-}
169 {-# COMPILE GHC getSearchPath = getSearchPath #-}
170
171 {-# COMPILE GHC splitExtension = const splitExtension #-}
172 {-# COMPILE GHC takeExtension = const takeExtension #-}
173 {-# COMPILE GHC replaceExtension = const replaceExtension #-}
174 {-# COMPILE GHC dropExtension = const dropExtension #-}
175 {-# COMPILE GHC addExtension = const addExtension #-}
176 {-# COMPILE GHC hasExtension = const hasExtension #-}
177 {-# COMPILE GHC splitExtensions = const splitExtensions #-}
178 {-# COMPILE GHC takeExtensions = const takeExtensions #-}
179 {-# COMPILE GHC replaceExtensions = const replaceExtensions #-}
180 {-# COMPILE GHC dropExtensions = const dropExtensions #-}
181 {-# COMPILE GHC isExtensionOf = const isExtensionOf #-}
182 {-# COMPILE GHC stripExtension = const stripExtension #-}
183
184 {-# COMPILE GHC splitFileName = const splitFileName #-}
185 {-# COMPILE GHC takeFileName = const $ pack . takeFileName #-}
186 {-# COMPILE GHC replaceFileName = const $ fmap (. unpack) replaceFileName #-}
187 {-# COMPILE GHC dropFileName = const dropFileName #-}
188 {-# COMPILE GHC takeBaseName = const $ pack . takeBaseName #-}
189 {-# COMPILE GHC replaceBaseName = const $ fmap (. unpack) replaceBaseName #-}
190 {-# COMPILE GHC takeDirectory = const takeDirectory #-}
191 {-# COMPILE GHC replaceDirectory = \ _ _ -> replaceDirectory #-}
192 {-# COMPILE GHC combine = const combine #-}
193 {-# COMPILE GHC splitPath = const splitPath #-}
194 {-# COMPILE GHC joinPath = joinPath #-}
195 {-# COMPILE GHC splitDirectories = const splitDirectories #-}
196
197 {-# COMPILE GHC splitDrive = const splitDrive #-}
198 {-# COMPILE GHC joinDrive = const joinDrive #-}
199 {-# COMPILE GHC takeDrive = const takeDrive #-}
200 {-# COMPILE GHC hasDrive = const hasDrive #-}
201 {-# COMPILE GHC dropDrive = const dropDrive #-}
202 {-# COMPILE GHC isDrive = const isDrive #-}
203
204 {-# COMPILE GHC hasTrailingPathSeparator = const hasTrailingPathSeparator #-}
205 {-# COMPILE GHC addTrailingPathSeparator = const addTrailingPathSeparator #-}
206 {-# COMPILE GHC dropTrailingPathSeparator = const dropTrailingPathSeparator #-}
207
208 {-# COMPILE GHC normalise = const normalise #-}
209 {-# COMPILE GHC equalFilePath = \ _ _ -> equalFilePath #-}
210 {-# COMPILE GHC makeRelative = \ _ _ -> makeRelative #-}
211 {-# COMPILE GHC isRelative = const isRelative #-}
212 {-# COMPILE GHC isAbsolute = const isAbsolute #-}
213 {-# COMPILE GHC checkFilePath = const checkFilePath #-}
214 {-# COMPILE GHC isValid = const isValid #-}
215 {-# COMPILE GHC makeValid = const makeValid #-}