pretty printing

This commit is contained in:
William Ball 2024-10-06 14:02:35 -07:00
parent d5a34360bb
commit 1330966180
2 changed files with 38 additions and 13 deletions

View file

@ -3,6 +3,7 @@
module Expr where module Expr where
import Data.Function (on) import Data.Function (on)
import Data.List (genericDrop)
data Expr where data Expr where
Var :: Integer -> Expr Var :: Integer -> Expr
@ -13,6 +14,29 @@ data Expr where
Pi :: Expr -> Expr -> Expr Pi :: Expr -> Expr -> Expr
deriving (Show, Eq) deriving (Show, Eq)
{- --------------------- PRETTY PRINTING ----------------------------- -}
-- TODO : store parsed identifiers for better printing
genName :: Integer -> String
genName k = case genericDrop k ["x", "y", "z", "w", "u", "v"] of
[] -> 'x' : show (k - 6)
(v : _) -> v
pretty :: Expr -> String
pretty = helper 0
where
helper :: Integer -> Expr -> String
helper k (Var n) = genName $ k - n - 1
helper _ Star = "*"
helper _ Square = ""
helper k (App e1 e2) = "(" ++ helper k e1 ++ " " ++ helper k e2 ++ ")"
helper k (Abs ty b) =
"" ++ genName k ++ " : " ++ helper k ty ++ " . " ++ helper (k + 1) b ++ ")"
helper k (Pi ty b) =
"(∏" ++ genName k ++ " : " ++ helper k ty ++ " . " ++ helper (k + 1) b ++ ")"
{- --------------- ACTUAL MATH STUFF ---------------- -}
isSort :: Expr -> Bool isSort :: Expr -> Bool
isSort Star = True isSort Star = True
isSort Square = True isSort Square = True

View file

@ -1,5 +1,6 @@
module Main where module Main where
import Expr
import Parser import Parser
import System.IO import System.IO
@ -10,4 +11,4 @@ main = do
input <- getLine input <- getLine
case pAll input of case pAll input of
Left err -> putStrLn err Left err -> putStrLn err
Right expr -> print expr Right expr -> putStrLn $ pretty expr