Commit 7beb23c9 authored by Bastian Kauschke's avatar Bastian Kauschke
Browse files

prettyImpl

parent 8cf7bf2d
......@@ -56,7 +56,6 @@ library
, Copar.Algorithm.Split
, Copar.FunctorExpression.Type
, Copar.FunctorExpression.Parser
, Copar.FunctorExpression.Pretty
, Copar.FunctorExpression.Printable
, Copar.FunctorExpression.Sorts
, Copar.FunctorExpression.Desorting
......
module Copar.FunctorExpression.Pretty
( PrettyPrint(..)
, pretty
) where
import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy.Builder as Build
import Data.Text.Lazy.Builder (Builder)
import Copar.FunctorExpression.Parser (Precedence)
import Copar.FunctorExpression.Type
class PrettyPrint f where
prettyPrint :: f Builder -> Builder
pretty :: (PrettyPrint f, Functor f) => FunctorExpression f Precedence -> Text
pretty = Build.toLazyText . prettyImpl 0
prettyImpl ::
(PrettyPrint f, Functor f)
=> Precedence
-> FunctorExpression f Precedence
-> Builder
prettyImpl precedenceOuter (Functor precedenceInner f)
| precedenceOuter >= precedenceInner = "(" <> recurse <> ")"
| otherwise = recurse
where
recurse = prettyPrint (fmap (prettyImpl precedenceInner) f)
prettyImpl _ Variable = "X"
......@@ -2,13 +2,10 @@ module Copar.FunctorExpression.Printable
( Printable (..)
, PrintConfig (..)
, printTree
, sortAsScope
, defaultPrintConfig
) where
import Data.Text.Lazy.Builder as Build
import Data.Text.Lazy.Builder.Int as Build
import Copar.FunctorExpression.Sorts (Sort)
-- | Print the required skeleton for the current scope.
--
......@@ -19,9 +16,6 @@ printSkeleton h xs =
(if null h then "└ " else "├ ")
xs
sortAsScope :: Sort -> Build.Builder
sortAsScope sort = "'" <> Build.decimal sort
printTree :: (a -> ([(Build.Builder, a)], Build.Builder)) -> a -> Build.Builder
printTree f head =
let (children, functor) = f head
......@@ -38,6 +32,11 @@ printInner _ [] = mempty
data PrintConfig = PrintConfig { withName :: Build.Builder -> Build.Builder }
defaultPrintConfig :: PrintConfig
defaultPrintConfig = PrintConfig {
withName = \name -> name <> " "
}
class Printable f where
-- | The operator precedence of the given functor,
--
......
......@@ -11,6 +11,7 @@ module Copar.FunctorExpression.Sorts
, annotateSorts
-- * Extracting a sort table
, sortTable
, sortAsScope
-- * Printing a sort table
-- $printing
, printSorts
......@@ -33,7 +34,7 @@ import qualified Data.Vector.Mutable as VM
import Control.DeepSeq (NFData)
import Copar.FunctorExpression.Type
import Copar.FunctorExpression.Pretty
import Copar.FunctorExpression.Printable
import Copar.PrettyShow
-- | Sorts are basically unique integers for every sub-expression in a functor
......@@ -51,6 +52,9 @@ data Sorted a = Sorted
instance PrettyShow a => PrettyShow (Sorted a) where
prettyShow s = "(" <> prettyShow (sortedSort s) <> ", " <> prettyShow (sortedElem s) <> ")"
sortAsScope :: Sort -> Build.Builder
sortAsScope sort = "'" <> Build.decimal sort
-- | Assigns each sub-expression a different sort, starting with 1.
annotateSorts :: Traversable f => FunctorExpression f a -> FunctorExpression f Sort
annotateSorts expr = evalState (traverse anon expr) initState
......@@ -91,7 +95,7 @@ sortTable expr = V.create $ do
--
-- See 'showSorts' for details on the format of this table.
printSorts ::
(PrettyPrint f, Foldable f, Functor f) => FunctorExpression f Sort -> IO ()
(Printable f, Foldable f, Functor f) => FunctorExpression f Sort -> IO ()
printSorts = mapM_ (TIO.putStrLn . Build.toLazyText) . printSortsHelper2
-- | Format all sorts of a sorted functor expression as a table.
......@@ -101,16 +105,16 @@ printSorts = mapM_ (TIO.putStrLn . Build.toLazyText) . printSortsHelper2
-- > 1: Func1 X
-- > 2: Func2 X X
-- > ...
showSorts :: (PrettyPrint f, Foldable f, Functor f) => FunctorExpression f Sort -> Text
showSorts :: (Printable f, Foldable f, Functor f) => FunctorExpression f Sort -> Text
showSorts expr =
T.toStrict
(Build.toLazyText (mconcat (intersperse "\n" (printSortsHelper2 expr))))
printSortsHelper2 ::
forall f. (Functor f, Foldable f, PrettyPrint f)
forall f. (Functor f, Foldable f, Printable f)
=> FunctorExpression f Sort
-> [Builder]
printSortsHelper2 = map print . zip [1..] . toList . sortTable
where
print :: (Int, f ()) -> Builder
print (i, f) = Build.decimal i <> ": " <> prettyPrint (fmap (const "X") f)
print (i, f) = Build.decimal i <> ": " <> printMe defaultPrintConfig (fmap (const "X") f)
......@@ -5,7 +5,7 @@ import Data.Text.Lazy as Lazy
import Data.Text.Lazy.Builder as Build
import Copar.FunctorExpression.Printable
import Copar.FunctorExpression.Sorts (Sort)
import Copar.FunctorExpression.Sorts (Sort, sortAsScope)
import Copar.FunctorExpression.Type (FunctorExpression (..))
printFunctor :: (Printable f, Foldable f, Functor f) => FunctorExpression f Sort -> T.Text
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment