Skip to content

Commit

Permalink
Use StorageKey newtype in Storage
Browse files Browse the repository at this point in the history
In Storage we want key terms to be compared as if they had no tags. We
achieve this by introucing a newtype for keys and stripping out the tags
as part of the Hashable/Eq instance definitions
  • Loading branch information
paulcadman committed Mar 28, 2024
1 parent d39cc4e commit 6b29c46
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Nockma/Evaluator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,4 @@ evalProfile inistack initerm =
Cell' typeFormula subFormula _ <- withCrumb (crumb crumbDecodeFirst) (asCell (c ^. operatorCellTerm))
void (evalArg crumbEvalFirst stack typeFormula)
key <- evalArg crumbEvalSecond stack subFormula
fromMaybeM (throwKeyNotInStorage key) (HashMap.lookup key <$> asks (^. storageKeyValueData))
fromMaybeM (throwKeyNotInStorage key) (HashMap.lookup (StorageKey key) <$> asks (^. storageKeyValueData))
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Nockma/Evaluator/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ instance (PrettyCode a, NockNatural a) => PrettyCode (KeyNotInStorage a) where
hashMapKvs <- vsep <$> mapM combineKeyValue (HashMap.toList (_keyNotInStorageStorage ^. storageKeyValueData))
return ("The key" <+> tm <+> "is not found in the storage." <> line <> "Storage contains the following key value pairs:" <> line <> hashMapKvs)
where
combineKeyValue :: (Term a, Term a) -> Sem r (Doc Ann)
combineKeyValue :: (StorageKey a, Term a) -> Sem r (Doc Ann)
combineKeyValue (t1, t2) = do
pt1 <- ppCode t1
pt2 <- ppCode t2
Expand Down
31 changes: 28 additions & 3 deletions src/Juvix/Compiler/Nockma/Evaluator/Storage.hs
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
module Juvix.Compiler.Nockma.Evaluator.Storage where

import Juvix.Compiler.Nockma.Language
import Juvix.Compiler.Nockma.Pretty.Base
import Juvix.Prelude.Base

newtype Storage a = Storage
{_storageKeyValueData :: HashMap (Term a) (Term a)}
{_storageKeyValueData :: HashMap (StorageKey a) (Term a)}

emptyStorage :: (Hashable a) => Storage a
emptyStorage = Storage {_storageKeyValueData = mempty}
newtype StorageKey a = StorageKey {_storageKeyTerm :: Term a}

makeLenses ''Storage
makeLenses ''StorageKey

stripTags :: Term a -> Term a
stripTags = \case
TermAtom a -> TermAtom (set atomTag Nothing a)
TermCell c ->
TermCell
( set cellTag Nothing
. over cellLeft stripTags
. over cellRight stripTags
. over (cellCall . _Just . stdlibCallArgs) stripTags
$ c
)

instance (Eq a) => Eq (StorageKey a) where
s1 == s2 = stripTags (s1 ^. storageKeyTerm) == stripTags (s2 ^. storageKeyTerm)

instance (Hashable a) => Hashable (StorageKey a) where
hashWithSalt s k = hashWithSalt s (stripTags (k ^. storageKeyTerm))

instance (PrettyCode a, NockNatural a) => PrettyCode (StorageKey a) where
ppCode k = ppCode (stripTags (k ^. storageKeyTerm))

emptyStorage :: (Hashable a) => Storage a
emptyStorage = Storage {_storageKeyValueData = mempty}
4 changes: 2 additions & 2 deletions test/Anoma/Compilation/Positive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,8 @@ allTests =
True
( Storage
( HashMap.fromList
[ (sk1, v1),
(sk2, v2)
[ (StorageKey sk1, v1),
(StorageKey sk2, v2)
]
)
)
Expand Down
2 changes: 1 addition & 1 deletion test/Nockma/Eval/Positive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ anomaTest n mainFun args _testCheck _evalInterceptStdlibCalls =
in Test {..}

testWithStorage :: [(Term Natural, Term Natural)] -> Text -> Term Natural -> Term Natural -> Check () -> Test
testWithStorage s = Test defaultEvalOptions Nothing (Storage (HashMap.fromList s))
testWithStorage s = Test defaultEvalOptions Nothing (Storage (HashMap.fromList (first StorageKey <$> s)))

test :: Text -> Term Natural -> Term Natural -> Check () -> Test
test = testWithStorage []
Expand Down

0 comments on commit 6b29c46

Please sign in to comment.