1 module Language.Symantic.Document.Sym where
3 import Data.Char (Char)
4 import Data.Foldable (Foldable(..))
5 import Data.Function ((.), ($))
6 import Data.Functor (Functor(..))
7 import Data.Int (Int, Int64)
8 import Data.Ord (Ord(..), Ordering(..))
9 import Data.Semigroup (Semigroup(..))
10 import Data.String (String, IsString)
11 import Data.Text (Text)
12 import Prelude (Integer, toInteger, fromIntegral, Num(..))
13 import qualified Data.List as L
14 import qualified Data.Text as T
15 import qualified Data.Text.Lazy as TL
18 class (IsString d, Semigroup d) => Doc_Text d where
19 charH :: Char -> d -- ^ XXX: MUST NOT be '\n'
20 stringH :: String -> d -- ^ XXX: MUST NOT contain '\n'
21 textH :: Text -> d -- ^ XXX: MUST NOT contain '\n'
22 ltextH :: TL.Text -> d -- ^ XXX: MUST NOT contain '\n'
23 replicate :: Int -> d -> d
24 integer :: Integer -> d
25 default replicate :: Doc_Text (ReprOf d) => Trans d => Int -> d -> d
26 default integer :: Doc_Text (ReprOf d) => Trans d => Integer -> d
27 default charH :: Doc_Text (ReprOf d) => Trans d => Char -> d
28 default stringH :: Doc_Text (ReprOf d) => Trans d => String -> d
29 default textH :: Doc_Text (ReprOf d) => Trans d => Text -> d
30 default ltextH :: Doc_Text (ReprOf d) => Trans d => TL.Text -> d
32 stringH = trans . stringH
34 ltextH = trans . ltextH
35 replicate = trans1 . replicate
36 integer = trans . integer
47 catH :: Foldable f => f d -> d
48 catV :: Foldable f => f d -> d
49 foldrWith :: Foldable f => (d -> d -> d) -> f d -> d
50 foldWith :: Foldable f => (d -> d) -> f d -> d
51 intercalate :: Foldable f => d -> f d -> d
52 between :: d -> d -> d -> d
56 x <+> y = x <> space <> y
57 int = integer . toInteger
58 char = \case '\n' -> newline; c -> charH c
59 string = catV . fmap stringH . L.lines
60 text = catV . fmap textH . T.lines
61 ltext = catV . fmap ltextH . TL.lines
62 catH = foldr (<>) empty
63 catV = foldrWith (\x y -> x<>newline<>y)
64 foldrWith f ds = if null ds then empty else foldr1 f ds
65 foldWith f = foldrWith (\a acc -> a <> f acc)
66 intercalate sep = foldrWith (\x y -> x<>sep<>y)
67 between o c d = o<>d<>c
68 -- default catH :: Doc_Text (ReprOf d) => Trans d => Foldable f => Functor f => f d -> d
69 -- default catV :: Doc_Text (ReprOf d) => Trans d => Foldable f => Functor f => f d -> d
70 -- catH l = trans (catH (fmap unTrans l))
71 -- catV l = trans (catV (fmap unTrans l))
73 -- * Class 'Doc_Align'
74 class Doc_Text d => Doc_Align d where
79 -- | @align d@, make @d@ uses current 'Column' as 'Indent' level.
81 -- | @hang ind d@, make @d@ uses current 'Column' plus @ind@ as 'Indent' level.
82 hang :: Indent d -> d -> d
83 hang ind = align . incrIndent ind
84 -- | @incrIndent ind d@, make @d@ uses current 'Indent' plus @ind@ as 'Indent' level.
85 incrIndent :: Indent d -> d -> d
86 -- | @withIndent ind d@, make @d@ uses @ind@ as 'Indent' level.
87 withIndent :: Indent d -> d -> d
88 -- | @withNewline nl d@, make @d@ uses @nl@ as 'newline'.
89 withNewline :: d -> d -> d
90 -- | @column f@, return @f@ applied to the current 'Column'.
91 column :: (Column d -> d) -> d
92 -- | @endToEndWidth d f@, return @d@ concatenated to
93 -- @f@ applied to the difference between the end 'Column' and start 'Column' of @d@.
95 -- Note that @f@ is given the end-to-end width,
96 -- which is not necessarily the maximal width.
97 default endToEndWidth ::
100 d -> (Column d -> d) -> d
101 endToEndWidth :: d -> (Column d -> d) -> d
102 endToEndWidth d f = column $ \c1 -> (d <>) $ column $ \c2 -> f $ c2 - c1
104 -- | @spaces ind@, replicates 'space' @ind@ times.
105 default spaces :: Indent d ~ Int => Indent d -> d
106 spaces :: Indent d -> d
107 spaces i = replicate i space
113 fill :: Indent d -> d -> d
115 endToEndWidth d $ \w ->
120 default breakableFill ::
124 breakableFill :: Indent d -> d -> d
126 endToEndWidth d $ \w ->
130 GT -> incrIndent m newline
132 -- * Class 'Doc_Wrap'
133 class (Doc_Text d, Doc_Align d) => Doc_Wrap d where
134 -- | @ifFit onFit onNoFit@,
135 -- return @onFit@ if @onFit@ leads to a 'Column'
136 -- lower or equal to the one sets with 'withWrapColumn',
137 -- otherwise return @onNoFit@.
139 -- | @breakpoint onNoBreak onBreak d@,
140 -- return @onNoBreak@ then @d@ if they fit,
141 -- @onBreak@ otherwise.
142 breakpoint :: d -> d -> d -> d
143 -- | @breakableEmpty d@, return @d@ if it fits, 'newline' then @d@ otherwise.
144 breakableEmpty :: d -> d
145 breakableEmpty = breakpoint empty newline
146 -- | @breakableSpace d@, return 'space' then @d@ it they fit, 'newline' then @d@ otherwise.
147 breakableSpace :: d -> d
148 breakableSpace = breakpoint space newline
149 -- | @breakableSpaces ds@ intercalate a 'breakableSpace' between items of @ds@.
150 breakableSpaces :: Foldable f => f d -> d
151 breakableSpaces = foldWith breakableSpace
152 -- | @withWrapColumn col d@ set the 'Column' triggering wrapping to @col@ within @d@.
153 withWrapColumn :: Column d -> d -> d
154 -- | @intercalateHorV sep ds@,
155 -- return @ds@ with @sep@ intercalated if the whole fits,
156 -- otherwise return 'align' of @ds@ with 'newline' and @sep@ intercalated.
157 intercalateHorV :: Foldable f => d -> f d -> d
158 intercalateHorV sep xs =
159 ifFit (foldr1 (\a acc -> a <> sep <> acc) xs)
160 (align $ foldr1 (\a acc -> a <> newline <> sep <> acc) xs)
162 -- * Class 'Doc_Color'
163 class Doc_Color d where
204 onMagentaer :: d -> d
208 default reverse :: Doc_Color (ReprOf d) => Trans d => d -> d
209 default black :: Doc_Color (ReprOf d) => Trans d => d -> d
210 default red :: Doc_Color (ReprOf d) => Trans d => d -> d
211 default green :: Doc_Color (ReprOf d) => Trans d => d -> d
212 default yellow :: Doc_Color (ReprOf d) => Trans d => d -> d
213 default blue :: Doc_Color (ReprOf d) => Trans d => d -> d
214 default magenta :: Doc_Color (ReprOf d) => Trans d => d -> d
215 default cyan :: Doc_Color (ReprOf d) => Trans d => d -> d
216 default white :: Doc_Color (ReprOf d) => Trans d => d -> d
217 default blacker :: Doc_Color (ReprOf d) => Trans d => d -> d
218 default redder :: Doc_Color (ReprOf d) => Trans d => d -> d
219 default greener :: Doc_Color (ReprOf d) => Trans d => d -> d
220 default yellower :: Doc_Color (ReprOf d) => Trans d => d -> d
221 default bluer :: Doc_Color (ReprOf d) => Trans d => d -> d
222 default magentaer :: Doc_Color (ReprOf d) => Trans d => d -> d
223 default cyaner :: Doc_Color (ReprOf d) => Trans d => d -> d
224 default whiter :: Doc_Color (ReprOf d) => Trans d => d -> d
225 default onBlack :: Doc_Color (ReprOf d) => Trans d => d -> d
226 default onRed :: Doc_Color (ReprOf d) => Trans d => d -> d
227 default onGreen :: Doc_Color (ReprOf d) => Trans d => d -> d
228 default onYellow :: Doc_Color (ReprOf d) => Trans d => d -> d
229 default onBlue :: Doc_Color (ReprOf d) => Trans d => d -> d
230 default onMagenta :: Doc_Color (ReprOf d) => Trans d => d -> d
231 default onCyan :: Doc_Color (ReprOf d) => Trans d => d -> d
232 default onWhite :: Doc_Color (ReprOf d) => Trans d => d -> d
233 default onBlacker :: Doc_Color (ReprOf d) => Trans d => d -> d
234 default onRedder :: Doc_Color (ReprOf d) => Trans d => d -> d
235 default onGreener :: Doc_Color (ReprOf d) => Trans d => d -> d
236 default onYellower :: Doc_Color (ReprOf d) => Trans d => d -> d
237 default onBluer :: Doc_Color (ReprOf d) => Trans d => d -> d
238 default onMagentaer :: Doc_Color (ReprOf d) => Trans d => d -> d
239 default onCyaner :: Doc_Color (ReprOf d) => Trans d => d -> d
240 default onWhiter :: Doc_Color (ReprOf d) => Trans d => d -> d
242 reverse = trans1 reverse
246 yellow = trans1 yellow
248 magenta = trans1 magenta
251 blacker = trans1 blacker
252 redder = trans1 redder
253 greener = trans1 greener
254 yellower = trans1 yellower
256 magentaer = trans1 magentaer
257 cyaner = trans1 cyaner
258 whiter = trans1 whiter
259 onBlack = trans1 onBlack
261 onGreen = trans1 onGreen
262 onYellow = trans1 onYellow
263 onBlue = trans1 onBlue
264 onMagenta = trans1 onMagenta
265 onCyan = trans1 onCyan
266 onWhite = trans1 onWhite
267 onBlacker = trans1 onBlacker
268 onRedder = trans1 onRedder
269 onGreener = trans1 onGreener
270 onYellower = trans1 onYellower
271 onBluer = trans1 onBluer
272 onMagentaer = trans1 onMagentaer
273 onCyaner = trans1 onCyaner
274 onWhiter = trans1 onWhiter
276 -- * Class 'Doc_Decoration'
277 class Doc_Decoration d where
281 default bold :: Doc_Decoration (ReprOf d) => Trans d => d -> d
282 default underline :: Doc_Decoration (ReprOf d) => Trans d => d -> d
283 default italic :: Doc_Decoration (ReprOf d) => Trans d => d -> d
285 underline = trans1 underline
286 italic = trans1 italic
290 -- | Return the underlying @tr@ of the transformer.
293 -- | Lift a tr to the transformer's.
294 trans :: ReprOf tr -> tr
295 -- | Unlift a tr from the transformer's.
296 unTrans :: tr -> ReprOf tr
298 -- | Identity transformation for a unary symantic method.
299 trans1 :: (ReprOf tr -> ReprOf tr) -> (tr -> tr)
300 trans1 f = trans . f . unTrans
302 -- | Identity transformation for a binary symantic method.
304 :: (ReprOf tr -> ReprOf tr -> ReprOf tr)
306 trans2 f t1 t2 = trans (f (unTrans t1) (unTrans t2))
308 -- | Identity transformation for a ternary symantic method.
310 :: (ReprOf tr -> ReprOf tr -> ReprOf tr -> ReprOf tr)
311 -> (tr -> tr -> tr -> tr)
312 trans3 f t1 t2 t3 = trans (f (unTrans t1) (unTrans t2) (unTrans t3))
314 int64OfInt :: Int -> Int64
315 int64OfInt = fromIntegral
317 intOfInt64 :: Int64 -> Int
318 intOfInt64 = fromIntegral