Skip to content

Commit

Permalink
Serialize Nockma output using nock jam
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
paulcadman committed Oct 3, 2024
1 parent 3585519 commit b456251
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 4 additions & 3 deletions app/Commands/Compile/Anoma.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))
4 changes: 4 additions & 0 deletions src/Juvix/Compiler/Nockma/Encoding/Jam.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b456251

Please sign in to comment.