Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix with-read desugaring to use pattern guard #281

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pact-repl/Pact/Core/IR/Eval/Direct/CoreBuiltin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,16 @@ rawSort info b _env = \case
chargeGasArgs info (GComparison (SortComparisons maxSize (V.length vli)))
vli' <- liftIO $ do
v' <- V.thaw vli
V.sort v'
V.sortBy sortPv v'
V.freeze v'
return (VList vli')
args -> argsError info b args
where
sortPv (PLiteral l) (PLiteral y) = l `compare` y
sortPv (PTime t) (PTime t') = t `compare` t'
sortPv (PLiteral _) (PTime _) = LT
sortPv (PTime _) (PLiteral _) = GT
sortPv _ _ = EQ
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, do you have any justification why this is correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See: Prod sorting


coreRemove :: (IsBuiltin b) => NativeFunction e b i
coreRemove info b _env = \case
Expand Down
8 changes: 7 additions & 1 deletion pact/Pact/Core/IR/Eval/CEK/CoreBuiltin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,16 @@ rawSort info b cont handler _env = \case
chargeGasArgs info (GComparison (SortComparisons maxSize (V.length vli)))
vli' <- liftIO $ do
v' <- V.thaw vli
V.sort v'
V.sortBy sortPv v'
V.freeze v'
returnCEKValue cont handler (VList vli')
args -> argsError info b args
where
sortPv (PLiteral l) (PLiteral y) = l `compare` y
sortPv (PTime t) (PTime t') = t `compare` t'
sortPv (PLiteral _) (PTime _) = LT
sortPv (PTime _) (PLiteral _) = GT
sortPv _ _ = EQ

coreRemove :: (IsBuiltin b) => NativeFunction e b i
coreRemove info b cont handler _env = \case
Expand Down
17 changes: 5 additions & 12 deletions pact/Pact/Core/Serialise/LegacyPact.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- |
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE PatternGuards #-}
{-# LANGUAGE TemplateHaskell #-}

module Pact.Core.Serialise.LegacyPact
Expand Down Expand Up @@ -621,36 +622,28 @@ fromLegacyTerm mh = \case
fn' <- fromLegacyTerm mh fn
case fn' of
Builtin b _ -> case b of
CoreBind -> case args of
[bObj, Legacy.TBinding bps scope _] -> do
CoreBind | [bObj, Legacy.TBinding bps scope _] <- args -> do
bObj' <- fromLegacyTerm mh bObj
lam <- objBindingToLet mh bps scope
pure (App fn' [bObj', lam] ())

_ -> throwError $ "invariant failure: CoreBind"
CoreWithRead -> case args of
[tbl, rowkey, Legacy.TBinding bps scope _] -> do
CoreWithRead | [tbl, rowkey, Legacy.TBinding bps scope _] <- args -> do
tbl' <- fromLegacyTerm mh tbl
rowkey' <- fromLegacyTerm mh rowkey
lam <- objBindingToLet mh bps scope
pure (App fn' [tbl', rowkey', lam] ())

_ -> throwError "invariant failure: CoreWithRead"
CoreWithDefaultRead -> case args of
[tbl, rowkey, defaultObj, Legacy.TBinding bps scope _] -> do
CoreWithDefaultRead | [tbl, rowkey, defaultObj, Legacy.TBinding bps scope _] <- args -> do
tbl' <- fromLegacyTerm mh tbl
rowkey' <- fromLegacyTerm mh rowkey
defaultObj' <- fromLegacyTerm mh defaultObj
lam <- objBindingToLet mh bps scope
pure (App fn' [tbl', rowkey', defaultObj', lam] ())

_ -> throwError "invariant failure: CoreWithDefaultRead"
CoreResume -> case args of
[Legacy.TBinding bps scope _] -> do
CoreResume | [Legacy.TBinding bps scope _] <- args -> do
lam <- objBindingToLet mh bps scope
pure (App fn' [lam] ())

_ -> throwError "invariant failure: CoreWithRead"
-- [HOF Translation]
-- Note: The following sections of translation are explained as follows:
-- we transform, for example `(map (+ k) other-arg)` into
Expand Down