PCF.hs 1.04 KB
Newer Older
Sven Keidel's avatar
Sven Keidel committed
1 2 3 4
{-# LANGUAGE OverloadedStrings #-}
module Pretty.PCF where

import WildcardSemantics
Jente Hidskes's avatar
Jente Hidskes committed
5
import Text.PrettyPrint
Sven Keidel's avatar
Sven Keidel committed
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

ppPCF :: Term -> Doc
ppPCF t = case t of
  Cons "Var" [v] -> text (show v)
  Cons "App" [e1,e2] ->
    ppPCF e1 <> space <> ppPCF e2
  Cons "Abs" [x,ty,e] ->
    char '\\' <> text (show x) <> char ':' <> ppType ty <> text ". " <> ppPCF e
  Cons "Zero" [] -> char '0'
  Cons "Succ" [e] -> text "succ" <> parens (ppPCF e)
  Cons "Add" [e1, e2] -> parens (ppPCF e1 <> text "+" <> ppPCF e2)
  Cons "Mul" [e1, e2] -> parens (ppPCF e1 <> text "*" <> ppPCF e2)
  Cons "Pred" [e] -> text "pred" <> parens (ppPCF e)
  Cons "Ifz" [e1,e2,e3] -> parens $ text "if " <> ppPCF e1 <> text " then " <> ppPCF e2 <> text " else " <> ppPCF e3
  Wildcard -> ppWildcard
  _ -> error $ "unexpected term: " ++ show t

ppType :: Term -> Doc
ppType t = case t of
  Cons "Num" [] -> text "num"
  Cons "Fun" [e1,e2] -> parens (ppType e1 <> text " -> " <> ppType e2)
  Wildcard -> ppWildcard
  _ -> error $ "unexpected term: " ++ show t

ppWildcard :: Doc
ppWildcard = char '?'