--}
- | null tt = ppr dc
- | otherwise = cparen (p >= app_prec)
- (ppr dc <+> sep (map (pprTerm app_prec) tt))
-
- where fixity = undefined
-
-pprTerm _ t = pprTerm1 t
-
-pprTerm1 Prim{value=words, ty=ty} = text$ repPrim (tyConAppTyCon ty) words
-pprTerm1 t@Term{} = pprTerm 0 t
-pprTerm1 Suspension{bound_to=Nothing} = char '_' -- <> ppr ct <> char '_'
-pprTerm1 Suspension{mb_ty=Just ty, bound_to=Just n}
- | Just _ <- splitFunTy_maybe ty = ptext SLIT("<function>")
- | otherwise = parens$ ppr n <> text "::" <> ppr ty
-
-
-cPprTerm :: forall m. Monad m =>
- ((Int->Term->m SDoc)->[Int->Term->m (Maybe SDoc)]) -> Term -> m SDoc
-cPprTerm custom = go 0 where
- go prec t@Term{subTerms=tt, dc=dc} = do
- let mb_customDocs = map (($t) . ($prec)) (custom go) :: [m (Maybe SDoc)]
- first_success <- firstJustM mb_customDocs
- case first_success of
- Just doc -> return$ cparen (prec>app_prec+1) doc
--- | dataConIsInfix dc, (t1:t2:tt') <- tt =
- Nothing -> do pprSubterms <- mapM (go (app_prec+1)) tt
- return$ cparen (prec >= app_prec)
- (ppr dc <+> sep pprSubterms)
- go _ t = return$ pprTerm1 t
+-} -- TODO Printing infix constructors properly
+ | null tt = return$ ppr dc
+ | otherwise = do
+ tt_docs <- mapM (y app_prec) tt
+ return$ cparen (p >= app_prec) (ppr dc <+> fsep tt_docs)
+
+pprTermM y p t@NewtypeWrap{} = pprNewtypeWrap y p t
+
+pprTermM _ _ t = pprTermM1 t
+
+pprTermM1 :: Monad m => Term -> m SDoc
+pprTermM1 Prim{value=words, ty=ty} =
+ return$ text$ repPrim (tyConAppTyCon ty) words
+pprTermM1 Term{} = panic "pprTermM1 - unreachable"
+pprTermM1 Suspension{bound_to=Nothing} = return$ char '_'
+pprTermM1 Suspension{mb_ty=Just ty, bound_to=Just n}
+ | Just _ <- splitFunTy_maybe ty = return$ ptext SLIT("<function>")
+ | otherwise = return$ parens$ ppr n <> text "::" <> ppr ty
+pprTermM1 _ = panic "pprTermM1"
+
+pprNewtypeWrap y p NewtypeWrap{ty=ty, wrapped_term=t}
+ | Just (tc,_) <- splitNewTyConApp_maybe ty
+ , ASSERT(isNewTyCon tc) True
+ , Just new_dc <- maybeTyConSingleCon tc = do
+ real_term <- y 10 t
+ return$ cparen (p >= app_prec) (ppr new_dc <+> real_term)
+pprNewtypeWrap _ _ _ = panic "pprNewtypeWrap"
+
+-------------------------------------------------------
+-- Custom Term Pretty Printers
+-------------------------------------------------------
+
+-- We can want to customize the representation of a
+-- term depending on its type.
+-- However, note that custom printers have to work with
+-- type representations, instead of directly with types.
+-- We cannot use type classes here, unless we employ some
+-- typerep trickery (e.g. Weirich's RepLib tricks),
+-- which I didn't. Therefore, this code replicates a lot
+-- of what type classes provide for free.
+
+-- Concretely a custom term printer takes an explicit
+-- recursion knot, and produces a list of Term Processors,
+-- which additionally need a precedence value to
+-- either produce a SDoc or fail (and they do this in some monad m).
+
+type Precedence = Int
+type RecursionKnot m = Precedence -> Term -> m SDoc
+type CustomTermPrinter m = RecursionKnot m
+ -> [Precedence -> Term -> (m (Maybe SDoc))]
+
+-- Takes a list of custom printers with a explicit recursion knot and a term,
+-- and returns the output of the first succesful printer, or the default printer
+cPprTerm :: Monad m => CustomTermPrinter m -> Term -> m SDoc
+cPprTerm printers_ = go 0 where
+ printers = printers_ go
+ go prec t | isTerm t || isNewtypeWrap t = do
+ let default_ = Just `liftM` pprTermM go prec t
+ mb_customDocs = [pp prec t | pp <- printers] ++ [default_]
+ Just doc <- firstJustM mb_customDocs
+ return$ cparen (prec>app_prec+1) doc
+ go _ t = pprTermM1 t
+