Skip to content

Conversation

RyanGlScott
Copy link
Collaborator

This adds support for pretty-printing expressions that update fields of a struct.

Fixes #526.

@ivanperez-keera
Copy link
Member

ivanperez-keera commented Aug 30, 2024

Change Manager:

  • Do we want to use the operators ## and =: instead of printing UpdateField ... at ... to? The answer may be "No", I'm just asking if one is preferable to the other and, if so, why.
  • Can you please rebase on top of the current master HEAD?

@RyanGlScott
Copy link
Collaborator Author

  • Do we want to use the operators ## and =: instead of printing UpdateField ... at ... to? The answer may be "No", I'm just asking if one is preferable to the other and, if so, why.

Actually, I think I would prefer the use of ## and =: when pretty-printing. The only reason I went with UpdateField was because the in-progress array update patch (see #36) used a similar approach when pretty-printing array updates, and I was just copying prior art.

I'll update the patch to use the infix notation.

Can you please rebase on top of the current master HEAD?

Sure.

@RyanGlScott RyanGlScott force-pushed the develop-copilot-prettyprinter-UpdateField branch from d6d0dad to cc9ca22 Compare August 30, 2024 12:15
@RyanGlScott
Copy link
Collaborator Author

I've pushed a new implementation. Let me know if you'd prefer changing anything about the layout or naming conventions in use.

@ivanperez-keera
Copy link
Member

Change Manager: Verified that:

  • Solution is implemented:
    • The code proposed compiles and passes all tests. Details:
      Build log: https://app.travis-ci.com/github/Copilot-Language/copilot/builds/272116442

    • The solution proposed produces the expected result. Details:
      The following Dockerfile 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
          
      --- UpdateFieldPP.hs
      {-# 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'

      Command (substitute variables based on new path after merge):

      $ docker run -e "REPO=https://github.com/GaloisInc/copilot-1" -e "NAME=copilot-1" -e "COMMIT=cc9ca2276e86676ef41393412224cff96c952a61" -it copilot-verify-526
      
  • Implementation is documented. Details:
    No new functions are provided; just an update to an existing function.
  • Change history is clear.
  • Commit messages are clear.
  • Changelogs are updated.
  • Examples are updated. Details:
    No updates needed.
  • Required version bumps are evaluated. Details:
    Bump not needed (bug fix).

@ivanperez-keera ivanperez-keera merged commit 6ea0338 into Copilot-Language:master Sep 2, 2024
1 check passed
@RyanGlScott RyanGlScott deleted the develop-copilot-prettyprinter-UpdateField branch May 18, 2025 12:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

copilot-prettyprinter: Support pretty-printing struct updates
2 participants