-
Notifications
You must be signed in to change notification settings - Fork 100
/
Term.hs
1625 lines (1372 loc) · 51.2 KB
/
Term.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
{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ViewPatterns #-}
-- Required for GHC >= 9
{-# OPTIONS_GHC -Wno-redundant-constraints #-}
-- |
-- Module : Pact.Types.Term
-- Copyright : (C) 2016 Stuart Popejoy
-- License : BSD-style (see the file LICENSE)
-- Maintainer : Stuart Popejoy <stuart@kadena.io>
--
-- Term and related types.
--
module Pact.Types.Term
( Namespace(..), nsName, nsUser, nsAdmin,
Meta(..),mDocs,mModel,
PublicKey(..),
KeySet(..), mkKeySet,
KeySetName(..),
PactGuard(..),
PactId(..),
UserGuard(..),
ModuleGuard(..),
Guard(..),_GPact,_GKeySet,_GKeySetRef,_GModule,_GUser,
DefType(..),_Defun,_Defpact,_Defcap,
defTypeRep,
FunApp(..),faDefType,faDocs,faInfo,faModule,faName,faTypes,
Ref'(..),_Direct,_Ref,Ref,
NativeDFun(..),
BindType(..),
BindPair(..),bpArg,bpVal,toBindPairs,
Module(..),mName,mGovernance,mMeta,mCode,mHash,mBlessed,mInterfaces,mImports,
Interface(..),interfaceCode, interfaceMeta, interfaceName, interfaceImports,
ModuleDef(..),_MDModule,_MDInterface,moduleDefName,moduleDefCode,moduleDefMeta,
Governance(..),
ModuleHash(..), mhHash,
ConstVal(..),constTerm,
Use(..),
App(..),appFun,appArgs,appInfo,
Def(..),dDefBody,dDefName,dDefType,dMeta,dFunType,dInfo,dModule,dDefMeta,
Lam(..), lamArg, lamBindBody, lamTy, lamInfo,
DefMeta(..),
DefcapMeta(..),
Example(..),
derefDef,
ObjectMap(..),objectMapToListWith,
Object(..),oObject,oObjectType,oInfo,oKeyOrder,
FieldKey(..),
Step(..),sEntity,sExec,sRollback,sInfo,
ModRef(..),modRefName,modRefSpec,modRefInfo,
modRefTy,
Term(..),
tApp,tBindBody,tBindPairs,tBindType,tConstArg,tConstVal,
tDef,tMeta,tFields,tFunTypes,tHash,tInfo,tGuard,
tListType,tList,tLiteral,tModuleBody,tModuleDef,tModule,tUse,
tNativeDocs,tNativeFun,tNativeName,tNativeExamples,
tNativeTopLevelOnly,tObject,tSchemaName,
tTableName,tTableType,tVar,tStep,tModuleName,
tDynModRef,tDynMember,tModRef,tLam,
_TModule, _TList, _TDef, _TNative, _TConst, _TApp,
_TVar, _TBinding, _TLam, _TObject, _TSchema,
_TLiteral, _TGuard, _TUse, _TStep, _TModRef,
_TTable, _TDynamic,
ToTerm(..),
toTermList,toTObject,toTObjectMap,toTList,toTListV,
typeof,typeof',guardTypeOf,
canUnifyWith,
prettyTypeTerm,
pattern TLitString,pattern TLitInteger,pattern TLitBool,
tLit,tStr,termEq,termEq1,termRefEq,canEq,refEq,
Gas(..),
module Pact.Types.Names
) where
import Bound
import Control.Applicative
import Control.Arrow ((&&&))
import Control.DeepSeq
import Control.Lens hiding ((.=), DefName(..))
import Control.Monad
#if MIN_VERSION_aeson(1,4,3)
import Data.Aeson hiding (pairs,Object, (<?>))
#else
import Data.Aeson hiding (pairs,Object)
#endif
import Data.Decimal
import Data.Default
import Data.Eq.Deriving
import Data.Foldable
import Data.Functor.Classes (Eq1(..), Show1(..))
import Data.Function
import qualified Data.HashMap.Strict as HM
import qualified Data.HashSet as HS
import Data.Hashable
import Data.Int (Int64)
import Data.List
import qualified Data.Map.Strict as M
import Data.Maybe
import Data.Serialize (Serialize)
import Data.String
import Data.Text (Text)
import qualified Data.Text as T
import Pact.Time (UTCTime)
import Data.Vector (Vector)
import qualified Data.Vector as V
import Data.Word (Word64, Word32)
import GHC.Generics (Generic)
import Prelude
import Test.QuickCheck hiding (Success)
import Text.Show.Deriving
import Pact.Types.Codec
import Pact.Types.Exp
import Pact.Types.Hash
import Pact.Types.Info
import Pact.Types.KeySet
import Pact.Types.Names
import Pact.Types.Pretty hiding (dot)
import Pact.Types.SizeOf
import Pact.Types.Type
import Pact.Types.Util
-- -------------------------------------------------------------------------- --
-- Meta
data Meta = Meta
{ _mDocs :: !(Maybe Text) -- ^ docs
, _mModel :: ![Exp Info] -- ^ models
} deriving (Eq, Show, Generic)
instance Pretty Meta where
pretty (Meta (Just doc) model) = dquotes (pretty doc) <> line <> prettyModel model
pretty (Meta Nothing model) = prettyModel model
instance NFData Meta
instance SizeOf Meta
prettyModel :: [Exp Info] -> Doc
prettyModel [] = mempty
prettyModel props = "@model " <> list (pretty <$> props)
instance ToJSON Meta where toJSON = lensyToJSON 2
instance FromJSON Meta where parseJSON = lensyParseJSON 2
instance Default Meta where def = Meta def def
instance Semigroup Meta where
(Meta d m) <> (Meta d' m') = Meta (d <> d') (m <> m')
instance Monoid Meta where
mempty = Meta Nothing []
-- -------------------------------------------------------------------------- --
-- PactId
newtype PactId = PactId Text
deriving (Eq,Ord,Show,Pretty,AsString,IsString,FromJSON,ToJSON, FromJSONKey, ToJSONKey, Generic,NFData,SizeOf)
instance Arbitrary PactId where
arbitrary = PactId <$> hashToText <$> arbitrary
-- -------------------------------------------------------------------------- --
-- UserGuard
data UserGuard a = UserGuard
{ _ugFun :: !Name
, _ugArgs :: ![a]
} deriving (Eq,Generic,Show,Functor,Foldable,Traversable,Ord)
instance (Arbitrary a) => Arbitrary (UserGuard a) where
arbitrary = UserGuard <$> arbitrary <*> arbitrary
instance NFData a => NFData (UserGuard a)
instance Pretty a => Pretty (UserGuard a) where
pretty UserGuard{..} = "UserGuard" <+> commaBraces
[ "fun: " <> pretty _ugFun
, "args: " <> pretty _ugArgs
]
instance (SizeOf p) => SizeOf (UserGuard p) where
sizeOf (UserGuard n arr) =
(constructorCost 2) + (sizeOf n) + (sizeOf arr)
instance ToJSON a => ToJSON (UserGuard a) where toJSON = lensyToJSON 3
instance FromJSON a => FromJSON (UserGuard a) where parseJSON = lensyParseJSON 3
-- -------------------------------------------------------------------------- --
-- DefType
data DefType
= Defun
| Defpact
| Defcap
deriving (Eq,Show,Generic, Bounded, Enum)
instance FromJSON DefType
instance ToJSON DefType
instance NFData DefType
instance SizeOf DefType where
sizeOf _ = 0
defTypeRep :: DefType -> String
defTypeRep Defun = "defun"
defTypeRep Defpact = "defpact"
defTypeRep Defcap = "defcap"
instance Pretty DefType where
pretty = prettyString . defTypeRep
-- -------------------------------------------------------------------------- --
-- Gas
-- | Gas compute cost unit.
newtype Gas = Gas Int64
deriving (Eq,Ord,Num,Real,Integral,Enum,ToJSON,FromJSON,Generic,NFData)
instance Show Gas where show (Gas g) = show g
instance Pretty Gas where
pretty (Gas i) = pretty i
instance Semigroup Gas where
(Gas a) <> (Gas b) = Gas $ a + b
instance Monoid Gas where
mempty = 0
-- -------------------------------------------------------------------------- --
-- BindType
-- | Binding forms.
data BindType n =
-- | Normal "let" bind
BindLet |
-- | Schema-style binding, with string value for key
BindSchema { _bType :: !n }
deriving (Eq,Functor,Foldable,Traversable,Ord,Show,Generic)
instance (Pretty n) => Pretty (BindType n) where
pretty BindLet = "let"
pretty (BindSchema b) = "bind" <> pretty b
instance ToJSON n => ToJSON (BindType n) where
toJSON BindLet = "let"
toJSON (BindSchema s) = object [ "bind" .= s ]
instance FromJSON n => FromJSON (BindType n) where
parseJSON v =
withThisText "BindLet" "let" v (pure BindLet) <|>
withObject "BindSchema" (\o -> BindSchema <$> o .: "bind") v
instance NFData n => NFData (BindType n)
instance (SizeOf n) => SizeOf (BindType n)
-- -------------------------------------------------------------------------- --
-- BindPair
data BindPair n = BindPair
{ _bpArg :: !(Arg n)
, _bpVal :: !n }
deriving (Eq,Show,Functor,Traversable,Foldable,Generic)
toBindPairs :: BindPair n -> (Arg n,n)
toBindPairs (BindPair a v) = (a,v)
instance Pretty n => Pretty (BindPair n) where
pretty (BindPair arg body) = pretty arg <+> pretty body
instance NFData n => NFData (BindPair n)
instance ToJSON n => ToJSON (BindPair n) where toJSON = lensyToJSON 3
instance FromJSON n => FromJSON (BindPair n) where parseJSON = lensyParseJSON 3
instance (SizeOf n) => SizeOf (BindPair n)
-- -------------------------------------------------------------------------- --
-- App
data App t = App
{ _appFun :: !t
, _appArgs :: ![t]
, _appInfo :: !Info
} deriving (Functor,Foldable,Traversable,Eq,Show,Generic)
instance HasInfo (App t) where getInfo = _appInfo
instance ToJSON t => ToJSON (App t) where toJSON = lensyToJSON 4
instance FromJSON t => FromJSON (App t) where parseJSON = lensyParseJSON 4
instance Pretty n => Pretty (App n) where
pretty App{..} = parensSep $ pretty _appFun : map pretty _appArgs
instance NFData t => NFData (App t)
instance (SizeOf t) => SizeOf (App t)
-- -------------------------------------------------------------------------- --
-- Governance
newtype Governance g = Governance { _gGovernance :: Either KeySetName g }
deriving (Eq,Ord,Functor,Foldable,Traversable,Show,NFData, Generic)
instance (SizeOf g) => SizeOf (Governance g)
instance Pretty g => Pretty (Governance g) where
pretty = \case
Governance (Left k) -> pretty k
Governance (Right r) -> pretty r
instance ToJSON g => ToJSON (Governance g) where
toJSON (Governance g) = case g of
Left ks -> object [ "keyset" .= ks ]
Right c -> object [ "capability" .= c ]
instance FromJSON g => FromJSON (Governance g) where
parseJSON = withObject "Governance" $ \o ->
Governance <$> (Left <$> o .: "keyset" <|>
Right <$> o .: "capability")
-- -------------------------------------------------------------------------- --
-- ModuleHash
-- | Newtype wrapper differentiating 'Hash'es from module hashes
--
newtype ModuleHash = ModuleHash { _mhHash :: Hash }
deriving (Eq, Ord, Show, Generic, Hashable, Serialize, AsString, Pretty, ToJSON, FromJSON, ParseText)
deriving newtype (NFData, SizeOf)
instance Arbitrary ModuleHash where
-- Coin contract is about 20K characters
arbitrary = ModuleHash <$> resize 20000 arbitrary
-- -------------------------------------------------------------------------- --
-- DefcapMeta
-- | Metadata specific to Defcaps.
data DefcapMeta n =
DefcapManaged
{ _dcManaged :: !(Maybe (Text,n))
-- ^ "Auto" managed or user-managed by (param,function)
} |
DefcapEvent
-- ^ Eventing defcap.
deriving (Functor,Foldable,Traversable,Generic,Eq,Show,Ord)
instance NFData n => NFData (DefcapMeta n)
instance Pretty n => Pretty (DefcapMeta n) where
pretty (DefcapManaged m) = case m of
Nothing -> tag
Just (p,f) -> tag <> " " <> pretty p <> " " <> pretty f
where
tag = "@managed"
pretty DefcapEvent = "@event"
instance (ToJSON n,FromJSON n) => ToJSON (DefcapMeta n) where
toJSON (DefcapManaged (Just (p,f))) = object
[ "managerFun" .= f
, "managedParam" .= p
]
toJSON (DefcapManaged Nothing) = object [ "managerAuto" .= True ]
toJSON DefcapEvent = "event"
instance (ToJSON n,FromJSON n) => FromJSON (DefcapMeta n) where
parseJSON v = parseUser v <|> parseAuto v <|> parseEvent v
where
parseUser = withObject "DefcapMeta" $ \o -> (DefcapManaged . Just) <$>
((,) <$> o .: "managedParam" <*> o .: "managerFun")
parseAuto = withObject "DefcapMeta" $ \o -> do
b <- o .: "managerAuto"
if b then pure (DefcapManaged Nothing)
else fail "Expected True"
parseEvent = withText "DefcapMeta" $ \t ->
if t == "event" then pure DefcapEvent
else fail "Expected 'event'"
instance (SizeOf a) => SizeOf (DefcapMeta a)
-- | Def metadata specific to 'DefType'.
-- Currently only specified for Defcap.
data DefMeta n =
DMDefcap !(DefcapMeta n)
deriving (Functor,Foldable,Traversable,Generic,Eq,Show,Ord)
instance NFData n => NFData (DefMeta n)
instance Pretty n => Pretty (DefMeta n) where
pretty (DMDefcap m) = pretty m
instance (ToJSON n,FromJSON n) => ToJSON (DefMeta n) where
toJSON (DMDefcap m) = toJSON m
instance (ToJSON n,FromJSON n) => FromJSON (DefMeta n) where
parseJSON = fmap DMDefcap . parseJSON
instance (SizeOf a) => SizeOf (DefMeta a)
-- -------------------------------------------------------------------------- --
-- ConstVal
data ConstVal n =
CVRaw { _cvRaw :: !n } |
CVEval { _cvRaw :: !n
, _cvEval :: !n }
deriving (Eq,Functor,Foldable,Traversable,Generic,Show)
instance NFData n => NFData (ConstVal n)
instance ToJSON n => ToJSON (ConstVal n) where
toJSON (CVRaw n) = object [ "raw" .= n ]
toJSON (CVEval n m) = object [ "raw" .= n, "eval" .= m ]
instance FromJSON n => FromJSON (ConstVal n) where
parseJSON v =
(withObject "CVEval"
(\o -> CVEval <$> o .: "raw" <*> o .: "eval") v) <|>
(withObject "CVRaw"
(\o -> CVRaw <$> o .: "raw") v)
-- | A term from a 'ConstVal', preferring evaluated terms when available.
constTerm :: ConstVal a -> a
constTerm (CVRaw raw) = raw
constTerm (CVEval _raw eval) = eval
instance (SizeOf n) => SizeOf (ConstVal n)
-- -------------------------------------------------------------------------- --
-- Example
data Example
= ExecExample !Text
-- ^ An example shown as a good execution
| ExecErrExample !Text
-- ^ An example shown as a failing execution
| LitExample !Text
-- ^ An example shown as a literal
deriving (Eq, Show, Generic)
instance Pretty Example where
pretty = \case
ExecExample str -> annotate Example $ "> " <> pretty str
ExecErrExample str -> annotate BadExample $ "> " <> pretty str
LitExample str -> annotate Example $ pretty str
instance IsString Example where
fromString = ExecExample . fromString
instance NFData Example
instance SizeOf Example
-- -------------------------------------------------------------------------- --
-- FieldKey
-- | Label type for objects.
newtype FieldKey = FieldKey Text
deriving (Eq,Ord,IsString,AsString,ToJSON,FromJSON,Show,NFData,Generic,ToJSONKey,SizeOf)
instance Pretty FieldKey where
pretty (FieldKey k) = dquotes $ pretty k
instance Arbitrary FieldKey where
arbitrary = resize 50 (FieldKey <$> genBareText)
-- -------------------------------------------------------------------------- --
-- Step
data Step n = Step
{ _sEntity :: !(Maybe n)
, _sExec :: !n
, _sRollback :: !(Maybe n)
, _sInfo :: !Info
} deriving (Eq,Show,Generic,Functor,Foldable,Traversable)
instance NFData n => NFData (Step n)
instance ToJSON n => ToJSON (Step n) where toJSON = lensyToJSON 2
instance FromJSON n => FromJSON (Step n) where parseJSON = lensyParseJSON 2
instance HasInfo (Step n) where getInfo = _sInfo
instance Pretty n => Pretty (Step n) where
pretty = \case
Step mEntity exec Nothing _i -> parensSep $
[ "step"
] ++ maybe [] (\entity -> [pretty entity]) mEntity ++
[ pretty exec
]
Step mEntity exec (Just rollback) _i -> parensSep $
[ "step-with-rollback"
] ++ maybe [] (\entity -> [pretty entity]) mEntity ++
[ pretty exec
, pretty rollback
]
instance (SizeOf n) => SizeOf (Step n)
-- -------------------------------------------------------------------------- --
-- ModRef
-- | A reference to a module or interface.
data ModRef = ModRef
{ _modRefName :: !ModuleName
-- ^ Fully-qualified module name.
, _modRefSpec :: !(Maybe [ModuleName])
-- ^ Specification: for modules, 'Just' implemented interfaces;
-- for interfaces, 'Nothing'.
, _modRefInfo :: !Info
} deriving (Eq,Show,Generic)
instance NFData ModRef
instance HasInfo ModRef where getInfo = _modRefInfo
instance Pretty ModRef where
pretty (ModRef mn _sm _i) = pretty mn
instance ToJSON ModRef where toJSON = lensyToJSON 4
instance FromJSON ModRef where parseJSON = lensyParseJSON 4
instance Ord ModRef where
(ModRef a b _) `compare` (ModRef c d _) = (a,b) `compare` (c,d)
instance Arbitrary ModRef where
arbitrary = ModRef <$> arbitrary <*> arbitrary <*> pure def
instance SizeOf ModRef where
sizeOf (ModRef n s _) = constructorCost 1 + sizeOf n + sizeOf s
-- -------------------------------------------------------------------------- --
-- ModuleGuard
data ModuleGuard = ModuleGuard
{ _mgModuleName :: !ModuleName
, _mgName :: !Text
} deriving (Eq,Generic,Show,Ord)
instance Arbitrary ModuleGuard where
arbitrary = ModuleGuard <$> arbitrary <*> genBareText
instance NFData ModuleGuard
instance Pretty ModuleGuard where
pretty ModuleGuard{..} = "ModuleGuard" <+> commaBraces
[ "module: " <> pretty _mgModuleName
, "name: " <> pretty _mgName
]
instance SizeOf ModuleGuard where
sizeOf (ModuleGuard md n) =
(constructorCost 2) + (sizeOf md) + (sizeOf n)
instance ToJSON ModuleGuard where toJSON = lensyToJSON 3
instance FromJSON ModuleGuard where parseJSON = lensyParseJSON 3
-- -------------------------------------------------------------------------- --
-- PactGuard
data PactGuard = PactGuard
{ _pgPactId :: !PactId
, _pgName :: !Text
} deriving (Eq,Generic,Show,Ord)
instance Arbitrary PactGuard where
arbitrary = PactGuard <$> arbitrary <*> genBareText
instance NFData PactGuard
instance Pretty PactGuard where
pretty PactGuard{..} = "PactGuard" <+> commaBraces
[ "pactId: " <> pretty _pgPactId
, "name: " <> pretty _pgName
]
instance SizeOf PactGuard where
sizeOf (PactGuard pid pn) =
(constructorCost 2) + (sizeOf pid) + (sizeOf pn)
instance ToJSON PactGuard where toJSON = lensyToJSON 3
instance FromJSON PactGuard where parseJSON = lensyParseJSON 3
-- -------------------------------------------------------------------------- --
-- ObjectMap
-- | Simple dictionary for object values.
newtype ObjectMap v = ObjectMap { _objectMap :: (M.Map FieldKey v) }
deriving (Eq,Ord,Show,Functor,Foldable,Traversable,Generic,SizeOf)
instance NFData v => NFData (ObjectMap v)
-- potentially long output due to constrained recursion of PactValue
instance (Eq v, FromJSON v, ToJSON v, Arbitrary v) => Arbitrary (ObjectMap v) where
arbitrary = ObjectMap <$> M.fromList <$> listOf1 arbitrary
-- | O(n) conversion to list. Adapted from 'M.toAscList'
objectMapToListWith :: (FieldKey -> v -> r) -> ObjectMap v -> [r]
objectMapToListWith f (ObjectMap m) = M.foldrWithKey (\k x xs -> (f k x):xs) [] m
instance Pretty v => Pretty (ObjectMap v) where
pretty om = annotate Val $ commaBraces $
objectMapToListWith (\k v -> pretty k <> ": " <> pretty v) om
instance ToJSON v => ToJSON (ObjectMap v)
where toJSON om =
object $ objectMapToListWith (\k v -> (asString k,toJSON v)) om
instance FromJSON v => FromJSON (ObjectMap v)
where parseJSON = withObject "ObjectMap" $ \o ->
ObjectMap . M.fromList <$>
traverse (\(k,v) -> (FieldKey k,) <$> parseJSON v) (HM.toList o)
instance Default (ObjectMap v) where
def = ObjectMap M.empty
-- -------------------------------------------------------------------------- --
-- Use
data Use = Use
{ _uModuleName :: !ModuleName
, _uModuleHash :: !(Maybe ModuleHash)
, _uImports :: !(Maybe (Vector Text))
, _uInfo :: !Info
} deriving (Show, Eq, Generic)
instance HasInfo Use where getInfo = _uInfo
instance Pretty Use where
pretty Use{..} =
let args = pretty _uModuleName : maybe [] (\mh -> [pretty mh]) _uModuleHash
in parensSep $ "use" : args
instance ToJSON Use where
toJSON Use{..} = object
[ "module" .= _uModuleName
, "hash" .= _uModuleHash
, "imports" .= _uImports
, "i" .= _uInfo
]
instance FromJSON Use where
parseJSON = withObject "Use" $ \o ->
Use <$> o .: "module"
<*> o .:? "hash"
<*> o .:? "imports"
<*> o .: "i"
instance NFData Use
instance SizeOf Use
-- -------------------------------------------------------------------------- --
-- Guard
data Guard a
= GPact !PactGuard
| GKeySet !KeySet
| GKeySetRef !KeySetName
| GModule !ModuleGuard
| GUser !(UserGuard a)
deriving (Eq,Show,Generic,Functor,Foldable,Traversable,Ord)
-- potentially long output due to constrained recursion of PactValue
instance (Arbitrary a) => Arbitrary (Guard a) where
arbitrary = oneof
[ GPact <$> arbitrary
, GKeySet <$> arbitrary
, GKeySetRef <$> arbitrary
, GModule <$> arbitrary
, GUser <$> arbitrary ]
instance NFData a => NFData (Guard a)
instance Pretty a => Pretty (Guard a) where
pretty (GPact g) = pretty g
pretty (GKeySet g) = pretty g
pretty (GKeySetRef g) = pretty g
pretty (GUser g) = pretty g
pretty (GModule g) = pretty g
instance (SizeOf p) => SizeOf (Guard p) where
sizeOf (GPact pg) = (constructorCost 1) + (sizeOf pg)
sizeOf (GKeySet ks) = (constructorCost 1) + (sizeOf ks)
sizeOf (GKeySetRef ksr) = (constructorCost 1) + (sizeOf ksr)
sizeOf (GModule mg) = (constructorCost 1) + (sizeOf mg)
sizeOf (GUser ug) = (constructorCost 1) + (sizeOf ug)
guardCodec :: (ToJSON a, FromJSON a) => Codec (Guard a)
guardCodec = Codec enc dec
where
enc (GKeySet k) = toJSON k
enc (GKeySetRef n) = object [ keyNamef .= n ]
enc (GPact g) = toJSON g
enc (GModule g) = toJSON g
enc (GUser g) = toJSON g
{-# INLINE enc #-}
dec v =
(GKeySet <$> parseJSON v) <|>
(withObject "KeySetRef" $ \o ->
GKeySetRef . KeySetName <$> o .: keyNamef) v <|>
(GPact <$> parseJSON v) <|>
(GModule <$> parseJSON v) <|>
(GUser <$> parseJSON v)
{-# INLINE dec #-}
keyNamef = "keysetref"
instance (FromJSON a,ToJSON a) => ToJSON (Guard a) where toJSON = encoder guardCodec
instance (FromJSON a,ToJSON a) => FromJSON (Guard a) where parseJSON = decoder guardCodec
-- -------------------------------------------------------------------------- --
-- Module
data Module g = Module
{ _mName :: !ModuleName
, _mGovernance :: !(Governance g)
, _mMeta :: !Meta
, _mCode :: !Code
, _mHash :: !ModuleHash
, _mBlessed :: !(HS.HashSet ModuleHash)
, _mInterfaces :: ![ModuleName]
, _mImports :: ![Use]
} deriving (Eq,Functor,Foldable,Traversable,Show,Generic)
instance NFData g => NFData (Module g)
instance Pretty g => Pretty (Module g) where
pretty Module{..} = parensSep
[ "module" , pretty _mName , pretty _mGovernance , pretty _mHash ]
instance ToJSON g => ToJSON (Module g) where toJSON = lensyToJSON 2
instance FromJSON g => FromJSON (Module g) where parseJSON = lensyParseJSON 2
instance (SizeOf g) => SizeOf (Module g)
-- -------------------------------------------------------------------------- --
-- Interface
data Interface = Interface
{ _interfaceName :: !ModuleName
, _interfaceCode :: !Code
, _interfaceMeta :: !Meta
, _interfaceImports :: ![Use]
} deriving (Eq,Show,Generic)
instance Pretty Interface where
pretty Interface{..} = parensSep [ "interface", pretty _interfaceName ]
instance ToJSON Interface where toJSON = lensyToJSON 10
instance FromJSON Interface where parseJSON = lensyParseJSON 10
instance NFData Interface
instance SizeOf Interface
-- -------------------------------------------------------------------------- --
-- Namespace
data Namespace a = Namespace
{ _nsName :: !NamespaceName
, _nsUser :: !(Guard a)
, _nsAdmin :: !(Guard a)
} deriving (Eq, Show, Generic)
instance (Arbitrary a) => Arbitrary (Namespace a) where
arbitrary = Namespace <$> arbitrary <*> arbitrary <*> arbitrary
instance Pretty (Namespace a) where
pretty Namespace{..} = "(namespace " <> prettyString (asString' _nsName) <> ")"
instance (SizeOf n) => SizeOf (Namespace n) where
sizeOf (Namespace name ug ag) =
(constructorCost 3) + (sizeOf name) + (sizeOf ug) + (sizeOf ag)
instance (ToJSON a, FromJSON a) => ToJSON (Namespace a) where toJSON = lensyToJSON 3
instance (FromJSON a, ToJSON a) => FromJSON (Namespace a) where parseJSON = lensyParseJSON 3
instance (NFData a) => NFData (Namespace a)
-- -------------------------------------------------------------------------- --
-- ModuleDef
data ModuleDef g
= MDModule !(Module g)
| MDInterface !Interface
deriving (Eq,Functor,Foldable,Traversable,Show,Generic)
instance NFData g => NFData (ModuleDef g)
instance Pretty g => Pretty (ModuleDef g) where
pretty = \case
MDModule m -> pretty m
MDInterface i -> pretty i
instance ToJSON g => ToJSON (ModuleDef g) where
toJSON (MDModule m) = toJSON m
toJSON (MDInterface i) = toJSON i
instance FromJSON g => FromJSON (ModuleDef g) where
parseJSON v = MDModule <$> parseJSON v <|> MDInterface <$> parseJSON v
instance (SizeOf g) => SizeOf (ModuleDef g)
moduleDefName :: ModuleDef g -> ModuleName
moduleDefName (MDModule m) = _mName m
moduleDefName (MDInterface m) = _interfaceName m
moduleDefCode :: ModuleDef g -> Code
moduleDefCode (MDModule m) = _mCode m
moduleDefCode (MDInterface m) = _interfaceCode m
moduleDefMeta :: ModuleDef g -> Meta
moduleDefMeta (MDModule m) = _mMeta m
moduleDefMeta (MDInterface m) = _interfaceMeta m
-- -------------------------------------------------------------------------- --
-- The following types have cyclic dependencies. There must be no TH splice
-- in between the definitions of these types.
--
-- * FunApp
-- * Ref' d (and Ref)
-- * NativeDFun
-- * Def n
-- * Object n
-- * Term n
-- -------------------------------------------------------------------------- --
-- FunApp
-- | Capture function application metadata
data FunApp = FunApp {
_faInfo :: !Info
, _faName :: !Text
, _faModule :: !(Maybe ModuleName)
, _faDefType :: !DefType
, _faTypes :: !(FunTypes (Term Name))
, _faDocs :: !(Maybe Text)
} deriving (Generic)
deriving instance (Show1 Term) => Show FunApp
deriving instance (Eq1 Term) => Eq FunApp
instance NFData FunApp
instance ToJSON FunApp where toJSON = lensyToJSON 3
instance FromJSON FunApp where parseJSON = lensyParseJSON 3
instance HasInfo FunApp where getInfo = _faInfo
-- -------------------------------------------------------------------------- --
-- Ref'
type Ref = Ref' (Term Name)
-- | Variable type for an evaluable 'Term'.
data Ref' d =
-- | "Reduced" (evaluated) or native (irreducible) term.
Direct !d |
-- | Unevaulated/un-reduced term, never a native.
Ref !(Term (Ref' d))
deriving (Functor,Foldable,Traversable,Generic)
deriving instance (Eq1 Term, Eq d) => Eq (Ref' d)
deriving instance (Show1 Term, Show d) => Show (Ref' d)
instance NFData d => NFData (Ref' d)
instance Pretty d => Pretty (Ref' d) where
pretty (Direct tm) = pretty tm
pretty (Ref tm) = pretty tm
instance HasInfo n => HasInfo (Ref' n) where
getInfo (Direct d) = getInfo d
getInfo (Ref r) = getInfo r
instance (SizeOf d) => SizeOf (Ref' d)
-- -------------------------------------------------------------------------- --
-- NativeDFun
data NativeDFun = NativeDFun
{ _nativeName :: !NativeDefName
, _nativeFun :: !(forall m . Monad m => FunApp -> [Term Ref] -> m (Gas,Term Name))
}
instance Eq NativeDFun where a == b = _nativeName a == _nativeName b
instance Show NativeDFun where
showsPrec p (NativeDFun name _) = showParen (p > 10) $
showString "NativeDFun " . showsPrec 11 name . showString " _"
instance NFData NativeDFun where
rnf (NativeDFun n _f) = seq n ()
-- -------------------------------------------------------------------------- --
-- Def
data Def n = Def
{ _dDefName :: !DefName
, _dModule :: !ModuleName
, _dDefType :: !DefType
, _dFunType :: !(FunType (Term n))
, _dDefBody :: !(Scope Int Term n)
, _dMeta :: !Meta
, _dDefMeta :: !(Maybe (DefMeta (Term n)))
, _dInfo :: !Info
} deriving (Functor,Foldable,Traversable,Generic)
deriving instance (Show1 Term, Show n) => Show (Def n)
deriving instance (Eq1 Term, Eq n) => Eq (Def n)
instance NFData (Term n) => NFData (Def n)
instance (Eq1 Term, Eq n) => Ord (Def n) where
a `compare` b = nm a `compare` nm b
where nm d = (_dModule d, _dDefName d)
instance HasInfo (Def n) where getInfo = _dInfo
instance Pretty (Term n) => Pretty (Def n) where
pretty Def{..} = parensSep $
[ prettyString (defTypeRep _dDefType)
, pretty _dModule <> "." <> pretty _dDefName <> ":" <> pretty (_ftReturn _dFunType)
, parensSep $ pretty <$> _ftArgs _dFunType
] ++ maybe [] (\docs -> [pretty docs]) (_mDocs _dMeta)
++ maybe [] (pure . pretty) _dDefMeta
instance (ToJSON (Term n), FromJSON (Term n)) => ToJSON (Def n) where toJSON = lensyToJSON 2
instance (ToJSON (Term n), FromJSON (Term n)) => FromJSON (Def n) where parseJSON = lensyParseJSON 2
derefDef :: Def n -> Name
derefDef Def{..} = QName $ QualifiedName _dModule (asString _dDefName) _dInfo
instance (SizeOf n) => SizeOf (Def n)
-- -------------------------------------------------------------------------- --
-- Lam
data Lam n
= Lam
{ _lamArg :: !Text
, _lamTy :: !(FunType (Term n))
, _lamBindBody :: !(Scope Int Term n)
, _lamInfo :: !Info
} deriving (Functor,Foldable,Traversable,Generic)
deriving instance (Show1 Term, Show n) => Show (Lam n)
deriving instance (Eq1 Term, Eq n) => Eq (Lam n)
instance HasInfo (Lam n) where getInfo = _lamInfo
instance Pretty n => Pretty (Lam n) where
pretty (Lam arg ty _ _) =
pretty arg <> ":" <> pretty (_ftReturn ty) <+> "lambda" <> (parensSep $ pretty <$> _ftArgs ty) <+> "..."
instance NFData n => NFData (Lam n)
instance (ToJSON (Term n), FromJSON (Term n)) => ToJSON (Lam n) where toJSON = lensyToJSON 2
instance (ToJSON (Term n), FromJSON (Term n)) => FromJSON (Lam n) where parseJSON = lensyParseJSON 2
instance (SizeOf n) => SizeOf (Lam n)
-- -------------------------------------------------------------------------- --
-- Object
-- | Full Term object.
data Object n = Object
{ _oObject :: !(ObjectMap (Term n))
, _oObjectType :: !(Type (Term n))
, _oKeyOrder :: !(Maybe [FieldKey])
, _oInfo :: !Info
} deriving (Functor,Foldable,Traversable,Generic)
deriving instance (Show1 Term, Show n) => Show (Object n)
deriving instance (Eq1 Term, Eq n) => Eq (Object n)
instance HasInfo (Object n) where getInfo = _oInfo
instance Pretty n => Pretty (Object n) where
pretty (Object bs _ Nothing _) = pretty bs
pretty (Object (ObjectMap om) _ (Just ko) _) =
annotate Val $ commaBraces $
map (\(k,v) -> pretty k <> ": " <> pretty v) $
sortBy (compare `on` (keyOrder . fst)) $
M.toList om
where keyOrder f = elemIndex f ko
instance NFData n => NFData (Object n)
instance (ToJSON n, FromJSON n) => ToJSON (Object n) where
toJSON Object{..} = object $
[ "obj" .= _oObject
, "type" .= _oObjectType
, "i" .= _oInfo ] ++
maybe [] (pure . ("keyorder" .=)) _oKeyOrder
instance (ToJSON n, FromJSON n) => FromJSON (Object n) where
parseJSON = withObject "Object" $ \o ->
Object <$> o .: "obj" <*> o .: "type" <*> o .:? "keyorder" <*> o .: "i"