Skip to content

Commit 6ea0338

Browse files
Merge branch 'develop-copilot-prettyprinter-UpdateField'. Close #526.
**Description** Copilot currently supports modifying values from streams of structs, but not pretty-printing them using `copilot-prettyprinter`. **Type** - Bug: exception produced when executing valid specification. **Additional context** - Issue #520 introduced support for modifying structs. **Requester** - Ryan Scott (Galois) **Method to check presence of bug** Running the following specification that pretty-prints a spec with a struct update: ```haskell {-# LANGUAGE DataKinds #-} {-# LANGUAGE NoImplicitPrelude #-} module Main (main) where import Data.Foldable (for_) import Data.Functor (void) import Data.Word (Word32) import qualified Copilot.PrettyPrint as PP import Language.Copilot data S = S { unS :: Field "unS" Word32 } instance Struct S where typeName _ = "s" toValues s = [Value typeOf (unS s)] instance Typed S where typeOf = Struct (S (Field 0)) spec :: Spec spec = do let externS :: Stream S externS = extern "extern_s" Nothing example :: Stream Word32 example = (externS ## unS =: 42) # unS trigger "example" (example == example) [arg externS, arg example] main :: IO () main = do spec' <- reify spec putStrLn $ PP.prettyPrint spec' ``` produces an error message: ``` $ runghc UpdateFieldPP.hs UpdateFieldPP.hs: src/Copilot/PrettyPrint.hs:(79,12)-(102,31): Non-exhaustive patterns in case ``` when it should instead execute correctly and pretty-print the spec. The following Dockerfile, when used with the above file in the context, installs copilot and runs a spec that uses copilot-prettyprinter to pretty print an expression with stream with a struct update in it, followed by "Success": ``` --- Dockerfile FROM ubuntu:focal ENV DEBIAN_FRONTEND=noninteractive RUN apt-get update RUN apt-get install --yes libz-dev RUN apt-get install --yes git RUN apt-get install --yes wget RUN mkdir -p $HOME/.ghcup/bin RUN wget https://downloads.haskell.org/~ghcup/0.1.19.2/x86_64-linux-ghcup-0.1.19.2 -O $HOME/.ghcup/bin/ghcup RUN chmod a+x $HOME/.ghcup/bin/ghcup ENV PATH=$PATH:/root/.ghcup/bin/ ENV PATH=$PATH:/root/.cabal/bin/ RUN apt-get install --yes curl RUN apt-get install --yes gcc g++ make libgmp3-dev RUN apt-get install --yes pkg-config SHELL ["/bin/bash", "-c"] RUN ghcup install ghc 9.4 RUN ghcup install cabal 3.2 RUN ghcup set ghc 9.4.8 RUN cabal update ADD UpdateFieldPP.hs /tmp/UpdateFieldPP.hs CMD git clone $REPO \ && cd $NAME \ && git checkout $COMMIT \ && cabal v1-sandbox init \ && cabal v1-install alex happy \ && cabal v1-install copilot**/ \ && cabal v1-exec -- runhaskell /tmp/UpdateFieldPP.hs \ && echo Success ``` Command (substitute variables based on new path after merge): ``` $ docker run -e "REPO=https://github.com/Copilot-Language/copilot" -e "NAME=copilot" -e "COMMIT=<HASH>" -it copilot-verify-526 ``` **Expected result** Running the dockerfile above prints an expression using struct updates and the message success, indicating that struct updates are supported by the pretty printer. **Solution implemented** Introduce a case for pretty-printing struct updates. **Further notes** None.
2 parents 0f5055e + cc9ca22 commit 6ea0338

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

copilot-prettyprinter/CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2024-08-30
2+
* Add support for pretty-printing struct update expressions. (#526)
3+
14
2024-07-07
25
* Version bump (3.20). (#522)
36

copilot-prettyprinter/src/Copilot/PrettyPrint.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ ppOp2 op = case op of
100100
BwShiftL _ _ -> ppInfix "<<"
101101
BwShiftR _ _ -> ppInfix ">>"
102102
Index _ -> ppInfix ".!!"
103+
UpdateField (Struct _) _ f -> \ doc1 doc2 ->
104+
parens $ doc1 <+> text "##" <+> text (accessorName f) <+> text "=:" <+> doc2
105+
UpdateField _ _ _ -> impossible "ppOp2" "Copilot.PrettyPrint"
103106

104107
-- | Pretty-print a ternary operation.
105108
ppOp3 :: Op3 a b c d -> Doc -> Doc -> Doc -> Doc

0 commit comments

Comments
 (0)