Skip to content

Commit

Permalink
Update stitch-lh benchmark to build with the latest stackage snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
facundominguez committed Dec 5, 2023
1 parent bb02d46 commit 8c593f9
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 115 deletions.
28 changes: 15 additions & 13 deletions tests/benchmarks/stitch-lh/src/Language/Stitch/LH/Check.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import Language.Stitch.LH.Type
import Language.Stitch.LH.Op
import Language.Stitch.LH.Pretty
import Language.Stitch.LH.Unchecked
import Text.PrettyPrint.ANSI.Leijen
import Language.Stitch.LH.Util
import Prettyprinter
import Prettyprinter.Render.Terminal


{-@
Expand Down Expand Up @@ -68,8 +70,8 @@ data Exp
{-@ data ScopedExp = ScopedExp (n :: NumVarsInScope) {e : Exp | numFreeVarsExp e <= n } @-}
data ScopedExp = ScopedExp NumVarsInScope Exp

instance Pretty ScopedExp where
pretty (ScopedExp n e) = pretty (ScopedUExp n (uncheckExp e))
prettyScopedExp :: ScopedExp -> Doc AnsiStyle
prettyScopedExp (ScopedExp n e) = prettyScopedUExp (ScopedUExp n (uncheckExp e))

{-@ uncheckExp :: e:Exp -> { uexp:UExp | numFreeVarsExp e = numFreeVars uexp } @-}
uncheckExp :: Exp -> UExp
Expand Down Expand Up @@ -275,23 +277,23 @@ data TyError
| TypeMismatch ScopedUExp Ty Ty ScopedUExp -- expression expected_type actual_type context
deriving Show

instance Pretty TyError where
pretty = \case
prettyTyError :: TyError -> Doc AnsiStyle
prettyTyError = \case
OutOfScopeGlobal name ->
text "Global variable not in scope:" <+> squotes (text name)
pretty "Global variable not in scope:" <+> squotes (pretty name)
NotAFunction e ty ->
text "Expected a function instead of" <+>
pretty "Expected a function instead of" <+>
squotes (prettyTypedExp e ty)
TypeMismatch e expected actual ctx ->
text "Found" <+> squotes (prettyTypedExp e expected) <$$>
text "but expected type" <+> squotes (pretty actual) <$$>
pretty "Found" <+> squotes (prettyTypedExp e expected) $$
pretty "but expected type" <+> squotes (pretty actual) $$
inTheExpression ctx

prettyTypedExp :: ScopedUExp -> Ty -> Doc
prettyTypedExp e ty = pretty e <+> text ":" <+> pretty ty
prettyTypedExp :: ScopedUExp -> Ty -> Doc AnsiStyle
prettyTypedExp e ty = prettyScopedUExp e <+> pretty ":" <+> pretty ty

inTheExpression :: ScopedUExp -> Doc
inTheExpression e = text "in the expression" <+> squotes (pretty e)
inTheExpression :: ScopedUExp -> Doc AnsiStyle
inTheExpression e = pretty "in the expression" <+> squotes (prettyScopedUExp e)


{-@
Expand Down
15 changes: 8 additions & 7 deletions tests/benchmarks/stitch-lh/src/Language/Stitch/LH/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import Language.Stitch.LH.Check
import Language.Stitch.LH.Op
import Language.Stitch.LH.Type

import Text.PrettyPrint.ANSI.Leijen
import Prettyprinter
import Prettyprinter.Render.Terminal

------------------------------------------------
-- Evaluation
Expand All @@ -58,12 +59,12 @@ data Value
@-}
data Value = VInt Int | VBool Bool | VFun Exp (Value -> Value)

instance Pretty Value where
pretty = \case
VInt i -> int i
VBool True -> text "true"
VBool False -> text "false"
VFun e _ -> pretty (ScopedExp (numFreeVarsExp e) e)
prettyValue :: Value -> Doc AnsiStyle
prettyValue = \case
VInt i -> pretty i
VBool True -> pretty "true"
VBool False -> pretty "false"
VFun e _ -> prettyScopedExp (ScopedExp (numFreeVarsExp e) e)

{-@
// XXX: Why can't we reflect map?
Expand Down
24 changes: 12 additions & 12 deletions tests/benchmarks/stitch-lh/src/Language/Stitch/LH/Monad.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ module Language.Stitch.LH.Monad (
) where

import Language.Stitch.LH.Check
import Language.Stitch.LH.Util

import System.Console.Haskeline

import Text.PrettyPrint.ANSI.Leijen
import Prettyprinter
import Prettyprinter.Render.Terminal

import Control.Monad
import Control.Monad.Trans.Maybe
Expand All @@ -48,8 +48,8 @@ newtype Stitch a = Stitch { unStitch :: MaybeT (StateT Globals (InputT IO)) a }
deriving (Monad, Functor, Applicative, MonadState Globals, MonadIO)

-- | Like the 'Stitch' monad, but also supporting error messages via 'Doc's
newtype StitchE a = StitchE { unStitchE :: ExceptT Doc Stitch a }
deriving (Monad, Functor, Applicative, MonadError Doc)
newtype StitchE a = StitchE { unStitchE :: ExceptT (Doc AnsiStyle) Stitch a }
deriving (Monad, Functor, Applicative, MonadError (Doc AnsiStyle))

instance MonadReader Globals StitchE where
ask = StitchE get
Expand All @@ -63,14 +63,14 @@ instance MonadReader Globals StitchE where
-- | Class for the two stitchorous monads
class StitchM m where
-- | Print a 'Doc' without a newline at the end
printDoc :: Doc -> m ()
printDoc :: Doc AnsiStyle -> m ()

-- | Print a 'Doc' with a newline
printLine :: Doc -> m ()
printLine :: Doc AnsiStyle -> m ()

instance StitchM Stitch where
printDoc = Stitch . liftIO . displayIO stdout . toSimpleDoc
printLine = Stitch . liftIO . displayIO stdout . toSimpleDoc . (<> hardline)
printDoc = Stitch . liftIO . hPutDoc stdout
printLine = Stitch . liftIO . hPutDoc stdout . (<> hardline)

instance StitchM StitchE where
printDoc = StitchE . lift . printDoc
Expand All @@ -84,16 +84,16 @@ prompt = Stitch . lift . lift . getInputLine
-- | Abort the 'Stitch' monad
quit :: Stitch a
quit = do
printLine (text "Good-bye.")
printLine (pretty "Good-bye.")
Stitch mzero

-- | Abort the computation with an error
issueError :: Doc -> StitchE a
issueError :: Doc AnsiStyle -> StitchE a
issueError = StitchE . throwError

-- | Hoist an 'Either' into 'StitchE'
eitherToStitchE :: Either String a -> StitchE a
eitherToStitchE (Left err) = issueError (text err)
eitherToStitchE (Left err) = issueError (pretty err)
eitherToStitchE (Right x) = return x

-- | Run a 'Stitch' computation
Expand All @@ -102,6 +102,6 @@ runStitch thing_inside
= void $ flip evalStateT emptyGlobals $ runMaybeT $ unStitch thing_inside

-- | Run a 'StitchE' computation
runStitchE :: StitchE a -> Stitch (Either Doc a)
runStitchE :: StitchE a -> Stitch (Either (Doc AnsiStyle) a)
runStitchE thing_inside
= runExceptT $ unStitchE thing_inside
22 changes: 11 additions & 11 deletions tests/benchmarks/stitch-lh/src/Language/Stitch/LH/Op.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import Data.Hashable
import Language.Stitch.LH.Type
import Language.Stitch.LH.Util (render)

import Text.PrettyPrint.ANSI.Leijen
import Prettyprinter

{-@
data ArithOp
Expand Down Expand Up @@ -75,16 +75,16 @@ arithType Equals = TBool
-- Pretty-printing

instance Pretty ArithOp where
pretty Plus = char '+'
pretty Minus = char '-'
pretty Times = char '*'
pretty Divide = char '/'
pretty Mod = char '%'
pretty Less = char '<'
pretty LessE = text "<="
pretty Greater = char '>'
pretty GreaterE = text ">="
pretty Equals = text "=="
pretty Plus = pretty '+'
pretty Minus = pretty '-'
pretty Times = pretty '*'
pretty Divide = pretty '/'
pretty Mod = pretty '%'
pretty Less = pretty '<'
pretty LessE = pretty "<="
pretty Greater = pretty '>'
pretty GreaterE = pretty ">="
pretty Equals = pretty "=="

instance Show ArithOp where
show = render . pretty
7 changes: 4 additions & 3 deletions tests/benchmarks/stitch-lh/src/Language/Stitch/LH/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import Language.Stitch.LH.Op
import Language.Stitch.LH.Util
import Language.Stitch.LH.Data.Nat

import Text.PrettyPrint.ANSI.Leijen
import Prettyprinter
import Prettyprinter.Render.Terminal

lamPrec, appPrec, appLeftPrec, appRightPrec, ifPrec :: Prec
lamPrec = 1
Expand Down Expand Up @@ -46,12 +47,12 @@ precInfo GreaterE = (4, 4, 4)
precInfo Equals = (4, 4, 4)

-- | A function that changes a 'Doc's color
type ApplyColor = Doc -> Doc
type ApplyColor = Doc AnsiStyle -> Doc AnsiStyle

-- | The colors used for all rendered expressions
{-@ coloring :: { v : [ApplyColor] | len v > 0 } @-}
coloring :: [ApplyColor]
coloring = [red, green, yellow, blue, magenta, cyan]
coloring = map (annotate . color) [Red, Green, Yellow, Blue, Magenta, Cyan]

{-@ ignore applyColor @-}
-- LH would need a proof that
Expand Down
11 changes: 6 additions & 5 deletions tests/benchmarks/stitch-lh/src/Language/Stitch/LH/Statement.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
----------------------------------------------------------------------------

{-# OPTIONS_GHC -Wno-unused-imports #-}
module Language.Stitch.LH.Statement ( Statement(..) ) where
module Language.Stitch.LH.Statement ( Statement(..), prettyStatement ) where

-- XXX: Import Op so LH doesn't fail with: Unknown type constructor `ArithOp`
import Language.Stitch.LH.Op
import Language.Stitch.LH.Unchecked

import Text.PrettyPrint.ANSI.Leijen
import Prettyprinter
import Prettyprinter.Render.Terminal

-- | A statement can either be a bare expression, which will be evaluated,
-- or an assignment to a global variable.
Expand All @@ -31,6 +32,6 @@ data Statement = BareExp UExp
| NewGlobal String UExp
deriving Show

instance Pretty Statement where
pretty (BareExp e) = pretty (ScopedUExp 0 e)
pretty (NewGlobal v e) = text v <+> char '=' <+> pretty (ScopedUExp 0 e)
prettyStatement :: Statement -> Doc AnsiStyle
prettyStatement (BareExp e) = prettyScopedUExp (ScopedUExp 0 e)
prettyStatement (NewGlobal v e) = pretty v <+> pretty '=' <+> prettyScopedUExp (ScopedUExp 0 e)
54 changes: 27 additions & 27 deletions tests/benchmarks/stitch-lh/src/Language/Stitch/LH/Token.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Language.Stitch.LH.Token (
import Language.Stitch.LH.Util
import Language.Stitch.LH.Op

import Text.PrettyPrint.ANSI.Leijen as Pretty
import Prettyprinter
import Text.Parsec.Pos ( SourcePos, newPos )

import Data.List as List
Expand Down Expand Up @@ -83,7 +83,7 @@ instance Pretty Token where
prettyList = printTogether . List.map printingInfo

instance Show Token where
show = render . pretty
show = show . pretty

instance Pretty LToken where
pretty = pretty . unLoc
Expand All @@ -92,38 +92,38 @@ instance Pretty LToken where
instance Show LToken where
show = render . pretty

type PrintingInfo = (Doc, Bool, Bool)
type PrintingInfo ann = (Doc ann, Bool, Bool)
-- the bools say whether or not to include a space before or a space after

alone :: Doc -> PrintingInfo
alone :: Doc ann -> PrintingInfo ann
alone = (, True, True)

getDoc :: PrintingInfo -> Doc
getDoc :: PrintingInfo ann -> Doc ann
getDoc (doc, _, _) = doc

printingInfo :: Token -> PrintingInfo
printingInfo LParen = (char '(', True, False)
printingInfo RParen = (char ')', False, True)
printingInfo Lambda = (char '\\', True, False)
printingInfo Dot = (char '.', False, True)
printingInfo ArrowTok = alone $ text "->"
printingInfo Colon = (char ':', False, False)
printingInfo :: Token -> PrintingInfo ann
printingInfo LParen = (pretty '(', True, False)
printingInfo RParen = (pretty ')', False, True)
printingInfo Lambda = (pretty '\\', True, False)
printingInfo Dot = (pretty '.', False, True)
printingInfo ArrowTok = alone $ pretty "->"
printingInfo Colon = (pretty ':', False, False)
printingInfo (ArithOp a) = alone $ pretty a
printingInfo (IntTok i) = alone $ int i
printingInfo (BoolTok True) = alone $ text "true"
printingInfo (BoolTok False) = alone $ text "false"
printingInfo If = alone $ text "if"
printingInfo Then = alone $ text "then"
printingInfo Else = alone $ text "else"
printingInfo FixTok = alone $ text "fix"
printingInfo LetTok = alone $ text "let"
printingInfo InTok = alone $ text "in"
printingInfo Assign = alone $ text "="
printingInfo Semi = (char ';', False, True)
printingInfo (Name t) = alone $ text t

printTogether :: [PrintingInfo] -> Doc
printTogether [] = Pretty.empty
printingInfo (IntTok i) = alone $ pretty i
printingInfo (BoolTok True) = alone $ pretty "true"
printingInfo (BoolTok False) = alone $ pretty "false"
printingInfo If = alone $ pretty "if"
printingInfo Then = alone $ pretty "then"
printingInfo Else = alone $ pretty "else"
printingInfo FixTok = alone $ pretty "fix"
printingInfo LetTok = alone $ pretty "let"
printingInfo InTok = alone $ pretty "in"
printingInfo Assign = alone $ pretty "="
printingInfo Semi = (pretty ';', False, True)
printingInfo (Name t) = alone $ pretty t

printTogether :: [PrintingInfo ann] -> Doc ann
printTogether [] = mempty
printTogether pis = getDoc $ List.foldl1 combine pis
where
combine (doc1, before_space, inner_space1) (doc2, inner_space2, after_space)
Expand Down
10 changes: 5 additions & 5 deletions tests/benchmarks/stitch-lh/src/Language/Stitch/LH/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Language.Stitch.LH.Type where

import Language.Stitch.LH.Util (Prec, topPrec, maybeParens)

import Text.PrettyPrint.ANSI.Leijen
import Prettyprinter
import Data.Hashable
import GHC.Generics

Expand Down Expand Up @@ -46,10 +46,10 @@ arrowLeftPrec = 5
arrowRightPrec = 4.9
arrowPrec = 5

pretty_ty :: Prec -> Ty -> Doc
pretty_ty :: Prec -> Ty -> Doc ann
pretty_ty p (TFun arg res) = maybeParens (p >= arrowPrec) $
hsep [ pretty_ty arrowLeftPrec arg
, text "->"
, pretty "->"
, pretty_ty arrowRightPrec res ]
pretty_ty _ TInt = text "Int"
pretty_ty _ TBool = text "Bool"
pretty_ty _ TInt = pretty "Int"
pretty_ty _ TBool = pretty "Bool"
Loading

0 comments on commit 8c593f9

Please sign in to comment.