Commit dc74bd5b authored by Bastian Kauschke's avatar Bastian Kauschke
Browse files

introduce custom architecture for print-functor

parent 21191f8c
......@@ -38,6 +38,7 @@ library
, Copar.RefinementInterface
, Copar.Functors
, Copar.FunctorDescription
, Copar.FunctorPrinter
, Copar.Functors.Powerset
, Copar.Functors.Bag
, Copar.Functors.GroupValued
......@@ -56,6 +57,7 @@ library
, Copar.FunctorExpression.Type
, Copar.FunctorExpression.Parser
, Copar.FunctorExpression.Pretty
, Copar.FunctorExpression.Printable
, Copar.FunctorExpression.Sorts
, Copar.FunctorExpression.Desorting
, Copar.FunctorExpression.Transform
......
module Copar.FunctorExpression.Printable
( Printable (..)
, printTree
, sortAsScope
) 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.
--
-- The current scope has already been popped of the stack.
printSkeleton :: [a] -> [[a]] -> Build.Builder
printSkeleton h xs =
foldl (\str e -> (if null e then " " else "│ ") <> str)
(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
in functor <> printInner f [children]
printInner :: (a -> ([(Build.Builder, a)], Build.Builder)) -> [[(Build.Builder, a)]] -> Build.Builder
printInner f (((scope, v):h):rs) =
"\n" <> printSkeleton h rs <> scope <> ": "
<> functor <> (printInner f (children:h:rs))
where
(children, functor) = f v
printInner f ([]:rs) = printInner f rs
printInner _ [] = mempty
class Printable f where
printMe :: f Build.Builder -> Build.Builder
module Copar.FunctorPrinter (printFunctor, Printable) where
import Data.Text as T
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.Type (FunctorExpression (..))
printFunctor :: (Printable f, Foldable f, Functor f) => FunctorExpression f Sort -> T.Text
printFunctor (Functor _ f') = Lazy.toStrict (Build.toLazyText (printTree inner f'))
printFunctor Variable = "Variable X"
inner :: (Printable f, Foldable f, Functor f)
=> f (FunctorExpression f Sort)
-- ([('sort, subfunctor)], functor)
-> ([(Build.Builder, f (FunctorExpression f Sort))], Build.Builder)
inner f = (foldMap getChild f, printMe (fmap printChild f))
where
printChild Variable = "X"
printChild (Functor sort _) = sortAsScope sort
getChild Variable = []
getChild (Functor sort f') = [(sortAsScope sort, f')]
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