-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathAST.hs
1192 lines (960 loc) · 40.8 KB
/
AST.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
-- |
-- Module : Cryptol.Parser.AST
-- Copyright : (c) 2013-2016 Galois, Inc.
-- License : BSD3
-- Maintainer : [email protected]
-- Stability : provisional
-- Portability : portable
{-# LANGUAGE Safe #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
module Cryptol.Parser.AST
( -- * Names
Ident, mkIdent, mkInfix, isInfixIdent, nullIdent, identText
, ModName, modRange
, PName(..), getModName, getIdent, mkUnqual, mkQual
, Named(..)
, Pass(..)
, Assoc(..)
-- * Types
, Schema(..)
, TParam(..)
, Kind(..)
, Type(..)
, Prop(..)
, tsName
, psName
, tsFixity
, psFixity
-- * Declarations
, Module
, ModuleG(..)
, mImports
, mSubmoduleImports
, Program(..)
, TopDecl(..)
, Decl(..)
, Fixity(..), defaultFixity
, FixityCmp(..), compareFixity
, TySyn(..)
, PropSyn(..)
, Bind(..)
, BindDef(..), LBindDef
, Pragma(..)
, ExportType(..)
, TopLevel(..)
, Import, ImportG(..), ImportSpec(..), ImpName(..)
, Newtype(..)
, PrimType(..)
, ParameterType(..)
, ParameterFun(..)
, NestedModule(..)
, PropGuardCase(..)
-- * Interactive
, ReplInput(..)
-- * Expressions
, Expr(..)
, Literal(..), NumInfo(..), FracInfo(..)
, Match(..)
, Pattern(..)
, Selector(..)
, TypeInst(..)
, UpdField(..)
, UpdHow(..)
, FunDesc(..)
, emptyFunDesc
, PrefixOp(..)
, prefixFixity
, asEApps
-- * Positions
, Located(..)
, LPName, LString, LIdent
, NoPos(..)
-- * Pretty-printing
, cppKind, ppSelector
) where
import Cryptol.Parser.Name
import Cryptol.Parser.Position
import Cryptol.Parser.Selector
import Cryptol.Utils.Fixity
import Cryptol.Utils.Ident
import Cryptol.Utils.RecordMap
import Cryptol.Utils.PP
import Data.List(intersperse)
import Data.Bits(shiftR)
import Data.Maybe (catMaybes)
import Data.Ratio(numerator,denominator)
import Data.Text (Text)
import Numeric(showIntAtBase,showFloat,showHFloat)
import GHC.Generics (Generic)
import Control.DeepSeq
import Prelude ()
import Prelude.Compat
-- AST -------------------------------------------------------------------------
-- | A name with location information.
type LPName = Located PName
-- | An identifier with location information.
type LIdent = Located Ident
-- | A string with location information.
type LString = Located String
-- | A record with located ident fields
type Rec e = RecordMap Ident (Range, e)
newtype Program name = Program [TopDecl name]
deriving (Show)
-- | A parsed module.
data ModuleG mname name = Module
{ mName :: Located mname -- ^ Name of the module
, mInstance :: !(Maybe (Located ModName)) -- ^ Functor to instantiate
-- (if this is a functor instnaces)
-- , mImports :: [Located Import] -- ^ Imports for the module
, mDecls :: [TopDecl name] -- ^ Declartions for the module
} deriving (Show, Generic, NFData)
mImports :: ModuleG mname name -> [ Located Import ]
mImports m =
[ li { thing = i { iModule = n } }
| DImport li <- mDecls m
, let i = thing li
, ImpTop n <- [iModule i]
]
mSubmoduleImports :: ModuleG mname name -> [ Located (ImportG name) ]
mSubmoduleImports m =
[ li { thing = i { iModule = n } }
| DImport li <- mDecls m
, let i = thing li
, ImpNested n <- [iModule i]
]
type Module = ModuleG ModName
newtype NestedModule name = NestedModule (ModuleG name name)
deriving (Show,Generic,NFData)
modRange :: Module name -> Range
modRange m = rCombs $ catMaybes
[ getLoc (mName m)
, getLoc (mImports m)
, getLoc (mDecls m)
, Just (Range { from = start, to = start, source = "" })
]
data TopDecl name =
Decl (TopLevel (Decl name))
| DPrimType (TopLevel (PrimType name))
| TDNewtype (TopLevel (Newtype name)) -- ^ @newtype T as = t
| Include (Located FilePath) -- ^ @include File@
| DParameterType (ParameterType name) -- ^ @parameter type T : #@
| DParameterConstraint [Located (Prop name)]
-- ^ @parameter type constraint (fin T)@
| DParameterFun (ParameterFun name) -- ^ @parameter someVal : [256]@
| DModule (TopLevel (NestedModule name)) -- ^ Nested module
| DImport (Located (ImportG (ImpName name))) -- ^ An import declaration
deriving (Show, Generic, NFData)
data ImpName name =
ImpTop ModName
| ImpNested name
deriving (Show, Generic, NFData)
data Decl name = DSignature [Located name] (Schema name)
| DFixity !Fixity [Located name]
| DPragma [Located name] Pragma
| DBind (Bind name)
| DRec [Bind name]
-- ^ A group of recursive bindings, introduced by the renamer.
| DPatBind (Pattern name) (Expr name)
| DType (TySyn name)
| DProp (PropSyn name)
| DLocated (Decl name) Range
deriving (Eq, Show, Generic, NFData, Functor)
-- | A type parameter
data ParameterType name = ParameterType
{ ptName :: Located name -- ^ name of type parameter
, ptKind :: Kind -- ^ kind of parameter
, ptDoc :: Maybe Text -- ^ optional documentation
, ptFixity :: Maybe Fixity -- ^ info for infix use
, ptNumber :: !Int -- ^ number of the parameter
} deriving (Eq,Show,Generic,NFData)
-- | A value parameter
data ParameterFun name = ParameterFun
{ pfName :: Located name -- ^ name of value parameter
, pfSchema :: Schema name -- ^ schema for parameter
, pfDoc :: Maybe Text -- ^ optional documentation
, pfFixity :: Maybe Fixity -- ^ info for infix use
} deriving (Eq,Show,Generic,NFData)
-- | An import declaration.
data ImportG mname = Import
{ iModule :: !mname
, iAs :: Maybe ModName
, iSpec :: Maybe ImportSpec
} deriving (Eq, Show, Generic, NFData)
type Import = ImportG ModName
-- | The list of names following an import.
data ImportSpec = Hiding [Ident]
| Only [Ident]
deriving (Eq, Show, Generic, NFData)
-- The 'Maybe Fixity' field is filled in by the NoPat pass.
data TySyn n = TySyn (Located n) (Maybe Fixity) [TParam n] (Type n)
deriving (Eq, Show, Generic, NFData, Functor)
-- The 'Maybe Fixity' field is filled in by the NoPat pass.
data PropSyn n = PropSyn (Located n) (Maybe Fixity) [TParam n] [Prop n]
deriving (Eq, Show, Generic, NFData, Functor)
tsName :: TySyn name -> Located name
tsName (TySyn lqn _ _ _) = lqn
psName :: PropSyn name -> Located name
psName (PropSyn lqn _ _ _) = lqn
tsFixity :: TySyn name -> Maybe Fixity
tsFixity (TySyn _ f _ _) = f
psFixity :: PropSyn name -> Maybe Fixity
psFixity (PropSyn _ f _ _) = f
{- | Bindings. Notes:
* The parser does not associate type signatures and pragmas with
their bindings: this is done in a separate pass, after de-sugaring
pattern bindings. In this way we can associate pragmas and type
signatures with the variables defined by pattern bindings as well.
* Currently, there is no surface syntax for defining monomorphic
bindings (i.e., bindings that will not be automatically generalized
by the type checker. However, they are useful when de-sugaring
patterns.
-}
data Bind name = Bind
{ bName :: Located name -- ^ Defined thing
, bParams :: [Pattern name] -- ^ Parameters
, bDef :: Located (BindDef name) -- ^ Definition
, bSignature :: Maybe (Schema name) -- ^ Optional type sig
, bInfix :: Bool -- ^ Infix operator?
, bFixity :: Maybe Fixity -- ^ Optional fixity info
, bPragmas :: [Pragma] -- ^ Optional pragmas
, bMono :: Bool -- ^ Is this a monomorphic binding
, bDoc :: Maybe Text -- ^ Optional doc string
, bExport :: !ExportType
} deriving (Eq, Generic, NFData, Functor, Show)
type LBindDef = Located (BindDef PName)
data BindDef name = DPrim
| DForeign
| DExpr (Expr name)
| DPropGuards [PropGuardCase name]
deriving (Eq, Show, Generic, NFData, Functor)
data PropGuardCase name = PropGuardCase
{ pgcProps :: [Located (Prop name)]
, pgcExpr :: Expr name
}
deriving (Eq,Generic,NFData,Functor,Show)
data Pragma = PragmaNote String
| PragmaProperty
deriving (Eq, Show, Generic, NFData)
data Newtype name = Newtype { nName :: Located name -- ^ Type name
, nParams :: [TParam name] -- ^ Type params
, nBody :: Rec (Type name) -- ^ Body
} deriving (Eq, Show, Generic, NFData)
-- | A declaration for a type with no implementation.
data PrimType name = PrimType { primTName :: Located name
, primTKind :: Located Kind
, primTCts :: ([TParam name], [Prop name])
-- ^ parameters are in the order used
-- by the type constructor.
, primTFixity :: Maybe Fixity
} deriving (Show,Generic,NFData)
-- | Input at the REPL, which can be an expression, a @let@
-- statement, or empty (possibly a comment).
data ReplInput name = ExprInput (Expr name)
| LetInput [Decl name]
| EmptyInput
deriving (Eq, Show)
-- | Export information for a declaration.
data ExportType = Public
| Private
deriving (Eq, Show, Ord, Generic, NFData)
-- | A top-level module declaration.
data TopLevel a = TopLevel { tlExport :: ExportType
, tlDoc :: Maybe (Located Text)
, tlValue :: a
}
deriving (Show, Generic, NFData, Functor, Foldable, Traversable)
-- | Infromation about the representation of a numeric constant.
data NumInfo = BinLit Text Int -- ^ n-digit binary literal
| OctLit Text Int -- ^ n-digit octal literal
| DecLit Text -- ^ overloaded decimal literal
| HexLit Text Int -- ^ n-digit hex literal
| PolyLit Int -- ^ polynomial literal
deriving (Eq, Show, Generic, NFData)
-- | Information about fractional literals.
data FracInfo = BinFrac Text
| OctFrac Text
| DecFrac Text
| HexFrac Text
deriving (Eq,Show,Generic,NFData)
-- | Literals.
data Literal = ECNum Integer NumInfo -- ^ @0x10@ (HexLit 2)
| ECChar Char -- ^ @'a'@
| ECFrac Rational FracInfo -- ^ @1.2e3@
| ECString String -- ^ @\"hello\"@
deriving (Eq, Show, Generic, NFData)
data Expr n = EVar n -- ^ @ x @
| ELit Literal -- ^ @ 0x10 @
| EGenerate (Expr n) -- ^ @ generate f @
| ETuple [Expr n] -- ^ @ (1,2,3) @
| ERecord (Rec (Expr n)) -- ^ @ { x = 1, y = 2 } @
| ESel (Expr n) Selector -- ^ @ e.l @
| EUpd (Maybe (Expr n)) [ UpdField n ] -- ^ @ { r | x = e } @
| EList [Expr n] -- ^ @ [1,2,3] @
| EFromTo (Type n) (Maybe (Type n)) (Type n) (Maybe (Type n))
-- ^ @ [1, 5 .. 117 : t] @
| EFromToBy Bool (Type n) (Type n) (Type n) (Maybe (Type n))
-- ^ @ [1 .. 10 by 2 : t ] @
| EFromToDownBy Bool (Type n) (Type n) (Type n) (Maybe (Type n))
-- ^ @ [10 .. 1 down by 2 : t ] @
| EFromToLessThan (Type n) (Type n) (Maybe (Type n))
-- ^ @ [ 1 .. < 10 : t ] @
| EInfFrom (Expr n) (Maybe (Expr n))-- ^ @ [1, 3 ...] @
| EComp (Expr n) [[Match n]] -- ^ @ [ 1 | x <- xs ] @
| EApp (Expr n) (Expr n) -- ^ @ f x @
| EAppT (Expr n) [(TypeInst n)] -- ^ @ f `{x = 8}, f`{8} @
| EIf (Expr n) (Expr n) (Expr n) -- ^ @ if ok then e1 else e2 @
| EWhere (Expr n) [Decl n] -- ^ @ 1 + x where { x = 2 } @
| ETyped (Expr n) (Type n) -- ^ @ 1 : [8] @
| ETypeVal (Type n) -- ^ @ `(x + 1)@, @x@ is a type
| EFun (FunDesc n) [Pattern n] (Expr n) -- ^ @ \\x y -> x @
| ELocated (Expr n) Range -- ^ position annotation
| ESplit (Expr n) -- ^ @ splitAt x @ (Introduced by NoPat)
| EParens (Expr n) -- ^ @ (e) @ (Removed by Fixity)
| EInfix (Expr n) (Located n) Fixity (Expr n)-- ^ @ a + b @ (Removed by Fixity)
| EPrefix PrefixOp (Expr n) -- ^ @ -1, ~1 @
deriving (Eq, Show, Generic, NFData, Functor)
-- | Prefix operator.
data PrefixOp = PrefixNeg -- ^ @ - @
| PrefixComplement -- ^ @ ~ @
deriving (Eq, Show, Generic, NFData)
prefixFixity :: PrefixOp -> Fixity
prefixFixity op = Fixity { fAssoc = LeftAssoc, .. }
where fLevel = case op of
PrefixNeg -> 80
PrefixComplement -> 100
-- | Description of functions. Only trivial information is provided here
-- by the parser. The NoPat pass fills this in as required.
data FunDesc n =
FunDesc
{ funDescrName :: Maybe n -- ^ Name of this function, if it has one
, funDescrArgOffset :: Int -- ^ number of previous arguments to this function
-- bound in surrounding lambdas (defaults to 0)
}
deriving (Eq, Show, Generic, NFData, Functor)
emptyFunDesc :: FunDesc n
emptyFunDesc = FunDesc Nothing 0
data UpdField n = UpdField UpdHow [Located Selector] (Expr n)
-- ^ non-empty list @ x.y = e@
deriving (Eq, Show, Generic, NFData, Functor)
data UpdHow = UpdSet | UpdFun -- ^ Are we setting or updating a field.
deriving (Eq, Show, Generic, NFData)
data TypeInst name = NamedInst (Named (Type name))
| PosInst (Type name)
deriving (Eq, Show, Generic, NFData, Functor)
data Match name = Match (Pattern name) (Expr name) -- ^ p <- e
| MatchLet (Bind name)
deriving (Eq, Show, Generic, NFData, Functor)
data Pattern n = PVar (Located n) -- ^ @ x @
| PWild -- ^ @ _ @
| PTuple [Pattern n] -- ^ @ (x,y,z) @
| PRecord (Rec (Pattern n)) -- ^ @ { x = (a,b,c), y = z } @
| PList [ Pattern n ] -- ^ @ [ x, y, z ] @
| PTyped (Pattern n) (Type n) -- ^ @ x : [8] @
| PSplit (Pattern n) (Pattern n)-- ^ @ (x # y) @
| PLocated (Pattern n) Range -- ^ Location information
deriving (Eq, Show, Generic, NFData, Functor)
data Named a = Named { name :: Located Ident, value :: a }
deriving (Eq, Show, Foldable, Traversable, Generic, NFData, Functor)
data Schema n = Forall [TParam n] [Prop n] (Type n) (Maybe Range)
deriving (Eq, Show, Generic, NFData, Functor)
data Kind = KProp | KNum | KType | KFun Kind Kind
deriving (Eq, Show, Generic, NFData)
data TParam n = TParam { tpName :: n
, tpKind :: Maybe Kind
, tpRange :: Maybe Range
}
deriving (Eq, Show, Generic, NFData, Functor)
data Type n = TFun (Type n) (Type n) -- ^ @[8] -> [8]@
| TSeq (Type n) (Type n) -- ^ @[8] a@
| TBit -- ^ @Bit@
| TNum Integer -- ^ @10@
| TChar Char -- ^ @'a'@
| TUser n [Type n] -- ^ A type variable or synonym
| TTyApp [Named (Type n)] -- ^ @`{ x = [8], y = Integer }@
| TRecord (Rec (Type n)) -- ^ @{ x : [8], y : [32] }@
| TTuple [Type n] -- ^ @([8], [32])@
| TWild -- ^ @_@, just some type.
| TLocated (Type n) Range -- ^ Location information
| TParens (Type n) -- ^ @ (ty) @
| TInfix (Type n) (Located n) Fixity (Type n) -- ^ @ ty + ty @
deriving (Eq, Show, Generic, NFData, Functor)
-- | A 'Prop' is a 'Type' that represents a type constraint.
newtype Prop n = CType (Type n)
deriving (Eq, Show, Generic, NFData, Functor)
--------------------------------------------------------------------------------
-- Note: When an explicit location is missing, we could use the sub-components
-- to try to estimate a location...
instance AddLoc (Expr n) where
addLoc x@ELocated{} _ = x
addLoc x r = ELocated x r
dropLoc (ELocated e _) = dropLoc e
dropLoc e = e
instance HasLoc (Expr name) where
getLoc (ELocated _ r) = Just r
getLoc _ = Nothing
instance HasLoc (TParam name) where
getLoc (TParam _ _ r) = r
instance AddLoc (TParam name) where
addLoc (TParam a b _) l = TParam a b (Just l)
dropLoc (TParam a b _) = TParam a b Nothing
instance HasLoc (Type name) where
getLoc (TLocated _ r) = Just r
getLoc _ = Nothing
instance AddLoc (Type name) where
addLoc = TLocated
dropLoc (TLocated e _) = dropLoc e
dropLoc e = e
instance AddLoc (Pattern name) where
addLoc = PLocated
dropLoc (PLocated e _) = dropLoc e
dropLoc e = e
instance HasLoc (Pattern name) where
getLoc (PLocated _ r) = Just r
getLoc (PTyped r _) = getLoc r
getLoc (PVar x) = getLoc x
getLoc _ = Nothing
instance HasLoc (Bind name) where
getLoc b = getLoc (bName b, bDef b)
instance HasLoc (Match name) where
getLoc (Match p e) = getLoc (p,e)
getLoc (MatchLet b) = getLoc b
instance HasLoc a => HasLoc (Named a) where
getLoc l = getLoc (name l, value l)
instance HasLoc (Schema name) where
getLoc (Forall _ _ _ r) = r
instance AddLoc (Schema name) where
addLoc (Forall xs ps t _) r = Forall xs ps t (Just r)
dropLoc (Forall xs ps t _) = Forall xs ps t Nothing
instance HasLoc (Decl name) where
getLoc (DLocated _ r) = Just r
getLoc _ = Nothing
instance AddLoc (Decl name) where
addLoc d r = DLocated d r
dropLoc (DLocated d _) = dropLoc d
dropLoc d = d
instance HasLoc a => HasLoc (TopLevel a) where
getLoc = getLoc . tlValue
instance HasLoc (TopDecl name) where
getLoc td =
case td of
Decl tld -> getLoc tld
DPrimType pt -> getLoc pt
TDNewtype n -> getLoc n
Include lfp -> getLoc lfp
DParameterType d -> getLoc d
DParameterFun d -> getLoc d
DParameterConstraint d -> getLoc d
DModule d -> getLoc d
DImport d -> getLoc d
instance HasLoc (PrimType name) where
getLoc pt = Just (rComb (srcRange (primTName pt)) (srcRange (primTKind pt)))
instance HasLoc (ParameterType name) where
getLoc a = getLoc (ptName a)
instance HasLoc (ParameterFun name) where
getLoc a = getLoc (pfName a)
instance HasLoc (ModuleG mname name) where
getLoc m
| null locs = Nothing
| otherwise = Just (rCombs locs)
where
locs = catMaybes [ getLoc (mName m)
, getLoc (mImports m)
, getLoc (mDecls m)
]
instance HasLoc (NestedModule name) where
getLoc (NestedModule m) = getLoc m
instance HasLoc (Newtype name) where
getLoc n
| null locs = Nothing
| otherwise = Just (rCombs locs)
where
locs = catMaybes ([ getLoc (nName n)] ++ map (Just . fst . snd) (displayFields (nBody n)))
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Pretty printing
ppL :: PP a => Located a -> Doc
ppL = pp . thing
ppNamed :: PP a => String -> Named a -> Doc
ppNamed s x = ppL (name x) <+> text s <+> pp (value x)
ppNamed' :: PP a => String -> (Ident, (Range, a)) -> Doc
ppNamed' s (i,(_,v)) = pp i <+> text s <+> pp v
instance (Show name, PPName mname, PPName name) => PP (ModuleG mname name) where
ppPrec _ = ppModule 0
ppModule :: (Show name, PPName mname, PPName name) =>
Int -> ModuleG mname name -> Doc
ppModule n m =
text "module" <+> ppL (mName m) <+> text "where" $$ nest n body
where
body = vcat (map ppL (mImports m))
$$ vcat (map pp (mDecls m))
instance (Show name, PPName name) => PP (NestedModule name) where
ppPrec _ (NestedModule m) = ppModule 2 m
instance (Show name, PPName name) => PP (Program name) where
ppPrec _ (Program ds) = vcat (map pp ds)
instance (Show name, PPName name) => PP (TopDecl name) where
ppPrec _ top_decl =
case top_decl of
Decl d -> pp d
DPrimType p -> pp p
TDNewtype n -> pp n
Include l -> text "include" <+> text (show (thing l))
DParameterFun d -> pp d
DParameterType d -> pp d
DParameterConstraint d ->
"parameter" <+> "type" <+> "constraint" <+> prop
where prop = case map (pp . thing) d of
[x] -> x
[] -> "()"
xs -> nest 1 (parens (commaSepFill xs))
DModule d -> pp d
DImport i -> pp (thing i)
instance (Show name, PPName name) => PP (PrimType name) where
ppPrec _ pt =
"primitive" <+> "type" <+> pp (primTName pt) <+> ":" <+> pp (primTKind pt)
instance (Show name, PPName name) => PP (ParameterType name) where
ppPrec _ a = text "parameter" <+> text "type" <+>
ppPrefixName (ptName a) <+> text ":" <+> pp (ptKind a)
instance (Show name, PPName name) => PP (ParameterFun name) where
ppPrec _ a = text "parameter" <+> ppPrefixName (pfName a) <+> text ":"
<+> pp (pfSchema a)
instance (Show name, PPName name) => PP (Decl name) where
ppPrec n decl =
case decl of
DSignature xs s -> commaSep (map ppL xs) <+> text ":" <+> pp s
DPatBind p e -> pp p <+> text "=" <+> pp e
DBind b -> ppPrec n b
DRec bs -> nest 2 (vcat ("recursive" : map (ppPrec n) bs))
DFixity f ns -> ppFixity f ns
DPragma xs p -> ppPragma xs p
DType ts -> ppPrec n ts
DProp ps -> ppPrec n ps
DLocated d _ -> ppPrec n d
ppFixity :: PPName name => Fixity -> [Located name] -> Doc
ppFixity (Fixity LeftAssoc i) ns = text "infixl" <+> int i <+> commaSep (map pp ns)
ppFixity (Fixity RightAssoc i) ns = text "infixr" <+> int i <+> commaSep (map pp ns)
ppFixity (Fixity NonAssoc i) ns = text "infix" <+> int i <+> commaSep (map pp ns)
instance PPName name => PP (Newtype name) where
ppPrec _ nt = nest 2 $ sep
[ fsep $ [text "newtype", ppL (nName nt)] ++ map pp (nParams nt) ++ [char '=']
, ppRecord (map (ppNamed' ":") (displayFields (nBody nt)))
]
instance PP mname => PP (ImportG mname) where
ppPrec _ d = text "import" <+> sep ([pp (iModule d)] ++ mbAs ++ mbSpec)
where
mbAs = maybe [] (\ name -> [text "as" <+> pp name]) (iAs d)
mbSpec = maybe [] (\x -> [pp x]) (iSpec d)
instance PP name => PP (ImpName name) where
ppPrec _ nm =
case nm of
ImpTop x -> pp x
ImpNested x -> "submodule" <+> pp x
instance PP ImportSpec where
ppPrec _ s = case s of
Hiding names -> text "hiding" <+> parens (commaSep (map pp names))
Only names -> parens (commaSep (map pp names))
-- TODO: come up with a good way of showing the export specification here
instance PP a => PP (TopLevel a) where
ppPrec _ tl = pp (tlValue tl)
instance PP Pragma where
ppPrec _ (PragmaNote x) = text x
ppPrec _ PragmaProperty = text "property"
ppPragma :: PPName name => [Located name] -> Pragma -> Doc
ppPragma xs p =
text "/*" <+> text "pragma" <+> commaSep (map ppL xs) <+> text ":" <+> pp p
<+> text "*/"
instance (Show name, PPName name) => PP (Bind name) where
ppPrec _ b = vcat (sig ++ [ ppPragma [f] p | p <- bPragmas b ] ++
[hang (def <+> eq) 4 (pp (thing (bDef b)))])
where def | bInfix b = lhsOp
| otherwise = lhs
f = bName b
sig = case bSignature b of
Nothing -> []
Just s -> [pp (DSignature [f] s)]
eq = if bMono b then text ":=" else text "="
lhs = fsep (ppL f : (map (ppPrec 3) (bParams b)))
lhsOp = case bParams b of
[x,y] -> pp x <+> ppL f <+> pp y
xs -> parens (parens (ppL f) <+> fsep (map (ppPrec 0) xs))
-- _ -> panic "AST" [ "Malformed infix operator", show b ]
instance (Show name, PPName name) => PP (BindDef name) where
ppPrec _ DPrim = text "<primitive>"
ppPrec _ DForeign = text "<foreign>"
ppPrec p (DExpr e) = ppPrec p e
ppPrec _p (DPropGuards _guards) = text "propguards"
instance PPName name => PP (TySyn name) where
ppPrec _ (TySyn x _ xs t) =
nest 2 $ sep $
[ fsep $ [text "type", ppL x] ++ map (ppPrec 1) xs ++ [text "="]
, pp t
]
instance PPName name => PP (PropSyn name) where
ppPrec _ (PropSyn x _ xs ps) =
nest 2 $ sep $
[ fsep $ [text "constraint", ppL x] ++ map (ppPrec 1) xs ++ [text "="]
, parens (commaSep (map pp ps))
]
instance PP Literal where
ppPrec _ lit =
case lit of
ECNum n i -> ppNumLit n i
ECChar c -> text (show c)
ECFrac n i -> ppFracLit n i
ECString s -> text (show s)
ppFracLit :: Rational -> FracInfo -> Doc
ppFracLit x i
| toRational dbl == x =
case i of
BinFrac _ -> frac
OctFrac _ -> frac
DecFrac _ -> text (showFloat dbl "")
HexFrac _ -> text (showHFloat dbl "")
| otherwise = frac
where
dbl = fromRational x :: Double
frac = "fraction`" <.> braces
(commaSep (map integer [ numerator x, denominator x ]))
ppNumLit :: Integer -> NumInfo -> Doc
ppNumLit n info =
case info of
DecLit _ -> integer n
BinLit _ w -> pad 2 "0b" w
OctLit _ w -> pad 8 "0o" w
HexLit _ w -> pad 16 "0x" w
PolyLit w -> text "<|" <+> poly w <+> text "|>"
where
pad base pref w =
let txt = showIntAtBase base ("0123456789abcdef" !!) n ""
in text pref <.> text (replicate (w - length txt) '0') <.> text txt
poly w = let (res,deg) = bits Nothing [] 0 n
z | w == 0 = []
| Just d <- deg, d + 1 == w = []
| otherwise = [polyTerm0 (w-1)]
in fsep $ intersperse (text "+") $ z ++ map polyTerm res
polyTerm 0 = text "1"
polyTerm 1 = text "x"
polyTerm p = text "x" <.> text "^^" <.> int p
polyTerm0 0 = text "0"
polyTerm0 p = text "0" <.> text "*" <.> polyTerm p
bits d res p num
| num == 0 = (res,d)
| even num = bits d res (p + 1) (num `shiftR` 1)
| otherwise = bits (Just p) (p : res) (p + 1) (num `shiftR` 1)
wrap :: Int -> Int -> Doc -> Doc
wrap contextPrec myPrec doc = optParens (myPrec < contextPrec) doc
isEApp :: Expr n -> Maybe (Expr n, Expr n)
isEApp (ELocated e _) = isEApp e
isEApp (EApp e1 e2) = Just (e1,e2)
isEApp _ = Nothing
asEApps :: Expr n -> (Expr n, [Expr n])
asEApps expr = go expr []
where go e es = case isEApp e of
Nothing -> (e, es)
Just (e1, e2) -> go e1 (e2 : es)
instance PPName name => PP (TypeInst name) where
ppPrec _ (PosInst t) = pp t
ppPrec _ (NamedInst x) = ppNamed "=" x
{- Precedences:
0: lambda, if, where, type annotation
2: infix expression (separate precedence table)
3: application, prefix expressions
-}
instance (Show name, PPName name) => PP (Expr name) where
-- Wrap if top level operator in expression is less than `n`
ppPrec n expr =
case expr of
-- atoms
EVar x -> ppPrefixName x
ELit x -> pp x
EGenerate x -> wrap n 3 (text "generate" <+> ppPrec 4 x)
ETuple es -> parens (commaSep (map pp es))
ERecord fs -> braces (commaSep (map (ppNamed' "=") (displayFields fs)))
EList es -> brackets (commaSep (map pp es))
EFromTo e1 e2 e3 t1 -> brackets (pp e1 <.> step <+> text ".." <+> end)
where step = maybe mempty (\e -> comma <+> pp e) e2
end = maybe (pp e3) (\t -> pp e3 <+> colon <+> pp t) t1
EFromToBy isStrict e1 e2 e3 t1 -> brackets (pp e1 <+> dots <+> pp e2 <+> text "by" <+> end)
where end = maybe (pp e3) (\t -> pp e3 <+> colon <+> pp t) t1
dots | isStrict = text ".. <"
| otherwise = text ".."
EFromToDownBy isStrict e1 e2 e3 t1 -> brackets (pp e1 <+> dots <+> pp e2 <+> text "down by" <+> end)
where end = maybe (pp e3) (\t -> pp e3 <+> colon <+> pp t) t1
dots | isStrict = text ".. >"
| otherwise = text ".."
EFromToLessThan e1 e2 t1 -> brackets (strt <+> text ".. <" <+> end)
where strt = maybe (pp e1) (\t -> pp e1 <+> colon <+> pp t) t1
end = pp e2
EInfFrom e1 e2 -> brackets (pp e1 <.> step <+> text "...")
where step = maybe mempty (\e -> comma <+> pp e) e2
EComp e mss -> brackets (pp e <> align (vcat (map arm mss)))
where arm ms = text " |" <+> commaSep (map pp ms)
EUpd mb fs -> braces (hd <+> "|" <+> commaSep (map pp fs))
where hd = maybe "_" pp mb
ETypeVal t -> text "`" <.> ppPrec 5 t -- XXX
EAppT e ts -> ppPrec 4 e <.> text "`" <.> braces (commaSep (map pp ts))
ESel e l -> ppPrec 4 e <.> text "." <.> pp l
-- low prec
EFun _ xs e -> wrap n 0 ((text "\\" <.> hsep (map (ppPrec 3) xs)) <+>
text "->" <+> pp e)
EIf e1 e2 e3 -> wrap n 0 $ sep [ text "if" <+> pp e1
, text "then" <+> pp e2
, text "else" <+> pp e3 ]
ETyped e t -> wrap n 0 (ppPrec 2 e <+> text ":" <+> pp t)
EWhere e ds -> wrap n 0 $ align $ vsep
[ pp e
, hang "where" 2 (vcat (map pp ds))
]
-- infix applications
_ | Just ifix <- isInfix expr ->
optParens (n > 2)
$ ppInfix 2 isInfix ifix
EApp _ _ -> let (e, es) = asEApps expr in
wrap n 3 (ppPrec 3 e <+> fsep (map (ppPrec 4) es))
ELocated e _ -> ppPrec n e
ESplit e -> wrap n 3 (text "splitAt" <+> ppPrec 4 e)
EParens e -> parens (pp e)
-- NOTE: these don't produce correctly parenthesized expressions without
-- explicit EParens nodes when necessary, since we don't check the actual
-- fixities of the operators.
EInfix e1 op _ e2 -> wrap n 0 (pp e1 <+> ppInfixName (thing op) <+> pp e2)
EPrefix op e -> wrap n 3 (text (prefixText op) <.> ppPrec 4 e)
where
isInfix (EApp (EApp (EVar ieOp) ieLeft) ieRight) = do
ieFixity <- ppNameFixity ieOp
return Infix { .. }
isInfix _ = Nothing
prefixText PrefixNeg = "-"
prefixText PrefixComplement = "~"
instance (Show name, PPName name) => PP (UpdField name) where
ppPrec _ (UpdField h xs e) = ppNestedSels (map thing xs) <+> pp h <+> pp e
instance PP UpdHow where
ppPrec _ h = case h of
UpdSet -> "="
UpdFun -> "->"
instance PPName name => PP (Pattern name) where
ppPrec n pat =
case pat of
PVar x -> pp (thing x)
PWild -> char '_'
PTuple ps -> ppTuple (map pp ps)
PRecord fs -> ppRecord (map (ppNamed' "=") (displayFields fs))
PList ps -> ppList (map pp ps)
PTyped p t -> wrap n 0 (ppPrec 1 p <+> text ":" <+> pp t)
PSplit p1 p2 -> wrap n 1 (ppPrec 1 p1 <+> text "#" <+> ppPrec 1 p2)
PLocated p _ -> ppPrec n p
instance (Show name, PPName name) => PP (Match name) where
ppPrec _ (Match p e) = pp p <+> text "<-" <+> pp e
ppPrec _ (MatchLet b) = pp b
instance PPName name => PP (Schema name) where
ppPrec _ (Forall xs ps t _) = sep (vars ++ preds ++ [pp t])
where vars = case xs of
[] -> []
_ -> [nest 1 (braces (commaSepFill (map pp xs)))]
preds = case ps of
[] -> []
_ -> [nest 1 (parens (commaSepFill (map pp ps))) <+> text "=>"]
instance PP Kind where
ppPrec _ KType = text "*"
ppPrec _ KNum = text "#"
ppPrec _ KProp = text "@"
ppPrec n (KFun k1 k2) = wrap n 1 (ppPrec 1 k1 <+> "->" <+> ppPrec 0 k2)
-- | "Conversational" printing of kinds (e.g., to use in error messages)
cppKind :: Kind -> Doc
cppKind KType = text "a value type"
cppKind KNum = text "a numeric type"
cppKind KProp = text "a constraint type"
cppKind (KFun {}) = text "a type-constructor type"
instance PPName name => PP (TParam name) where
ppPrec n (TParam p Nothing _) = ppPrec n p
ppPrec n (TParam p (Just k) _) = wrap n 1 (pp p <+> text ":" <+> pp k)
-- 4: atomic type expression
-- 3: [_]t or application
-- 2: infix type
-- 1: function type
instance PPName name => PP (Type name) where
ppPrec n ty =
case ty of
TWild -> text "_"
TTuple ts -> parens $ commaSep $ map pp ts
TTyApp fs -> braces $ commaSep $ map (ppNamed " = ") fs
TRecord fs -> braces $ commaSep $ map (ppNamed' ":") (displayFields fs)
TBit -> text "Bit"
TNum x -> integer x
TChar x -> text (show x)
TSeq t1 TBit -> brackets (pp t1)
TSeq t1 t2 -> optParens (n > 3)
$ brackets (pp t1) <.> ppPrec 3 t2
TUser f [] -> ppPrefixName f
TUser f ts -> optParens (n > 3)
$ ppPrefixName f <+> fsep (map (ppPrec 4) ts)
TFun t1 t2 -> optParens (n > 1)
$ sep [ppPrec 2 t1 <+> text "->", ppPrec 1 t2]
TLocated t _ -> ppPrec n t
TParens t -> parens (pp t)
TInfix t1 o _ t2 -> optParens (n > 2)
$ sep [ppPrec 2 t1 <+> ppInfixName o, ppPrec 3 t2]