summaryrefslogtreecommitdiff
path: root/Coalpit.hs
blob: dfb84d3540f4ab5eb02aff3f7672f33d480eb0ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
{- |
Description :  Command-line options and DSV parsing and printing
Maintainer  :  defanor <defanor@uberspace.net>
Stability   :  unstable
Portability :  non-portable (uses GHC extensions)

Coalpit is a library for building command-line interfaces: the goal is
to build interfaces quickly and easily (by deriving those), while
keeping them language-agnostic and more user- and shell
scripting-friendly than JSON and similar formats.

-}

{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Coalpit ( fromDSV
               , fromDSVList
               , toDSV
               , toDSVList
               , dsvFromList
               , Coalpit(..)
                 -- * Usage
               , usage
               , usageString
               , Usage(..)
                 -- * Options
               , SelNamePolicy(..)
               , Options(..)
               , defOpt
                 -- * Parsing and composition helpers
               , escape
               , pString
               , pFieldSep
               , pRecordSep
               ) where

import GHC.Generics
import Text.Parsec
import Text.Parsec.String
import Data.Char (toLower)
import Data.Proxy (Proxy(..))
import qualified Data.List.NonEmpty as NE
import Data.List.NonEmpty (NonEmpty(..))
import Data.List (intercalate)
import Data.Word (Word8, Word16, Word32, Word64)
import Numeric.Natural (Natural)
import Data.Int (Int8, Int16, Int32, Int64)
import Data.Time.Clock (DiffTime, NominalDiffTime, UniversalTime, UTCTime)
import Data.Time.Format ( TimeLocale, formatTime
                        , iso8601DateFormat, defaultTimeLocale
                        , ParseTime, readSTime)
import Data.Time.Calendar (Day)
import Data.Time.LocalTime (TimeOfDay, LocalTime, ZonedTime)
import Data.Scientific (Scientific, FPFormat(..), formatScientific, scientificP)
import Text.ParserCombinators.ReadP (readP_to_S)
import Data.Complex (Complex)
import Data.Version (Version, parseVersion, showVersion)
import System.Exit (ExitCode)
import Network.URI (URI, parseURIReference, uriToString)

-- | Usage description: can be translated into help messages or
-- documentation formats.
data Usage = UConstructor String
           -- ^ Data constructor.
           | URecursive String
           -- ^ Constructor of a recursive data structure (its second
           -- appearance in the tree).
           | USelector Bool String Usage
           -- ^ Record selector.
           | UOptional Usage
           -- ^ Optional element.
           | USum Usage Usage
           -- ^ Sum.
           | UProduct Usage Usage
           -- ^ Product.
           | UUnit
           -- ^ Unit.
           | UType String
           -- ^ Type name (e.g., \"INT\").
           deriving (Show)

-- | How to handle selector names.
data SelNamePolicy = SNDisable
                   -- ^ Do not parse or print selector names
                   | SNAvoid
                   -- ^ Allow selector names on parsing, but do not
                   -- print them
                   | SNPrefer
                   -- ^ Allow selector names on parsing, print them
                   | SNRequire
                   -- ^ Require selector names on parsing, print them
  deriving (Show, Eq)

-- | Printing and parsing options.
data Options = Options { fieldSeparators :: NonEmpty Char
                       -- ^ Separators between fields
                       , recordSeparators :: NonEmpty Char
                       -- ^ Separators between records (which may
                       -- correspond to lines)
                       , conNameMod :: String -> String
                       -- ^ Constructor name modifier
                       , selNameMod :: String -> String
                       -- ^ Record selector name modifier
                       , selNamePolicy :: SelNamePolicy
                       , timeLocale :: TimeLocale
                       , dateFormat :: String
                       -- ^ See "Data.Time.Format".
                       , timeFormat :: String
                       , dateTimeFormat :: String
                       , scientificFormat :: FPFormat
                       , scientificDecimals :: Maybe Int
                       , uriUserInfo :: String -> String
                       -- ^ Used to map userinfo parts of URIs.
                       }

-- | Default options.
defOpt :: Options
defOpt = Options (' ' :| ['\t']) ('\n' :| [])
  (map toLower) (("--" ++) . map toLower) SNAvoid
  defaultTimeLocale (iso8601DateFormat Nothing) "%H:%M:%S"
  (iso8601DateFormat (Just "%H:%M:%S")) Generic Nothing id

parseDSV :: Parser a -> String -> Either String a
parseDSV p s = case parse p "DSV" s of
  Left err -> Left $ show err
  Right x -> Right x

-- | Parse a single record from a string.
fromDSV :: Coalpit a => Options -> String -> Either String a
fromDSV opt str = parseDSV (coalpitParser opt) str

-- | Parse multiple records from a string.
fromDSVList :: Coalpit a => Options -> String -> Either String [a]
fromDSVList opt str =
  parseDSV (coalpitParser opt `sepEndBy` pRecordSep opt) str

-- | Enquote and escape a string, if it contains any characters that
-- need it.
escape :: Options -> String -> String
escape opt str
  | not (null str) &&
    all (\fs -> not (fs `elem` str))
    ('\\'
     : '\"'
     : NE.toList (fieldSeparators opt)
     ++ NE.toList (recordSeparators opt)) = str
  | otherwise = '"' : escaped str ++ "\""
  where
    escaped :: String -> String
    escaped [] = []
    escaped (c:rest)
      | c `elem` "\\\"" = '\\' : c : escaped rest
      | otherwise = c : escaped rest

-- | Build a record ("line") out of individual strings, escaping those
-- if needed.
dsvFromList :: Options -> [String] -> String
dsvFromList opt l = intercalate [NE.head (fieldSeparators opt)]
                    (map (escape opt) l)

-- | Serialize a value.
toDSV :: Coalpit a => Options -> a -> String
toDSV opt x = dsvFromList opt (coalpitPrint opt x)

-- | Serialize multiple values.
toDSVList :: Coalpit a => Options -> [a] -> String
toDSVList opt l =
  concatMap (\x -> toDSV opt x ++ [NE.head (recordSeparators opt)]) l

-- | Compose 'Usage' description.
usage :: Coalpit a => Options -> Proxy a -> Usage
usage opt = coalpitDescription opt []

-- | Compose a usage string.
usageString :: Coalpit a => Options -> Proxy a -> String
usageString opt = usageToString . usage opt

-- | Translate 'Usage' into a string, used by 'usageString'.
usageToString :: Usage -> String
usageToString (UConstructor c) = c
usageToString (URecursive c) = c ++ "..."
usageToString (USelector False s u) = "[" ++ s ++ "] " ++ usageToString u
usageToString (USelector True s u) = s ++ " " ++ usageToString u
usageToString (UOptional u) = "[" ++ usageToString u ++ "]"
usageToString (USum ul ur) = concat [ "("
                                    , usageToString ul
                                    , " | "
                                    , usageToString ur
                                    , ")"]
usageToString (UProduct u1 UUnit) = usageToString u1
usageToString (UProduct u1 u2) = usageToString u1 ++ " " ++ usageToString u2
usageToString UUnit = ""
usageToString (UType t) = t

-- | Parse a field separator.
pFieldSep :: Options -> Parsec String m ()
pFieldSep opt =
  oneOf (NE.toList $ fieldSeparators opt) *> pure ()

-- | Parse a record (line) separator.
pRecordSep :: Options -> Parsec String m ()
pRecordSep opt =
  choice (eof
          : map (\c -> char c *> pure ())
           (NE.toList $ recordSeparators opt))

-- | Parse a token: either a quoted string or a string without
-- unescaped separators. The opposite of 'escape'.
pString :: Options -> Parsec String m String
pString opt =
  (try (quotedString <?> "quoted string"))
  <|> (unquotedString <?> "unquoted string")
  where
    endChars = NE.toList (fieldSeparators opt)
               ++ NE.toList (recordSeparators opt)
    unquotedString = do
      c <- escapedChar endChars
      s <- manyTill (escapedChar endChars)
        (lookAhead $ eof <|> oneOf endChars *> pure ())
      pure (c:s)
    escapedChar ecs = (char '\\' *> oneOf ('\\' : ecs)) <|> anyChar
    quotedString = char '"'
      *> manyTill (escapedChar "\"") (char '"')

-- | Parses a time argument.
pTime :: ParseTime a => Options -> String -> Parser a
pTime opt tf = try $ do
    x <- pString opt
    case readSTime False (timeLocale opt) tf x of
      [(t, "")] -> pure t
      _ -> fail "Failed to parse time"

-- | Read an argument using its 'Read' instance.
pRead :: Read a => Options -> Parser a
pRead opt = do
  x <- pString opt
  case reads x of
    [(n, "")] -> pure n
    _ -> fail $ "Failed to read: " ++ x

-- | Coalpit class: parsing, printing, usage strings.
class Coalpit a where
  coalpitParser :: Options -> Parser a
  default coalpitParser :: (Generic a, GCoalpit (Rep a)) => Options -> Parser a
  coalpitParser opt = to <$> gCoalpitParser opt

  coalpitPrint :: Options -> a -> [String]
  default coalpitPrint :: (Generic a, GCoalpit (Rep a)) => Options -> a -> [String]
  coalpitPrint opt a = gCoalpitPrint opt (from a)

  coalpitDescription :: Options -> [String] -> Proxy a -> Usage
  default coalpitDescription :: (GCoalpit (Rep a))
                    => Options -> [String] -> Proxy a -> Usage
  coalpitDescription opt path Proxy =
    gCoalpitDescription opt path (Proxy :: Proxy (Rep a p))

class GCoalpit a where
  gCoalpitParser :: Options -> Parser (a p)
  gCoalpitPrint :: Options -> a p -> [String]
  gCoalpitDescription :: Options -> [String] -> Proxy (a p) -> Usage


-- Units
instance GCoalpit U1 where
  gCoalpitParser _ = pure U1
  gCoalpitPrint _ U1 = []
  gCoalpitDescription _ _ (Proxy :: Proxy (U1 f)) = UUnit


-- Products
instance (GCoalpit a, GCoalpit b) => GCoalpit (a :*: b) where
  gCoalpitParser opt =
    ((:*:) <$>
     (gCoalpitParser opt <* pFieldSep opt) <*> gCoalpitParser opt)
    <?> "product"
  gCoalpitPrint opt (x :*: y) =
    gCoalpitPrint opt x ++ gCoalpitPrint opt y
  gCoalpitDescription opt path (Proxy :: Proxy ((a :*: b) p)) =
    UProduct (gCoalpitDescription opt path (Proxy :: Proxy (a p)))
    (gCoalpitDescription opt path (Proxy :: Proxy (b p)))


-- Sums
instance
  (GCoalpit a, GCoalpit b) => GCoalpit (a :+: b) where
  gCoalpitParser opt =
    (try (L1 <$> gCoalpitParser opt))
    <|>
    (R1 <$> gCoalpitParser opt)
  gCoalpitPrint opt (L1 x) = gCoalpitPrint opt x
  gCoalpitPrint opt (R1 x) = gCoalpitPrint opt x
  gCoalpitDescription opt path (Proxy :: Proxy ((a :+: b) p)) =
      USum (gCoalpitDescription opt path (Proxy :: Proxy (a p)))
       (gCoalpitDescription opt path (Proxy :: Proxy (b p)))

-- Record Selectors

parseS1 :: (GCoalpit a) => String -> Options -> Parser (S1 selA a p)
parseS1 nameA opt =
  let sName = case (nameA, selNamePolicy opt) of
        ("", _) -> pure ()
        (_, SNDisable) -> pure ()
        (_, SNRequire) -> string (selNameMod opt nameA) *> pFieldSep opt
        (_, _) -> optional
          (try $ (string (selNameMod opt nameA)) *> pFieldSep opt)
  in M1 <$> (sName *> gCoalpitParser opt)

printS1 :: (GCoalpit a, Selector selA) => Options -> S1 selA a p -> [String]
printS1 opt sel@(M1 x) = case (selName sel, selNamePolicy opt) of
                           ("", _) -> gCoalpitPrint opt x
                           (_, SNDisable) -> gCoalpitPrint opt x
                           (_, SNAvoid) -> gCoalpitPrint opt x
                           (name, _) -> selNameMod opt name : gCoalpitPrint opt x

helpS1 :: (GCoalpit a)
       => String -> Options -> [String] -> Proxy (S1 selA a p) -> Usage
helpS1 nameA opt path (Proxy :: Proxy (S1 selA a p)) =
  case (nameA, selNamePolicy opt) of
    ("", _) -> gCoalpitDescription opt path (Proxy :: Proxy (a p))
    (_, SNDisable) -> gCoalpitDescription opt path (Proxy :: Proxy (a p))
    (_, snpol) -> USelector (snpol == SNRequire) (selNameMod opt nameA)
      (gCoalpitDescription opt path (Proxy :: Proxy (a p)))

instance (GCoalpit a, Selector selA) => GCoalpit (S1 selA a) where
  gCoalpitParser = parseS1 (selName (undefined :: S1 selA a p))
  gCoalpitPrint = printS1
  gCoalpitDescription = helpS1 (selName (undefined :: S1 selA a p))

-- Constructors

-- | Handles recursive constructors.
handleRecCon :: GCoalpit a
             => String
             -- ^ Constructor name
             -> Options
             -> [String]
             -> Proxy (a p)
             -> Usage
handleRecCon nameA opt path (Proxy :: Proxy (a p)) =
  let n = conNameMod opt nameA
  in if nameA `elem` path
     then URecursive n
     else UProduct (UConstructor n)
          (gCoalpitDescription opt (nameA : path) (Proxy :: Proxy (a p)))

-- A constructor wrapping just a unit: no field separator is required
-- after such a constructor.
instance {-#OVERLAPPING#-} (Constructor conA) => GCoalpit (C1 conA U1) where
  gCoalpitParser opt =
    ((string
       (conNameMod opt $ conName (undefined :: C1 conA U1 w))
       <?> "constructor name"))
    *> (fmap M1 (gCoalpitParser opt))
  gCoalpitPrint opt (M1 x) = conNameMod opt (conName (undefined :: C1 conA U1 w))
                       : gCoalpitPrint opt x
  gCoalpitDescription opt path (Proxy :: Proxy (C1 conA U1 p)) =
    (handleRecCon (conName (undefined :: C1 conA U1 w)) opt path
     (Proxy :: Proxy (U1 p)))

-- A constructor with non-unit children, with a field separator
-- between constructor name and its children.
instance (GCoalpit a, Constructor conA) => GCoalpit (C1 conA a) where
  gCoalpitParser opt =
    ((string
       (conNameMod opt $ conName (undefined :: C1 conA a w))
       <?> "constructor name"))
    *> (pFieldSep opt)
    *> (fmap M1 (gCoalpitParser opt))
  gCoalpitPrint opt (M1 x) = conNameMod opt (conName (undefined :: C1 conA a w))
                       : gCoalpitPrint opt x
  gCoalpitDescription opt path (Proxy :: Proxy (C1 conA a p)) =
    (handleRecCon (conName (undefined :: C1 conA a w)) opt path
     (Proxy :: Proxy (a p)))

-- Data types
instance (GCoalpit a) => GCoalpit (D1 conA a) where
  gCoalpitParser = fmap M1 . gCoalpitParser
  gCoalpitPrint opt (M1 x) = gCoalpitPrint opt x
  gCoalpitDescription opt path (Proxy :: Proxy (D1 conA a p)) =
    gCoalpitDescription opt path (Proxy :: Proxy (a p))

-- Constraints and such
instance (Coalpit a) => GCoalpit (K1 i a) where
  gCoalpitParser = fmap K1 . coalpitParser
  gCoalpitPrint opt (K1 x) = coalpitPrint opt x
  gCoalpitDescription opt path (Proxy :: Proxy (K1 x a p)) =
    coalpitDescription opt path (Proxy :: Proxy a)


-- Common types

instance Coalpit Int where
  coalpitParser opt = pRead opt
  coalpitPrint _ i = [show i]
  coalpitDescription _ _ _ = UType "INT"

instance Coalpit Integer where
  coalpitParser opt = pRead opt
  coalpitPrint _ i = [show i]
  coalpitDescription _ _ _ = UType "INTEGER"

instance Coalpit Word8 where
  coalpitParser opt = pRead opt
  coalpitPrint _ i = [show i]
  coalpitDescription _ _ _ = UType "WORD8"

instance Coalpit Word16 where
  coalpitParser opt = pRead opt
  coalpitPrint _ i = [show i]
  coalpitDescription _ _ _ = UType "WORD16"

instance Coalpit Word32 where
  coalpitParser opt = pRead opt
  coalpitPrint _ i = [show i]
  coalpitDescription _ _ _ = UType "WORD32"

instance Coalpit Word64 where
  coalpitParser opt = pRead opt
  coalpitPrint _ i = [show i]
  coalpitDescription _ _ _ = UType "WORD64"

instance Coalpit Int8 where
  coalpitParser opt = pRead opt
  coalpitPrint _ i = [show i]
  coalpitDescription _ _ _ = UType "INT8"

instance Coalpit Int16 where
  coalpitParser opt = pRead opt
  coalpitPrint _ i = [show i]
  coalpitDescription _ _ _ = UType "INT16"

instance Coalpit Int32 where
  coalpitParser opt = pRead opt
  coalpitPrint _ i = [show i]
  coalpitDescription _ _ _ = UType "INT32"

instance Coalpit Int64 where
  coalpitParser opt = pRead opt
  coalpitPrint _ i = [show i]
  coalpitDescription _ _ _ = UType "INT64"

instance Coalpit Natural where
  coalpitParser opt = pRead opt
  coalpitPrint _ i = [show i]
  coalpitDescription _ _ _ = UType "NATURAL"

instance Coalpit Rational where
  coalpitParser opt = pRead opt
  coalpitPrint _ i = [show i]
  coalpitDescription _ _ _ = UType "RATIONAL"

instance Coalpit Double where
  coalpitParser opt = pRead opt
  coalpitPrint _ i = [show i]
  coalpitDescription _ _ _ = UType "DOUBLE"

instance Coalpit Float where
  coalpitParser opt = pRead opt
  coalpitPrint _ i = [show i]
  coalpitDescription _ _ _ = UType "FLOAT"

instance Coalpit Char where
  coalpitParser opt = pRead opt
  coalpitPrint _ c = [show c]
  coalpitDescription _ _ _ = UType "CHAR"


instance {-#OVERLAPPING#-} Coalpit String where
  coalpitParser opt = pString opt
  coalpitPrint _ i = [i]
  coalpitDescription _ _ _ = UType "STRING"

instance Coalpit Scientific where
  coalpitParser opt = try $ do
    x <- pString opt
    case reverse $ readP_to_S scientificP x of
      (n, ""):_ -> pure n
      _ -> fail $ "Failed to read a scientific number: " ++ x
  coalpitPrint opt n = [formatScientific
                  (scientificFormat opt) (scientificDecimals opt) n]
  coalpitDescription _ _ _ = UType "SCIENTIFIC"

instance Coalpit Version where
  coalpitParser opt = try $ do
    x <- pString opt
    case reverse $ readP_to_S parseVersion x of
      (v, ""):_ -> pure v
      _ -> fail $ "Failed to read a version: " ++ x
  coalpitPrint _ v = [showVersion v]
  coalpitDescription _ _ _ = UType "VERSION"


-- | An URI reference (absolute or relative).
instance Coalpit URI where
  coalpitParser opt = try $ do
    x <- pString opt
    maybe (fail $ "Failed to parse URI: " ++ x) pure (parseURIReference x)
  coalpitPrint opt u = [uriToString (uriUserInfo opt) u ""]
  coalpitDescription _ _ _ = UType "URI"


-- | Uses 'dateTimeFormat'.
instance Coalpit UTCTime where
  coalpitParser opt = pTime opt (dateTimeFormat opt)
  coalpitPrint opt t = [formatTime (timeLocale opt) (dateTimeFormat opt) t]
  coalpitDescription _ _ _ = UType "UTC_TIME"

-- | Uses 'dateTimeFormat'.
instance Coalpit ZonedTime where
  coalpitParser opt = pTime opt (dateTimeFormat opt)
  coalpitPrint opt t = [formatTime (timeLocale opt) (dateTimeFormat opt) t]
  coalpitDescription _ _ _ = UType "ZONED_TIME"

-- | Uses 'dateTimeFormat'.
instance Coalpit LocalTime where
  coalpitParser opt = pTime opt (dateTimeFormat opt)
  coalpitPrint opt t = [formatTime (timeLocale opt) (dateTimeFormat opt) t]
  coalpitDescription _ _ _ = UType "LOCAL_TIME"

-- | Uses 'dateTimeFormat'.
instance Coalpit UniversalTime where
  coalpitParser opt = pTime opt (dateTimeFormat opt)
  coalpitPrint opt t = [formatTime (timeLocale opt) (dateTimeFormat opt) t]
  coalpitDescription _ _ _ = UType "UNIVERSAL_TIME"

-- | Uses 'timeFormat'.
instance Coalpit TimeOfDay where
  coalpitParser opt = pTime opt (timeFormat opt)
  coalpitPrint opt t = [formatTime (timeLocale opt) (timeFormat opt) t]
  coalpitDescription _ _ _ = UType "TIME_OF_DAY"

-- | Uses 'dateFormat'.
instance Coalpit Day where
  coalpitParser opt = pTime opt (dateFormat opt)
  coalpitPrint opt t = [formatTime (timeLocale opt) (dateFormat opt) t]
  coalpitDescription _ _ _ = UType "DAY"

-- | Converts to/from 'Scientific'.
instance Coalpit NominalDiffTime where
  coalpitParser opt = fromRational . toRational
                  <$> (coalpitParser opt :: Parser Scientific)
  coalpitPrint opt = coalpitPrint opt .
    (fromRational . toRational :: NominalDiffTime -> Scientific)
  coalpitDescription _ _ _ = UType "NOMINAL_DIFF_TIME"

-- | Converts to/from 'Scientific'.
instance Coalpit DiffTime where
  coalpitParser opt = fromRational . toRational
                  <$> (coalpitParser opt :: Parser Scientific)
  coalpitPrint opt = coalpitPrint opt .
    (fromRational . toRational :: DiffTime -> Scientific)
  coalpitDescription _ _ _ = UType "DIFF_TIME"

instance Coalpit ()
instance Coalpit Bool
instance Coalpit Ordering
instance Coalpit ExitCode
instance Coalpit a => Coalpit (Complex a)
instance Coalpit a => Coalpit (Maybe a)
instance Coalpit a => Coalpit [a]
instance Coalpit a => Coalpit (NonEmpty a)
instance (Coalpit a, Coalpit b) => Coalpit (Either a b)
instance (Coalpit a, Coalpit b) => Coalpit (a, b)
instance (Coalpit a, Coalpit b, Coalpit c) => Coalpit (a, b, c)
instance (Coalpit a, Coalpit b, Coalpit c, Coalpit d) => Coalpit (a, b, c, d)