/
Serialise.hs
1828 lines (1568 loc) · 67.2 KB
/
Serialise.hs
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# OPTIONS_GHC -fwarn-missing-signatures #-}
-- Andreas, Makoto, Francesco 2014-10-15 AIM XX:
-- -O2 does not have any noticable effect on runtime
-- but sabotages cabal repl with -Werror
-- (due to a conflict with --interactive wraning)
-- {-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeSynonymInstances #-}
-- | Structure-sharing serialisation of Agda interface files.
-- -!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-
-- NOTE: Every time the interface format is changed the interface
-- version number should be bumped _in the same patch_.
-- -!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-!-
module Agda.TypeChecking.Serialise
( encode, encodeFile, encodeInterface
, decode, decodeFile, decodeInterface, decodeHashes
, EmbPrj
)
where
import Control.Applicative
import Control.Arrow (first, second)
import Control.DeepSeq
import qualified Control.Exception as E
import Control.Monad
import Control.Monad.Reader
import Control.Monad.State.Strict
import Data.Array.IArray
import Data.Word
import qualified Data.ByteString.Lazy as L
import Data.Hashable
import qualified Data.HashTable.IO as H
import Data.Int (Int32)
import Data.Maybe
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Set (Set)
import qualified Data.Set as Set
import qualified Data.Binary as B
import qualified Data.Binary.Get as B
import qualified Data.Binary.Put as B
import qualified Data.List as List
import Data.Function
import Data.Typeable
import qualified Codec.Compression.GZip as G
import qualified Agda.Compiler.Epic.Interface as Epic
import Agda.Syntax.Common
import Agda.Syntax.Concrete.Name as C
import qualified Agda.Syntax.Abstract as A
import Agda.Syntax.Info
import Agda.Syntax.Internal as I
import Agda.Syntax.Scope.Base
import Agda.Syntax.Position (Position, Range, noRange)
import qualified Agda.Syntax.Position as P
import Agda.Syntax.Fixity
import Agda.Syntax.Notation
import Agda.Syntax.Literal
import qualified Agda.Compiler.JS.Syntax as JS
import qualified Agda.Interaction.Highlighting.Range as HR
import qualified Agda.Interaction.Highlighting.Precise as HP
import Agda.Interaction.FindFile
import qualified Agda.TypeChecking.Monad.Benchmark as Bench
import Agda.TypeChecking.Monad.Benchmark (billSub, billTo)
import Agda.TypeChecking.Monad
import Agda.TypeChecking.CompiledClause
-- import Agda.TypeChecking.Pretty
import Agda.Utils.BiMap (BiMap)
import qualified Agda.Utils.BiMap as BiMap
import Agda.Utils.HashMap (HashMap)
import Agda.Utils.Hash
import qualified Agda.Utils.HashMap as HMap
import Agda.Utils.FileName
import Agda.Utils.IORef
import Agda.Utils.Lens
import Agda.Utils.Monad
import Agda.Utils.Permutation
import Agda.Utils.Except ( ExceptT, MonadError(throwError), runExceptT )
#include "undefined.h"
import Agda.Utils.Impossible
-- | Compatibility with @bytestring < 0.10@ which does not implement
-- @instance NFData@, to support @ghc <= 7.4@.
--
-- Note that we only @deepSeq@ for the purpose of correct benchmarking.
-- Thus, a simply non-forcing @return@ would be a possible implementation.
returnForcedByteString :: Monad m => L.ByteString -> m L.ByteString
#if MIN_VERSION_bytestring(0,10,0)
returnForcedByteString bs = return $!! bs
#else
returnForcedByteString bs = return $! bs
#endif
-- Note that the Binary instance for Int writes 64 bits, but throws
-- away the 32 high bits when reading (at the time of writing, on
-- 32-bit machines). Word64 does not have these problems.
currentInterfaceVersion :: Word64
currentInterfaceVersion = 20141022 * 10 + 0
-- | Constructor tag (maybe omitted) and argument indices.
type Node = [Int32]
-- | The type of hashtables used in this module.
--
-- A very limited amount of testing indicates that 'H.CuckooHashTable'
-- is somewhat slower than 'H.BasicHashTable', and that
-- 'H.LinearHashTable' and the hashtables from "Data.Hashtable" are
-- much slower.
#if defined(mingw32_HOST_OS) && defined(x86_64_HOST_ARCH)
type HashTable k v = H.CuckooHashTable k v
#else
type HashTable k v = H.BasicHashTable k v
#endif
-- | Structure providing fresh identifiers for hash map
-- and counting hash map hits (i.e. when no fresh identifier required).
data FreshAndReuse = FreshAndReuse
{ farFresh :: !Int32 -- ^ Number of hash map misses.
, farReuse :: !Int32 -- ^ Number of hash map hits.
}
farEmpty :: FreshAndReuse
farEmpty = FreshAndReuse 0 0
lensFresh :: Lens' Int32 FreshAndReuse
lensFresh f r = f (farFresh r) <&> \ i -> r { farFresh = i }
lensReuse :: Lens' Int32 FreshAndReuse
lensReuse f r = f (farReuse r) <&> \ i -> r { farReuse = i }
-- | State of the the encoder.
data Dict = Dict
{ nodeD :: !(HashTable Node Int32)
, stringD :: !(HashTable String Int32)
, integerD :: !(HashTable Integer Int32)
, doubleD :: !(HashTable Double Int32)
, termD :: !(HashTable (Ptr Term) Int32)
, nodeC :: !(IORef FreshAndReuse) -- counters for fresh indexes
, stringC :: !(IORef FreshAndReuse)
, integerC :: !(IORef FreshAndReuse)
, doubleC :: !(IORef FreshAndReuse)
, termC :: !(IORef FreshAndReuse)
, stats :: !(HashTable String Int32)
, collectStats :: Bool
-- ^ If @True@ collect in @stats@ the quantities of
-- calls to @icode@ for each @Typeable a@.
, fileMod :: !SourceToModule
}
-- | Creates an empty dictionary.
emptyDict
:: Bool
-- ^ Collect statistics for @icode@ calls?
-> SourceToModule
-- ^ Maps file names to the corresponding module names.
-- Must contain a mapping for every file name that is later encountered.
-> IO Dict
emptyDict collectStats fileMod = Dict
<$> H.new
<*> H.new
<*> H.new
<*> H.new
<*> H.new
<*> newIORef farEmpty
<*> newIORef farEmpty
<*> newIORef farEmpty
<*> newIORef farEmpty
<*> newIORef farEmpty
<*> H.new
<*> pure collectStats
<*> pure fileMod
-- | Universal type, wraps everything.
data U = forall a . Typeable a => U !a
-- | Univeral memo structure, to introduce sharing during decoding
type Memo = HashTable (Int32, TypeRep) U -- (node index, type rep)
-- | State of the decoder.
data St = St
{ nodeE :: !(Array Int32 Node)
, stringE :: !(Array Int32 String)
, integerE :: !(Array Int32 Integer)
, doubleE :: !(Array Int32 Double)
, nodeMemo :: !Memo
, modFile :: !ModuleToSource
-- ^ Maps module names to file names. This is the only component
-- of the state which is updated by the decoder.
, includes :: [AbsolutePath]
-- ^ The include directories.
}
-- | Monad used by the encoder.
type S a = ReaderT Dict IO a
-- | Monad used by the decoder.
--
-- 'TCM' is not used because the associated overheads would make
-- decoding slower.
type R a = ExceptT TypeError (StateT St IO) a
-- | Throws an error which is suitable when the data stream is
-- malformed.
malformed :: R a
malformed = throwError $ GenericError "Malformed input."
class Typeable a => EmbPrj a where
icode :: a -> S Int32 -- ^ Serialization (wrapper).
icod_ :: a -> S Int32 -- ^ Serialization (worker).
value :: Int32 -> R a -- ^ Deserialization.
icode a = do
tickICode a
icod_ a
-- | Increase entry for @a@ in 'stats'.
tickICode :: forall a. Typeable a => a -> S ()
tickICode _ = whenM (asks collectStats) $ do
let key = "icode " ++ show (typeOf (undefined :: a))
hmap <- asks stats
liftIO $ do
n <- fromMaybe 0 <$> H.lookup hmap key
H.insert hmap key $! n + 1
-- | Encodes something. To ensure relocatability file paths in
-- positions are replaced with module names.
encode :: EmbPrj a => a -> TCM L.ByteString
encode a = do
collectStats <- hasVerbosity "profile.serialize" 20
fileMod <- sourceToModule
newD@(Dict nD sD iD dD _ nC sC iC dC tC stats _ _) <- liftIO $
emptyDict collectStats fileMod
root <- liftIO $ runReaderT (icode a) newD
nL <- benchSort $ l nD
sL <- benchSort $ l sD
iL <- benchSort $ l iD
dL <- benchSort $ l dD
-- Record reuse statistics.
verboseS "profile.sharing" 10 $ do
statistics "pointers" tC
verboseS "profile.serialize" 10 $ do
statistics "Integer" iC
statistics "String" sC
statistics "Double" dC
statistics "Node" nC
when collectStats $ do
stats <- Map.fromList . map (second toInteger) <$> do
liftIO $ H.toList stats
modifyStatistics $ Map.union stats
-- Encode hashmaps and root, and compress.
bits1 <- billSub [ Bench.Serialization, Bench.BinaryEncode ] $
returnForcedByteString $ B.encode (root, nL, sL, iL, dL)
let compressParams = G.defaultCompressParams
{ G.compressLevel = G.bestSpeed
, G.compressStrategy = G.huffmanOnlyStrategy
}
cbits <- billSub [ Bench.Serialization, Bench.Compress ] $
returnForcedByteString $ G.compressWith compressParams bits1
let x = B.encode currentInterfaceVersion `L.append` cbits
return x
where
l h = List.map fst . List.sortBy (compare `on` snd) <$> H.toList h
benchSort = billTo [Bench.Serialization, Bench.Sort] . liftIO
statistics :: String -> IORef FreshAndReuse -> TCM ()
statistics kind ioref = do
FreshAndReuse fresh reused <- liftIO $ readIORef ioref
tickN (kind ++ " (fresh)") $ fromIntegral fresh
tickN (kind ++ " (reused)") $ fromIntegral reused
-- encode :: EmbPrj a => a -> TCM L.ByteString
-- encode a = do
-- fileMod <- sourceToModule
-- (x, shared, total) <- liftIO $ do
-- newD@(Dict nD sD iD dD _ _ _ _ _ stats _) <- emptyDict fileMod
-- root <- runReaderT (icode a) newD
-- nL <- l nD; sL <- l sD; iL <- l iD; dL <- l dD
-- (shared, total) <- readIORef stats
-- return (B.encode currentInterfaceVersion `L.append`
-- G.compress (B.encode (root, nL, sL, iL, dL)), shared, total)
-- verboseS "profile.sharing" 10 $ do
-- tickN "pointers (reused)" $ fromIntegral shared
-- tickN "pointers" $ fromIntegral total
-- return x
-- where
-- l h = List.map fst . List.sortBy (compare `on` snd) <$> H.toList h
-- | Data.Binary.runGetState is deprecated in favour of runGetIncremental.
-- Reimplementing it in terms of the new function. The new Decoder type contains
-- strict byte strings so we need to be careful not to feed the entire lazy byte
-- string to the decoder at once.
runGetState :: B.Get a -> L.ByteString -> B.ByteOffset -> (a, L.ByteString, B.ByteOffset)
runGetState g s n = feed (B.runGetIncremental g) (L.toChunks s)
where
feed (B.Done s n' x) ss = (x, L.fromChunks (s : ss), n + n')
feed (B.Fail _ _ err) _ = error err
feed (B.Partial f) (s : ss) = feed (f $ Just s) ss
feed (B.Partial f) [] = feed (f Nothing) []
-- | Decodes something. The result depends on the include path.
--
-- Returns 'Nothing' if the input does not start with the right magic
-- number or some other decoding error is encountered.
decode :: EmbPrj a => L.ByteString -> TCM (Maybe a)
decode s = do
mf <- use stModuleToSource
incs <- getIncludeDirs
-- Note that B.runGetState and G.decompress can raise errors if the
-- input is malformed. The decoder is (intended to be) strict enough
-- to ensure that all such errors can be caught by the handler here.
(mf, r) <- liftIO $ E.handle (\(E.ErrorCall s) -> noResult s) $ do
(ver, s, _) <- return $ runGetState B.get s 0
if ver /= currentInterfaceVersion
then noResult "Wrong interface version."
else do
((r, nL, sL, iL, dL), s, _) <-
return $ runGetState B.get (G.decompress s) 0
if s /= L.empty
-- G.decompress seems to throw away garbage at the end, so
-- the then branch is possibly dead code.
then noResult "Garbage at end."
else do
st <- St (ar nL) (ar sL) (ar iL) (ar dL)
<$> liftIO H.new
<*> return mf <*> return incs
(r, st) <- runStateT (runExceptT (value r)) st
return (Just (modFile st), r)
case mf of
Nothing -> return ()
Just mf -> stModuleToSource .= mf
case r of
Right x -> return (Just x)
Left err -> do
reportSLn "import.iface" 5 $ "Error when decoding interface file"
-- Andreas, 2014-06-11 deactivated debug printing
-- in order to get rid of dependency of Serialize on TCM.Pretty
-- reportSDoc "import.iface" 5 $
-- text "Error when decoding interface file:"
-- $+$ nest 2 (prettyTCM err)
return Nothing
where
ar l = listArray (0, List.genericLength l - 1) l
noResult s = return (Nothing, Left $ GenericError s)
encodeInterface :: Interface -> TCM L.ByteString
encodeInterface i = L.append hashes <$> encode i
where
hashes :: L.ByteString
hashes = B.runPut $ B.put (iSourceHash i) >> B.put (iFullHash i)
-- | Encodes something. To ensure relocatability file paths in
-- positions are replaced with module names.
encodeFile :: FilePath -> Interface -> TCM ()
encodeFile f i = liftIO . L.writeFile f =<< encodeInterface i
-- | Decodes something. The result depends on the include path.
--
-- Returns 'Nothing' if the file does not start with the right magic
-- number or some other decoding error is encountered.
decodeInterface :: L.ByteString -> TCM (Maybe Interface)
decodeInterface s = decode $ L.drop 16 s
decodeHashes :: L.ByteString -> Maybe (Hash, Hash)
decodeHashes s
| L.length s < 16 = Nothing
| otherwise = Just $ B.runGet getH $ L.take 16 s
where getH = (,) <$> B.get <*> B.get
decodeFile :: FilePath -> TCM (Maybe Interface)
decodeFile f = decodeInterface =<< liftIO (L.readFile f)
instance EmbPrj String where
icod_ = icodeString
value i = (! i) `fmap` gets stringE
instance EmbPrj Integer where
icod_ = icodeInteger
value i = (! i) `fmap` gets integerE
instance EmbPrj Word64 where
icod_ i = icode2' (int32 q) (int32 r)
where (q, r) = quotRem i (2^32)
int32 :: Word64 -> Int32
int32 = fromIntegral
value = vcase valu where valu [a, b] = return $ n * mod (fromIntegral a) n + mod (fromIntegral b) n
valu _ = malformed
n = 2^32
instance EmbPrj Int32 where
icod_ i = return i
value i = return i
instance EmbPrj Int where
icod_ i = return (fromIntegral i)
value i = return (fromIntegral i)
instance EmbPrj Char where
icod_ c = return (fromIntegral $ fromEnum c)
value i = return (toEnum $ fromInteger $ toInteger i)
instance EmbPrj Double where
icod_ = icodeDouble
value i = (! i) `fmap` gets doubleE
instance EmbPrj () where
icod_ () = icode0'
value = vcase valu where valu [] = valu0 ()
valu _ = malformed
instance (EmbPrj a, EmbPrj b) => EmbPrj (a, b) where
icod_ (a, b) = icode2' a b
value = vcase valu where valu [a, b] = valu2 (,) a b
valu _ = malformed
instance (EmbPrj a, EmbPrj b, EmbPrj c) => EmbPrj (a, b, c) where
icod_ (a, b, c) = icode3' a b c
value = vcase valu where valu [a, b, c] = valu3 (,,) a b c
valu _ = malformed
instance (EmbPrj a, EmbPrj b) => EmbPrj (Either a b) where
icod_ (Left x) = icode1 0 x
icod_ (Right x) = icode1 1 x
value = vcase valu where valu [0, x] = valu1 Left x
valu [1, x] = valu1 Right x
valu _ = malformed
instance EmbPrj a => EmbPrj (Maybe a) where
icod_ Nothing = icode0'
icod_ (Just x) = icode1' x
value = vcase valu where valu [] = valu0 Nothing
valu [x] = valu1 Just x
valu _ = malformed
instance EmbPrj Bool where
icod_ True = icode0'
icod_ False = icode0 0
value = vcase valu where valu [] = valu0 True
valu [0] = valu0 False
valu _ = malformed
instance EmbPrj AbsolutePath where
icod_ file = do
mm <- Map.lookup file <$> asks fileMod
case mm of
Just m -> icode m
Nothing -> __IMPOSSIBLE__
value m = do
m :: TopLevelModuleName
<- value m
mf <- gets modFile
incs <- gets includes
(r, mf) <- liftIO $ findFile'' incs m mf
modify $ \s -> s { modFile = mf }
case r of
Left err -> throwError $ findErrorToTypeError m err
Right f -> return f
instance EmbPrj Position where
icod_ (P.Pn file pos line col) = icode4' file pos line col
value = vcase valu
where
valu [f, p, l, c] = valu4 P.Pn f p l c
valu _ = malformed
instance EmbPrj TopLevelModuleName where
icod_ (TopLevelModuleName a) = icode1' a
value = vcase valu where valu [a] = valu1 TopLevelModuleName a
valu _ = malformed
instance EmbPrj a => EmbPrj [a] where
icod_ xs = icodeN =<< mapM icode xs
value = vcase (mapM value)
-- icode [] = icode0'
-- icode (x : xs) = icode2' x xs
-- value = vcase valu where valu [] = valu0 []
-- valu [x, xs] = valu2 (:) x xs
-- valu _ = malformed
instance (Ord a, Ord b, EmbPrj a, EmbPrj b) => EmbPrj (BiMap a b) where
icod_ m = icode (BiMap.toList m)
value m = BiMap.fromList <$> value m
instance (Ord a, EmbPrj a, EmbPrj b) => EmbPrj (Map a b) where
icod_ m = icode (Map.toList m)
value m = Map.fromList `fmap` value m
instance (Ord a, EmbPrj a) => EmbPrj (Set a) where
icod_ s = icode (Set.toList s)
value s = Set.fromList `fmap` value s
instance EmbPrj P.Interval where
icod_ (P.Interval p q) = icode2' p q
value = vcase valu where valu [p, q] = valu2 P.Interval p q
valu _ = malformed
instance EmbPrj Range where
icod_ (P.Range is) = icode1' is
value = vcase valu where valu [is] = valu1 P.Range is
valu _ = malformed
instance EmbPrj HR.Range where
icod_ (HR.Range a b) = icode2' a b
value = vcase valu where valu [a, b] = valu2 HR.Range a b
valu _ = malformed
instance EmbPrj C.Name where
icod_ (C.NoName a b) = icode2 0 a b
icod_ (C.Name r xs) = icode2' r xs
value = vcase valu where valu [0, a, b] = valu2 C.NoName a b
valu [r, xs] = valu2 C.Name r xs
valu _ = malformed
instance EmbPrj NamePart where
icod_ Hole = icode0'
icod_ (Id a) = icode1' a
value = vcase valu where valu [] = valu0 Hole
valu [a] = valu1 Id a
valu _ = malformed
instance EmbPrj C.QName where
icod_ (Qual a b) = icode2' a b
icod_ (C.QName a ) = icode1' a
value = vcase valu where valu [a, b] = valu2 Qual a b
valu [a] = valu1 C.QName a
valu _ = malformed
instance EmbPrj Scope where
icod_ (Scope a b c d e) = icode5' a b c d e
value = vcase valu where valu [a, b, c, d, e] = valu5 Scope a b c d e
valu _ = malformed
instance EmbPrj NameSpaceId where
icod_ PublicNS = icode0'
icod_ PrivateNS = icode0 1
icod_ ImportedNS = icode0 2
icod_ OnlyQualifiedNS = icode0 3
value = vcase valu where valu [] = valu0 PublicNS
valu [1] = valu0 PrivateNS
valu [2] = valu0 ImportedNS
valu [3] = valu0 OnlyQualifiedNS
valu _ = malformed
instance EmbPrj Access where
icod_ PrivateAccess = icode0 0
icod_ PublicAccess = icode0'
icod_ OnlyQualified = icode0 2
value = vcase valu where valu [0] = valu0 PrivateAccess
valu [] = valu0 PublicAccess
valu [2] = valu0 OnlyQualified
valu _ = malformed
instance EmbPrj NameSpace where
icod_ (NameSpace a b) = icode2' a b
value = vcase valu where valu [a, b] = valu2 NameSpace a b
valu _ = malformed
instance EmbPrj WhyInScope where
icod_ Defined = icode0'
icod_ (Opened a b) = icode2 0 a b
icod_ (Applied a b) = icode2 1 a b
value = vcase valu where valu [] = valu0 Defined
valu [0, a, b] = valu2 Opened a b
valu [1, a, b] = valu2 Applied a b
valu _ = malformed
instance EmbPrj AbstractName where
icod_ (AbsName a b c) = icode3' a b c
value = vcase valu where valu [a, b, c] = valu3 AbsName a b c
valu _ = malformed
instance EmbPrj AbstractModule where
icod_ (AbsModule a b) = icode2' a b
value = vcase valu where valu [a, b] = valu2 AbsModule a b
valu _ = malformed
instance EmbPrj KindOfName where
icod_ DefName = icode0'
icod_ ConName = icode0 1
icod_ FldName = icode0 2
icod_ PatternSynName = icode0 3
icod_ QuotableName = icode0 4
value = vcase valu where valu [] = valu0 DefName
valu [1] = valu0 ConName
valu [2] = valu0 FldName
valu [3] = valu0 PatternSynName
valu [4] = valu0 QuotableName
valu _ = malformed
instance EmbPrj Agda.Syntax.Fixity.Fixity where
icod_ (LeftAssoc a b) = icode2 0 a b
icod_ (RightAssoc a b) = icode2 1 a b
icod_ (NonAssoc a b) = icode2' a b
value = vcase valu where valu [0, a, b] = valu2 LeftAssoc a b
valu [1, a, b] = valu2 RightAssoc a b
valu [a, b] = valu2 NonAssoc a b
valu _ = malformed
instance EmbPrj Agda.Syntax.Fixity.Fixity' where
icod_ (Fixity' a b) = icode2' a b
value = vcase valu where valu [a,b] = valu2 Fixity' a b
valu _ = malformed
instance EmbPrj GenPart where
icod_ (BindHole a) = icode1 0 a
icod_ (NormalHole a) = icode1 1 a
icod_ (IdPart a) = icode1' a
value = vcase valu where valu [0, a] = valu1 BindHole a
valu [1, a] = valu1 NormalHole a
valu [a] = valu1 IdPart a
valu _ = malformed
instance EmbPrj A.QName where
icod_ (A.QName a b) = icode2' a b
value = vcase valu where valu [a, b] = valu2 A.QName a b
valu _ = malformed
instance EmbPrj A.AmbiguousQName where
icod_ (A.AmbQ a) = icode a
value n = A.AmbQ `fmap` value n
instance EmbPrj A.ModuleName where
icod_ (A.MName a) = icode a
value n = A.MName `fmap` value n
instance EmbPrj A.Name where
icod_ (A.Name a b c d) = icode4' a b c d
value = vcase valu where valu [a, b, c, d] = valu4 A.Name a b c d
valu _ = malformed
instance EmbPrj A.Assign where
icod_ (A.Assign a b) = icode2' a b
value = vcase valu where valu [a, b] = valu2 A.Assign a b
valu _ = malformed
instance (EmbPrj s, EmbPrj t) => EmbPrj (Named s t) where
icod_ (Named a b) = icode2' a b
value = vcase valu where valu [a, b] = valu2 Named a b
valu _ = malformed
instance EmbPrj a => EmbPrj (Ranged a) where
icod_ (Ranged r x) = icode2' r x
value = vcase valu where valu [r, x] = valu2 Ranged r x
valu _ = malformed
instance EmbPrj LocalVar where
icod_ (LocalVar a) = icode1' a
icod_ (ShadowedVar a b) = icode2' a b
value = vcase valu where valu [a] = valu1 LocalVar a
valu [a, b] = valu2 ShadowedVar a b
valu _ = malformed
-- Only used for pattern synonyms
instance EmbPrj A.Expr where
icod_ (A.Var n) = icode1 0 n
icod_ (A.Def n) = icode1 1 n
icod_ (A.Con ns) = icode1 2 ns
icod_ (A.Lit l) = icode1 3 l
icod_ (A.QuestionMark{}) = icode0 4
icod_ (A.Underscore _) = icode0 5
icod_ (A.App _ a b) = icode2 6 a b
icod_ (A.WithApp _ a b) = icode2 7 a b
icod_ (A.Lam _ a b) = icode2 8 a b
icod_ (A.AbsurdLam _ a) = icode1 9 a
icod_ (A.ExtendedLam _ _ _ _) = __IMPOSSIBLE__
icod_ (A.Pi _ a b) = icode2 11 a b
icod_ (A.Fun _ a b) = icode2 12 a b
icod_ (A.Set _ a) = icode1 13 a
icod_ (A.Prop _) = icode0 14
icod_ (A.Let _ _ _) = __IMPOSSIBLE__
icod_ (A.ETel a) = icode1 16 a
icod_ (A.Rec _ a) = icode1 17 a
icod_ (A.RecUpdate _ a b) = icode2 18 a b
icod_ (A.ScopedExpr a b) = icode2 19 a b
icod_ (A.QuoteGoal _ a b) = icode2 20 a b
icod_ (A.QuoteContext _) = icode0 21
icod_ (A.Quote _) = icode0 22
icod_ (A.QuoteTerm _) = icode0 23
icod_ (A.Unquote _) = icode0 24
icod_ (A.Tactic _ _ _ _) = __IMPOSSIBLE__
icod_ (A.DontCare a) = icode1 25 a
icod_ (A.PatternSyn a) = icode1 26 a
icod_ (A.Proj a) = icode1 27 a
value = vcase valu
where
valu [0, a] = valu1 A.Var a
valu [1, a] = valu1 A.Def a
valu [2, a] = valu1 A.Con a
valu [3, a] = valu1 A.Lit a
valu [4] = valu0 (A.QuestionMark emptyMetaInfo 0)
valu [5] = valu0 (A.Underscore emptyMetaInfo)
valu [6, a, b] = valu2 (A.App i) a b
valu [7, a, b] = valu2 (A.WithApp i) a b
valu [8, a, b] = valu2 (A.Lam i) a b
valu [9, a] = valu1 (A.AbsurdLam i) a
valu [11, a, b] = valu2 (A.Pi i) a b
valu [12, a, b] = valu2 (A.Fun i) a b
valu [13, a] = valu1 (A.Set i) a
valu [14] = valu0 (A.Prop i)
valu [16, a] = valu1 A.ETel a
valu [17, a] = valu1 (A.Rec i) a
valu [18, a, b] = valu2 (A.RecUpdate i) a b
valu [19, a, b] = valu2 A.ScopedExpr a b
valu [20, a, b] = valu2 (A.QuoteGoal i) a b
valu [21] = valu0 (A.QuoteContext i)
valu [22] = valu0 (A.Quote i)
valu [23] = valu0 (A.QuoteTerm i)
valu [24] = valu0 (A.Unquote i)
valu [25, a] = valu1 A.DontCare a
valu [26, a] = valu1 A.PatternSyn a
valu [27, a] = valu1 A.Proj a
valu _ = malformed
i = ExprRange noRange
instance EmbPrj A.Pattern where
icod_ (A.VarP a) = icode1 0 a
icod_ (A.ConP _ a b) = icode2 1 a b
icod_ (A.DefP _ a b) = icode2 2 a b
icod_ (A.WildP _) = icode0 3
icod_ (A.AsP _ a b) = icode2 4 a b
icod_ (A.DotP _ a) = icode1 5 a
icod_ (A.AbsurdP _) = icode0 6
icod_ (A.LitP a) = icode1 7 a
icod_ (A.ImplicitP _) = icode0 8
icod_ (A.PatternSynP _ a b) = icode2 9 a b
value = vcase valu
where
valu [0, a] = valu1 A.VarP a
valu [1, a, b] = valu2 (A.ConP (ConPatInfo False i)) a b
valu [2, a, b] = valu2 (A.DefP i) a b
valu [3] = valu0 (A.WildP i)
valu [4, a, b] = valu2 (A.AsP i) a b
valu [5, a] = valu1 (A.DotP i) a
valu [6] = valu0 (A.AbsurdP i)
valu [7, a] = valu1 (A.LitP) a
valu [8] = valu0 (A.ImplicitP i)
valu [9, a, b] = valu2 (A.PatternSynP i) a b
valu _ = malformed
i = patNoRange
instance EmbPrj A.LamBinding where
icod_ (A.DomainFree i e) = icode2 0 i e
icod_ (A.DomainFull a) = icode1 1 a
value = vcase valu where valu [0, i, e] = valu2 A.DomainFree i e
valu [1, a] = valu1 A.DomainFull a
valu _ = malformed
instance EmbPrj A.LetBinding where
icod_ (A.LetBind _ a b c d) = icode4 0 a b c d
icod_ (A.LetPatBind _ a b ) = icode2 1 a b
icod_ (A.LetApply _ _ _ _ _) = icode0 2
icod_ (A.LetOpen _ _) = icode0 2
value = vcase valu
where
valu [0, a, b, c, d] = valu4 (A.LetBind (LetRange noRange)) a b c d
valu [1, a, b] = valu2 (A.LetPatBind (LetRange noRange)) a b
valu [2] = throwError $ NotSupported
"importing pattern synonym containing let module"
valu _ = malformed
instance EmbPrj A.TypedBindings where
icod_ (A.TypedBindings a b) = icode2' a b
value = vcase valu where valu [a, b] = valu2 A.TypedBindings a b
valu _ = malformed
instance EmbPrj A.TypedBinding where
icod_ (A.TBind a b c) = icode3 0 a b c
icod_ (A.TLet a b) = icode2 1 a b
value = vcase valu where valu [0, a, b, c] = valu3 A.TBind a b c
valu [1, a, b] = valu2 A.TLet a b
valu _ = malformed
instance EmbPrj c => EmbPrj (Agda.Syntax.Common.ArgInfo c) where
icod_ (ArgInfo h r cs) = icode3' h r cs
value = vcase valu where valu [h, r, cs] = valu3 ArgInfo h r cs
valu _ = malformed
instance EmbPrj NameId where
icod_ (NameId a b) = icode2' a b
value = vcase valu where valu [a, b] = valu2 NameId a b
valu _ = malformed
instance EmbPrj Signature where
icod_ (Sig a b) = icode2' a b
value = vcase valu where valu [a, b] = valu2 Sig a b
valu _ = malformed
instance (Eq k, Hashable k, EmbPrj k, EmbPrj v) => EmbPrj (HashMap k v) where
icod_ m = icode (HMap.toList m)
value m = HMap.fromList `fmap` value m
instance EmbPrj Section where
icod_ (Section a b) = icode2' a b
value = vcase valu where valu [a, b] = valu2 Section a b
valu _ = malformed
instance EmbPrj Telescope where
icod_ EmptyTel = icode0'
icod_ (ExtendTel a b) = icode2' a b
value = vcase valu where valu [] = valu0 EmptyTel
valu [a, b] = valu2 ExtendTel a b
valu _ = malformed
instance EmbPrj Permutation where
icod_ (Perm a b) = icode2' a b
value = vcase valu where valu [a, b] = valu2 Perm a b
valu _ = malformed
instance EmbPrj a => EmbPrj (Drop a) where
icod_ (Drop a b) = icode2' a b
value = vcase valu where valu [a, b] = valu2 Drop a b
valu _ = malformed
instance EmbPrj a => EmbPrj (Elim' a) where
icod_ (Apply a) = icode1' a
icod_ (Proj a) = icode1 0 a
value = vcase valu where valu [a] = valu1 Apply a
valu [0, a] = valu1 Proj a
valu _ = malformed
instance (EmbPrj a, EmbPrj c) => EmbPrj (Agda.Syntax.Common.Arg c a) where
icod_ (Arg i e) = icode2' i e
value = vcase valu where valu [i, e] = valu2 Arg i e
valu _ = malformed
instance (EmbPrj a, EmbPrj c) => EmbPrj (Agda.Syntax.Common.Dom c a) where
icod_ (Dom i e) = icode2' i e
value = vcase valu where valu [i, e] = valu2 Dom i e
valu _ = malformed
instance EmbPrj Agda.Syntax.Common.Induction where
icod_ Inductive = icode0'
icod_ CoInductive = icode0 1
value = vcase valu where valu [] = valu0 Inductive
valu [1] = valu0 CoInductive
valu _ = malformed
instance EmbPrj Agda.Syntax.Common.Hiding where
icod_ Hidden = icode0 0
icod_ NotHidden = icode0'
icod_ Instance = icode0 2
value = vcase valu where valu [0] = valu0 Hidden
valu [] = valu0 NotHidden
valu [2] = valu0 Instance
valu _ = malformed
instance EmbPrj Agda.Syntax.Common.Relevance where
icod_ Relevant = icode0'
icod_ Irrelevant = icode0 1
icod_ Forced = icode0 2
icod_ NonStrict = icode0 3
icod_ UnusedArg = icode0 4
value = vcase valu where valu [] = valu0 Relevant
valu [1] = valu0 Irrelevant
valu [2] = valu0 Forced
valu [3] = valu0 NonStrict
valu [4] = valu0 UnusedArg
valu _ = malformed
instance EmbPrj I.ConHead where
icod_ (ConHead a b c) = icode3' a b c
value = vcase valu where valu [a, b, c] = valu3 ConHead a b c
valu _ = malformed
instance EmbPrj I.Type where
icod_ (El a b) = icode2' a b
value = vcase valu where valu [a, b] = valu2 El a b
valu _ = malformed
instance (EmbPrj a) => EmbPrj (I.Abs a) where
icod_ (NoAbs a b) = icode2 0 a b
icod_ (Abs a b) = icode2' a b
value = vcase valu where valu [a, b] = valu2 Abs a b
valu [0, a, b] = valu2 NoAbs a b
valu _ = malformed
instance EmbPrj I.Term where
icod_ (Var a b) = icode2 0 a b
icod_ (Lam a b) = icode2 1 a b
icod_ (Lit a ) = icode1 2 a
icod_ (Def a b) = icode2 3 a b
icod_ (Con a b) = icode2 4 a b
icod_ (Pi a b) = icode2 5 a b
icod_ (Sort a ) = icode1 7 a
icod_ (MetaV a b) = __IMPOSSIBLE__
icod_ ExtLam{} = __IMPOSSIBLE__
icod_ (DontCare a ) = icode1 8 a
icod_ (Level a ) = icode1 9 a
icod_ (Shared p) = do
h <- asks termD
mi <- liftIO $ H.lookup h p
st <- asks termC
case mi of
Just i -> liftIO $ do
modifyIORef' st $ over lensReuse (+ 1)
return i
Nothing -> do
liftIO $ modifyIORef' st $ over lensFresh (+1)
n <- icode (derefPtr p)
liftIO $ H.insert h p n
return n
value r = vcase valu' r
where
valu' xs = shared <$> valu xs
valu [0, a, b] = valu2 Var a b
valu [1, a, b] = valu2 Lam a b
valu [2, a] = valu1 Lit a
valu [3, a, b] = valu2 Def a b
valu [4, a, b] = valu2 Con a b
valu [5, a, b] = valu2 Pi a b
valu [7, a] = valu1 Sort a
valu [8, a] = valu1 DontCare a
valu [9, a] = valu1 Level a
valu _ = malformed
instance EmbPrj Level where
icod_ (Max a) = icode1' a
value = vcase valu where valu [a] = valu1 Max a
valu _ = malformed
instance EmbPrj PlusLevel where
icod_ (ClosedLevel a) = icode1' a
icod_ (Plus a b) = icode2' a b
value = vcase valu where valu [a] = valu1 ClosedLevel a
valu [a, b] = valu2 Plus a b
valu _ = malformed
instance EmbPrj LevelAtom where
icod_ (NeutralLevel a) = icode1' a
icod_ (UnreducedLevel a) = icode1 1 a
icod_ MetaLevel{} = __IMPOSSIBLE__
icod_ BlockedLevel{} = __IMPOSSIBLE__
value = vcase valu where valu [a] = valu1 NeutralLevel a
valu [1, a] = valu1 UnreducedLevel a
valu _ = malformed
instance EmbPrj I.Sort where
icod_ (Type a ) = icode1' a
icod_ Prop = icode1 1 ()
icod_ Inf = icode1 4 ()
icod_ (DLub a b) = __IMPOSSIBLE__
value = vcase valu where valu [a] = valu1 Type a
valu [1, _] = valu0 Prop
valu [4, _] = valu0 Inf
valu _ = malformed
instance EmbPrj Agda.Syntax.Literal.Literal where
icod_ (LitInt a b) = icode2' a b
icod_ (LitFloat a b) = icode2 1 a b
icod_ (LitString a b) = icode2 2 a b
icod_ (LitChar a b) = icode2 3 a b
icod_ (LitQName a b) = icode2 5 a b
value = vcase valu where valu [a, b] = valu2 LitInt a b
valu [1, a, b] = valu2 LitFloat a b
valu [2, a, b] = valu2 LitString a b