types - Haskell: Pretty print infix and prefix -


i have type represent haskell types:

data type      = tapp type type     | tvar name     | tlit name infixl 8 `tapp`  -- -> b atob = tlit "fun" `tapp` tvar "a" `tapp` tvar "b"  -- maybe (io int) maybeioint = tlit "maybe" `tapp` (tlit "io" `tapp` tlit "int") 

i want print haskell does, namely, literals symbols printed infix while other literal printed prefix. parenthesis should added when necessary:

show atob = "a -> b" show maybeioint = "maybe (io int)"  show ast = ??? 

how can implement this?

the usual way thread through precedence variable printing function. also, should prefer pretty printing library instead of using raw strings (both performance reasons, , ease). ghc ships pretty , recommend newer prettyprinter.

using former (and assuming type name = string):

import text.prettyprint.hughespj  prettytype :: type -> doc prettytype = go 0       go :: int -> type -> doc     go _ (tvar x) = text x     go _ (tlit n) = text n     go n (tlit "fun" `tapp` l `tapp` r) = maybeparens (n > 0) (go 1 l <+> text "->" <+> go 0 r)     go n (l `tapp` r) = maybeparens (n > 1) (go 1 l <+> go 2 r) 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -