From b4562519e003e9d684707b71481f5e9f4fca7f03 Mon Sep 17 00:00:00 2001 From: Paul Cadman Date: Thu, 26 Sep 2024 18:07:54 +0100 Subject: [PATCH] Serialize Nockma output using nock jam The Anoma API accepts jammed nock terms as input. The benefit to this is that jammed terms are greatly compressed compared to the original term. --- app/Commands/Compile/Anoma.hs | 7 ++++--- src/Juvix/Compiler/Nockma/Encoding/Jam.hs | 4 ++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/Commands/Compile/Anoma.hs b/app/Commands/Compile/Anoma.hs index 5b0086158e..516fedf72f 100644 --- a/app/Commands/Compile/Anoma.hs +++ b/app/Commands/Compile/Anoma.hs @@ -3,6 +3,7 @@ module Commands.Compile.Anoma where import Commands.Base import Commands.Compile.Anoma.Options import Commands.Extra.NewCompile +import Juvix.Compiler.Nockma.Encoding.Jam qualified as Encoding import Juvix.Compiler.Nockma.Pretty qualified as Nockma import Juvix.Compiler.Nockma.Translation.FromTree qualified as Nockma @@ -25,9 +26,9 @@ runCommand opts = do res <- getRight r outputAnomaResult (opts' ^. compileDebug) nockmaFile res -outputAnomaResult :: (Members '[EmbedIO, App] r) => Bool -> Path Abs File -> Nockma.AnomaResult -> Sem r () +outputAnomaResult :: (Members '[EmbedIO, App, Files] r) => Bool -> Path Abs File -> Nockma.AnomaResult -> Sem r () outputAnomaResult debugOutput nockmaFile Nockma.AnomaResult {..} = do - let code = Nockma.ppSerialize _anomaClosure + let code = Encoding.jamToByteString _anomaClosure prettyNockmaFile = replaceExtensions' [".debug", ".nockma"] nockmaFile - writeFileEnsureLn nockmaFile code + writeFileBS nockmaFile code when debugOutput (writeFileEnsureLn prettyNockmaFile (Nockma.ppPrint _anomaClosure)) diff --git a/src/Juvix/Compiler/Nockma/Encoding/Jam.hs b/src/Juvix/Compiler/Nockma/Encoding/Jam.hs index 393b174e79..0a7facaf6e 100644 --- a/src/Juvix/Compiler/Nockma/Encoding/Jam.hs +++ b/src/Juvix/Compiler/Nockma/Encoding/Jam.hs @@ -95,6 +95,10 @@ jamToBits = . evalState (initJamState @a) . jamSem +-- | jam encode a Nock term to the bytes encoding of an atom +jamToByteString :: forall a. (Integral a, Hashable a) => Term a -> ByteString +jamToByteString = vectorBitsToByteString . jamToBits + -- | jam encode a Nock term to an atom jam :: forall a r. (Integral a, Hashable a, NockNatural a, Member (Error (ErrNockNatural a)) r) => Term a -> Sem r (Atom a) jam t = do