packages feed

phino-0.0.0.7 / src / Printer.hs

{-# LANGUAGE FlexibleInstances #-}{-# LANGUAGE InstanceSigs #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule Printer (printExpression, printProgram, printSubstitutions, printCondition) whereimport Astimport qualified Data.Map.Strict as Mapimport Matcherimport Prettyprinterimport Prettyprinter.Render.String (renderString)import Text.Printf (vFmt)import Yaml (Condition)import qualified Yaml as YprettyMeta :: String -> Doc annprettyMeta meta = pretty "!" <> pretty metaprettyArrow :: Doc annprettyArrow = pretty "↦"prettyDashedArrow :: Doc annprettyDashedArrow = pretty "⤍"instance Pretty Attribute where  pretty (AtLabel name) = pretty name  pretty (AtAlpha index) = pretty "α" <> pretty index  pretty AtRho = pretty "ρ"  pretty AtPhi = pretty "φ"  pretty (AtMeta meta) = prettyMeta metainstance Pretty Binding where  pretty (BiTau attr expr) = pretty attr <+> prettyArrow <+> pretty expr  pretty (BiMeta meta) = prettyMeta meta  pretty (BiDelta bytes) = pretty "Δ" <+> prettyDashedArrow <+> pretty bytes  pretty (BiMetaDelta meta) = pretty "Δ" <+> prettyDashedArrow <+> prettyMeta meta  pretty (BiVoid attr) = pretty attr <+> prettyArrow <+> pretty "∅"  pretty (BiLambda func) = pretty "λ" <+> prettyDashedArrow <+> pretty func  pretty (BiMetaLambda meta) = pretty "λ" <+> prettyDashedArrow <+> prettyMeta metainstance {-# OVERLAPPING #-} Pretty [Binding] where  pretty bindings = vsep (punctuate comma (map pretty bindings))instance Pretty Expression where  pretty (ExFormation []) = pretty "⟦⟧"  pretty (ExFormation [binding]) = case binding of    BiTau _ _ -> vsep [pretty "⟦", indent 2 (pretty binding), pretty "⟧"]    _ -> pretty "⟦" <+> pretty binding <+> pretty "⟧"  pretty (ExFormation bindings) = vsep [pretty "⟦", indent 2 (pretty bindings), pretty "⟧"]  pretty ExThis = pretty "ξ"  pretty ExGlobal = pretty "Φ"  pretty ExTermination = pretty "⊥"  pretty (ExMeta meta) = prettyMeta meta  pretty (ExApplication expr tau) = pretty expr <> vsep [lparen, indent 2 (pretty tau), rparen]  pretty (ExDispatch expr attr) = pretty expr <> pretty "." <> pretty attr  pretty (ExMetaTail expr meta) = pretty expr <+> pretty "*" <+> prettyMeta metainstance Pretty Program where  pretty (Program expr) = pretty "Φ" <+> prettyArrow <+> pretty exprinstance Pretty Tail where  pretty (TaApplication tau) = vsep [lparen, indent 2 (pretty tau), rparen]  pretty (TaDispatch attr) = pretty "." <> pretty attrinstance Pretty MetaValue where  pretty (MvAttribute attr) = pretty attr  pretty (MvBytes bytes) = pretty bytes  pretty (MvBindings []) = pretty "[]"  pretty (MvBindings bindings) = vsep [pretty "[", indent 2 (pretty bindings), pretty "]"]  pretty (MvFunction func) = pretty func  pretty (MvExpression expr) = pretty expr  pretty (MvTail tails) = vsep (punctuate comma (map pretty tails))instance Pretty Subst where  pretty (Subst mp) =    vsep      [ lparen,        indent          2          ( vsep              ( punctuate                  comma                  ( map                      (\(key, value) -> prettyMeta key <+> pretty ">>" <+> pretty value)                      (Map.toList mp)                  )              )          ),        rparen      ]instance Pretty Condition where  pretty (Y.NF expr) = pretty "NF(" <+> pretty expr <+> rpareninstance {-# OVERLAPPING #-} Pretty [Subst] where  pretty :: [Subst] -> Doc ann  pretty [] = pretty "[]"  pretty substs = vsep [pretty "[", indent 2 (vsep (punctuate comma (map pretty substs))), pretty "]"]prettyPrint :: (Pretty a) => a -> StringprettyPrint printable = renderString (layoutPretty defaultLayoutOptions (pretty printable))printCondition :: Condition -> StringprintCondition = prettyPrintprintSubstitutions :: [Subst] -> StringprintSubstitutions = prettyPrintprintExpression :: Expression -> StringprintExpression = prettyPrintprintProgram :: Program -> StringprintProgram = prettyPrint