1
1
{-# LANGUAGE FlexibleContexts, UndecidableInstances
2
- , FlexibleInstances, OverloadedStrings, ScopedTypeVariables, GADTs #-}
2
+ , FlexibleInstances, OverloadedStrings, ScopedTypeVariables, GADTs, ViewPatterns #-}
3
3
module Syntax.Pretty (displayType , applyCons , prettyMotive , displayTypeTyped ) where
4
4
5
5
import Control.Lens hiding (Lazy )
@@ -114,9 +114,13 @@ prettyType (TyApp (TyCon v _) x) | show (pretty v) == mempty = displayType x
114
114
-- This is really gross
115
115
116
116
prettyType (TyApp x e) = parenTyFun x (displayType x) <+> parenTyArg e (displayType e)
117
- prettyType (TyRows p rows) = enclose (lbrace <> space) (space <> rbrace) $
117
+
118
+ prettyType (flatRows -> Just (Just p, rows)) = enclose (lbrace <> space) (space <> rbrace) $
118
119
pretty p <+> soperator pipe <+> hsep (punctuate comma (displayRows rows))
119
- prettyType (TyExactRows rows) = record (displayRows rows)
120
+ prettyType (flatRows -> Just (Nothing , rows)) = record (displayRows rows)
121
+ prettyType TyRows {} = error " unmatched flatRows"
122
+ prettyType TyExactRows {} = error " unmatched flatRows"
123
+
120
124
prettyType (TyTupleL x y) = parens $ prettyType x <> comma <+> prettyType y
121
125
prettyType (TyTuple t s) =
122
126
parenTyFun t (displayType t) <+> soperator (char ' *' ) <+> parenTuple s (displayType s)
@@ -134,7 +138,7 @@ prettyTypeTyped t@TyWithConstraints{} = displayTypeTyped (applyCons t)
134
138
prettyTypeTyped x@ TyPromotedCon {} = pretty x
135
139
136
140
prettyTypeTyped (TyWildcard x) = case x of
137
- Just ty -> displayType ty
141
+ Just ty -> displayTypeTyped ty
138
142
Nothing -> skeyword (char ' _' )
139
143
prettyTypeTyped (TySkol v) = stypeSkol (squote <> pretty (v ^. skolVar))
140
144
prettyTypeTyped (TyPi x t) = uncurry (prettyQuantifiers prettyTypeTyped) . second reverse $ unwind t [x] where
@@ -149,16 +153,26 @@ prettyTypeTyped (TyApp (TyCon v _) x) | show (pretty v) == mempty = displayTypeT
149
153
prettyTypeTyped (TyApp x e) = parenTyFun x (displayType x) <+> parenTyArg' e (displayTypeTyped e) where
150
154
parenTyArg' e d | Just _ <- listType e = d
151
155
parenTyArg' e d = parenTyArg e d
152
- prettyTypeTyped (TyRows p rows) = enclose (lbrace <> space) (space <> rbrace) $
153
- pretty p <+> soperator pipe <+> hsep (punctuate comma (displayRows rows))
154
- prettyTypeTyped (TyExactRows rows) = record (displayRowsTyped rows)
155
156
prettyTypeTyped (TyTupleL x y) = parens $ prettyTypeTyped x <> comma <+> prettyTypeTyped y
156
157
prettyTypeTyped (TyTuple t s) =
157
- parenTyFun t (displayType t) <+> soperator (char ' *' ) <+> parenTuple s (displayType s)
158
+ parenTyFun t (displayTypeTyped t) <+> soperator (char ' *' ) <+> parenTuple s (displayTypeTyped s)
158
159
prettyTypeTyped (TyParens t) = parens $ prettyTypeTyped t
159
160
prettyTypeTyped (TyOperator l o r) = prettyTypeTyped l <+> pretty o <+> prettyTypeTyped r
160
161
prettyTypeTyped TyType = keyword " type"
161
162
prettyTypeTyped (TyLit l) = pretty l
163
+ prettyTypeTyped (flatRows -> Just (Just p, rows)) = enclose (lbrace <> space) (space <> rbrace) $
164
+ pretty p <+> soperator pipe <+> hsep (punctuate comma (displayRows rows))
165
+ prettyTypeTyped (flatRows -> Just (Nothing , rows)) = record (displayRowsTyped rows)
166
+ prettyTypeTyped TyRows {} = error " unmatched flatRows"
167
+ prettyTypeTyped TyExactRows {} = error " unmatched flatRows"
168
+
169
+ flatRows :: Type p -> Maybe (Maybe (Type p ), [(Text , Type p )])
170
+ flatRows (TyExactRows rs) = Just (Nothing , rs)
171
+ flatRows (TyRows t rs) =
172
+ case flatRows t of
173
+ Nothing -> Just (Just t, rs)
174
+ Just (t', rs') -> Just (t', rs ++ rs')
175
+ flatRows _ = Nothing
162
176
163
177
displayRows :: (Ord (Var p ), Pretty (Var p )) => [(Text , Type p )] -> [Doc ]
164
178
displayRows = map (\ (n, v) -> text n <+> colon <+> displayType v) . sortOn fst
0 commit comments