Skip to content

Commit

Permalink
Ramsay t/u casereduce (#6591)
Browse files Browse the repository at this point in the history
* CaseReduce translation relation and start of decision procedure.

* WIP - Messy Agda in the decision procedure needs rethinking.

* WIP - Messy Agda in the decision procedure needs rethinking.

* Working Case Reduce and decision procedure.
  • Loading branch information
ramsay-t authored Oct 22, 2024
1 parent cfd420c commit 9b4e76a
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
7 changes: 7 additions & 0 deletions plutus-metatheory/src/Untyped/CEK.lagda.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ open import Relation.Binary.PropositionalEquality using (refl;sym;trans;cong)
open import Data.List using (List;[];_∷_)
open import Data.Product using (_,_)
import Relation.Binary.PropositionalEquality as Eq
open Eq using (_≡_; refl)
open import Untyped using (_⊢)
open _⊢
open import Untyped.RenamingSubstitution using (Sub;sub;lifts)
Expand Down Expand Up @@ -601,6 +604,10 @@ lookup? n [] = nothing
lookup? zero (x ∷ xs) = just x
lookup? (suc n) (x ∷ xs) = lookup? n xs
lookup?-deterministic : {A : Set} {x₁ x₂ : A} → (n : ℕ) → (xs : List A) → lookup? n xs ≡ just x₁ → lookup? n xs ≡ just x₂ → x₁ ≡ x₂
lookup?-deterministic n xs p₁ p₂ with trans (sym p₁) p₂
... | refl = refl
step : State → State
step (s ; ρ ▻ ` x) = s ◅ lookup ρ x
step (s ; ρ ▻ ƛ t) = s ◅ V-ƛ ρ t
Expand Down
117 changes: 117 additions & 0 deletions plutus-metatheory/src/VerifiedCompilation/UCaseReduce.lagda.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
---
title: VerifiedCompilation.UCaseReduce
layout: page
---

# Case-Reduce Translation Phase
```
module VerifiedCompilation.UCaseReduce where
```
## Imports

```
open import VerifiedCompilation.Equality using (DecEq; _≟_;decPointwise)
open import VerifiedCompilation.UntypedViews using (Pred; isCase?; isApp?; isLambda?; isForce?; isBuiltin?; isConstr?; isDelay?; isTerm?; isVar?; allTerms?; iscase; isapp; islambda; isforce; isbuiltin; isconstr; isterm; allterms; isdelay; isvar)
open import VerifiedCompilation.UntypedTranslation using (Translation; translation?; Relation; convert; reflexive)
open import Relation.Nullary.Product using (_×-dec_)
open import Untyped using (_⊢; case; builtin; _·_; force; `; ƛ; delay; con; constr; error; toWellScoped)
import Relation.Binary.PropositionalEquality as Eq
open Eq using (_≡_; refl)
open import Relation.Binary.PropositionalEquality.Core using (trans; sym; subst)
open import Untyped.CEK using (lookup?; lookup?-deterministic)
open import Data.Nat using (ℕ; zero; suc)
open import Data.List using (List; _∷_; []; [_])
open import Data.Maybe using (Maybe; just; nothing)
open import Data.List.Relation.Binary.Pointwise.Base using (Pointwise)
import Relation.Binary as Binary using (Decidable)
open import Relation.Nullary using (Dec; yes; no; ¬_)
open import Data.Product using (_,_)
open import RawU using (tag2TyTag; tmCon)
open import Agda.Builtin.Int using (Int)
open import Data.Empty using (⊥)
open import Function using (case_of_)
```

## Translation Relation

```
iterApp : {X : Set} → X ⊢ → List (X ⊢) → X ⊢
iterApp x [] = x
iterApp x (v ∷ vs) = iterApp (x · v) vs
data CaseReduce : Relation where
casereduce : {X : Set} {{_ : DecEq X}} {x : X ⊢} { x' : X ⊢} {vs xs : List (X ⊢)} {i : ℕ}
→ lookup? i xs ≡ just x
→ Translation CaseReduce (iterApp x vs) x'
→ CaseReduce (case (constr i vs) xs) x'
```
## Decision Procedure

```
isCaseReduce? : {X : Set} {{_ : DecEq X}} → Binary.Decidable (Translation CaseReduce {X})
justEq : {X : Set} {x x₁ : X} → (just x) ≡ (just x₁) → x ≡ x₁
justEq refl = refl
{-# TERMINATING #-}
isCR? : {X : Set} {{_ : DecEq X}} → Binary.Decidable CaseReduce
isCR? ast ast' with (isCase? (isConstr? allTerms?) allTerms?) ast
... | no ¬p = no λ { (casereduce _ _) → ¬p (iscase (isconstr _ (allterms _)) (allterms _)) }
... | yes (iscase (isconstr i (allterms vs)) (allterms xs)) with lookup? i xs in xv
... | nothing = no λ { (casereduce p _) → case trans (sym xv) p of λ { () } }
... | just x with isCaseReduce? (iterApp x vs) ast'
... | yes p = yes (casereduce xv p)
... | no ¬t = no λ { (casereduce p t) → ¬t (subst (λ x → Translation CaseReduce (iterApp x vs) ast') (justEq (trans (sym p) xv)) t)}
isCaseReduce? = translation? isCR?
```
## An Example:

(program 1.1.0
(case (constr 1 (con integer 12) (con integer 42)) (lam x (lam y x)) (lam x (lam y (case (constr 0 (con integer 99)) y))) )
)

becomes:

(program 1.1.0 [ (con integer 42) (con integer 99) ])

_Compiler version: _
```
integer : RawU.TyTag
integer = tag2TyTag RawU.integer
con-integer : {X : Set} → ℕ → X ⊢
con-integer n = (con (tmCon integer (Int.pos n)))
```
This simple example applies the rule once, and works
```
ast₁ : (Maybe ⊥) ⊢
ast₁ = (case (constr 0 [ (con-integer 99) ]) [ (` nothing) ])
ast₁' : (Maybe ⊥) ⊢
ast₁' = ((` nothing) · (con-integer 99))
_ : CaseReduce ast₁ ast₁'
_ = casereduce refl reflexive
```
The longer example definately executes in the compiler, but requires some true β-reduction to make work here.
```
ast : ⊥ ⊢
ast = (case (constr 1 ((con-integer 12) ∷ (con-integer 42) ∷ [])) ( (ƛ (ƛ (` (just nothing)))) ∷ (ƛ (ƛ (case (constr 0 [ (con-integer 99) ]) [ (` nothing) ]))) ∷ []) )
ast' : ⊥ ⊢
ast' = ((con-integer 42) · (con-integer 99))
{-
_ : CaseReduce ast ast'
_ = casereduce refl {!!}
-- This would require unpacking the meaning of the lambdas and applications, not just the AST,
-- so is beyond the scope of this translation relation.
-}
```

1 comment on commit 9b4e76a

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Plutus Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.05.

Benchmark suite Current: 9b4e76a Previous: cfd420c Ratio
validation-auction_2-4 902.2 μs 803.1 μs 1.12
validation-auction_2-5 311.5 μs 230.1 μs 1.35
validation-future-increase-margin-3 764.1 μs 535.6 μs 1.43
validation-future-increase-margin-4 692.1 μs 487 μs 1.42
validation-future-increase-margin-5 1163 μs 811.6 μs 1.43
validation-future-pay-out-1 358.9 μs 250.7 μs 1.43
validation-future-pay-out-2 766.3 μs 538.1 μs 1.42
validation-future-pay-out-3 610.4 μs 537.6 μs 1.14
validation-future-pay-out-4 1089 μs 816.5 μs 1.33
validation-future-settle-early-1 357.7 μs 251.6 μs 1.42
validation-future-settle-early-2 746.5 μs 538.2 μs 1.39
validation-future-settle-early-3 579.2 μs 539.3 μs 1.07
validation-game-sm-success_2-2 284.4 μs 265 μs 1.07
validation-game-sm-success_2-3 907.9 μs 644.4 μs 1.41
validation-multisig-sm-6 506.2 μs 477.9 μs 1.06
validation-multisig-sm-7 442.1 μs 410.3 μs 1.08
validation-multisig-sm-8 552.8 μs 489.1 μs 1.13
validation-multisig-sm-9 560.6 μs 393.2 μs 1.43
validation-multisig-sm-10 794.8 μs 558.4 μs 1.42
validation-ping-pong-1 464.5 μs 328.2 μs 1.42
validation-ping-pong-2 465.3 μs 329.6 μs 1.41
validation-uniswap-1 436.6 μs 414.6 μs 1.05
validation-decode-auction_1-1 268.1 μs 188.9 μs 1.42
validation-decode-auction_1-2 742.5 μs 523.6 μs 1.42
validation-decode-auction_1-3 743.8 μs 525.1 μs 1.42
validation-decode-auction_1-4 271.6 μs 191.4 μs 1.42
validation-decode-auction_2-1 271 μs 189.5 μs 1.43
validation-decode-auction_2-2 741.8 μs 524.4 μs 1.41
validation-decode-auction_2-3 643.6 μs 524.7 μs 1.23
validation-decode-crowdfunding-success-1 332.7 μs 232.6 μs 1.43
validation-decode-crowdfunding-success-2 333.4 μs 231.8 μs 1.44
validation-decode-crowdfunding-success-3 334.8 μs 232.9 μs 1.44
validation-decode-currency-1 321.5 μs 229 μs 1.40
validation-decode-escrow-redeem_1-1 443.1 μs 311.6 μs 1.42
validation-decode-escrow-redeem_1-2 429.9 μs 312.4 μs 1.38
validation-decode-game-sm-success_2-5 723.5 μs 511 μs 1.42
validation-decode-stablecoin_2-4 199.4 μs 174.4 μs 1.14
nofib-clausify/formula1 4305 μs 3890 μs 1.11
nofib-clausify/formula2 5734 μs 4070.9999999999995 μs 1.41
nofib-clausify/formula3 15710 μs 11150 μs 1.41
nofib-clausify/formula4 36230 μs 25650 μs 1.41
nofib-clausify/formula5 76080 μs 53930 μs 1.41
nofib-knights/4x4 25090 μs 18880 μs 1.33
nofib-knights/6x6 65990 μs 58520 μs 1.13
nofib-primetest/50digits 113400 μs 103500 μs 1.10
nofib-queens4x4/bjbt1 9174 μs 6499 μs 1.41
nofib-queens4x4/bjbt2 8606 μs 6119 μs 1.41
nofib-queens4x4/fc 19370 μs 13730 μs 1.41
nofib-queens5x5/bt 102800 μs 73260 μs 1.40
nofib-queens5x5/bm 107400 μs 76530 μs 1.40
nofib-queens5x5/bjbt1 120300 μs 85780 μs 1.40
nofib-queens5x5/bjbt2 116700 μs 92650 μs 1.26
marlowe-semantics/0001000101000000010101000001000001010101010100000001000001010000 621.6 μs 453.2 μs 1.37
marlowe-semantics/0543a00ba1f63076c1db6bf94c6ff13ae7d266dd7544678743890b0e8e1add63 1429 μs 1014.9999999999999 μs 1.41
marlowe-semantics/0705030002040601010206030604080208020207000101060706050502040301 1278 μs 987.3 μs 1.29
marlowe-semantics/0bcfd9487614104ec48de2ea0b2c0979866a95115748c026f9ec129384c262c4 1576 μs 1122 μs 1.40
marlowe-semantics/0be82588e4e4bf2ef428d2f44b7687bbb703031d8de696d90ec789e70d6bc1d8 1929 μs 1371 μs 1.41
marlowe-semantics/0f1d0110001b121d051e15140c0c05141d151c1f1d201c040f10091b020a0e1a 669.7 μs 474.3 μs 1.41
marlowe-semantics/119fbea4164e2bf21d2b53aa6c2c4e79414fe55e4096f5ce2e804735a7fbaf91 1072 μs 762.6 μs 1.41
marlowe-semantics/12910f24d994d451ff379b12c9d1ecdb9239c9b87e5d7bea570087ec506935d5 687.4 μs 489.9 μs 1.40
marlowe-semantics/18cefc240debc0fcab14efdd451adfd02793093efe7bc76d6322aed6ddb582ad 1064 μs 756.5 μs 1.41
marlowe-semantics/1a2f2540121f09321216090b2b1f211e3f020c2c133a1a3c3f3c232a26153a04 424.3 μs 302.3 μs 1.40
marlowe-semantics/1a573aed5c46d637919ccb5548dfc22a55c9fc38298d567d15ee9f2eea69d89e 1281 μs 914.7 μs 1.40
marlowe-semantics/1d56060c3b271226064c672a282663643b1b0823471c67737f0b076870331260 1090 μs 776.1 μs 1.40
marlowe-semantics/1d6e3c137149a440f35e0efc685b16bfb8052ebcf66ec4ad77e51c11501381c7 426.2 μs 304.1 μs 1.40
marlowe-semantics/1f0f02191604101e1f201016171604060d010d1d1c150e110a110e1006160a0d 1415 μs 1025 μs 1.38
marlowe-semantics/202d273721330b31193405101e0637202e2a0f1140211c3e3f171e26312b0220 8141 μs 5994 μs 1.36
marlowe-semantics/21953bf8798b28df60cb459db24843fb46782b19ba72dc4951941fb4c20d2263 502.6 μs 396.2 μs 1.27
marlowe-semantics/238b21364ab5bdae3ddb514d7001c8feba128b4ddcf426852b441f9a9d02c882 426.6 μs 405.4 μs 1.05
marlowe-semantics/26e24ee631a6d927ea4fb4fac530cfd82ff7636986014de2d2aaa460ddde0bc3 799.2 μs 570.3 μs 1.40
marlowe-semantics/2797d7ac77c1b6aff8e42cf9a47fa86b1e60f22719a996871ad412cbe4de78b5 2585 μs 1887 μs 1.37
marlowe-semantics/28fdce478e179db0e38fb5f3f4105e940ece450b9ce8a0f42a6e313b752e6f2c 1294 μs 924.6 μs 1.40
marlowe-semantics/2cb21612178a2d9336b59d06cbf80488577463d209a453048a66c6eee624a695 1109 μs 787 μs 1.41
marlowe-semantics/2f58c9d884813042bce9cf7c66048767dff166785e8b5183c8139db2aa7312d1 1084 μs 772.6 μs 1.40
marlowe-semantics/30aa34dfbe89e0c43f569929a96c0d2b74c321d13fec0375606325eee9a34a6a 1640 μs 1164 μs 1.41
marlowe-semantics/322acde099bc34a929182d5b894214fc87ec88446e2d10625119a9d17fa3ec3d 427 μs 303.9 μs 1.41
marlowe-semantics/331e4a1bb30f28d7073c54f9a13c10ae19e2e396c299a0ce101ee6bf4b2020db 529.3 μs 466.8 μs 1.13
marlowe-semantics/7529b206a78becb793da74b78c04d9d33a2540a1abd79718e681228f4057403a 993.2 μs 803.4 μs 1.24
marlowe-semantics/75a8bb183688bce447e00f435a144c835435e40a5defc6f3b9be68b70b4a3db6 980.4 μs 698.9 μs 1.40
marlowe-semantics/7a758e17486d1a30462c32a5d5309bd1e98322a9dcbe277c143ed3aede9d265f 718.9 μs 511.2 μs 1.41
marlowe-semantics/7cbc5644b745f4ea635aca42cce5e4a4b9d2e61afdb3ac18128e1688c07071ba 676.7 μs 483 μs 1.40
marlowe-semantics/82213dfdb6a812b40446438767c61a388d2c0cfd0cbf7fd4a372b0dc59fa17e1 1793 μs 1283 μs 1.40
marlowe-semantics/8c7fdc3da6822b5112074380003524f50fb3a1ce6db4e501df1086773c6c0201 1639 μs 1170 μs 1.40
marlowe-semantics/8d9ae67656a2911ab15a8e5301c960c69aa2517055197aff6b60a87ff718d66c 502.5 μs 357.7 μs 1.40
marlowe-semantics/96e1a2fa3ceb9a402f2a5841a0b645f87b4e8e75beb636692478ec39f74ee221 426 μs 304.8 μs 1.40
marlowe-semantics/9fabc4fc3440cdb776b28c9bb1dd49c9a5b1605fe1490aa3f4f64a3fa8881b25 1442 μs 1026 μs 1.41
marlowe-semantics/a85173a832db3ea944fafc406dfe3fa3235254897d6d1d0e21bc380147687bd5 512.3 μs 363.8 μs 1.41
marlowe-semantics/a9a853b6d083551f4ed2995551af287880ef42aee239a2d9bc5314d127cce592 717.1 μs 512.6 μs 1.40
marlowe-semantics/acb9c83c2b78dabef8674319ad69ba54912cd9997bdf2d8b2998c6bfeef3b122 927.6 μs 658.9 μs 1.41
marlowe-semantics/acce04815e8fd51be93322888250060da173eccf3df3a605bd6bc6a456cde871 388.5 μs 351.8 μs 1.10
marlowe-role-payout/03d730a62332c51c7b70c16c64da72dd1c3ea36c26b41cd1a1e00d39fda3d6cc 274.4 μs 246 μs 1.12
marlowe-role-payout/0403020000030204010000030001000202010101000304030001040404030100 253.1 μs 179.8 μs 1.41
marlowe-role-payout/0405010105020401010304080005050800040301010800080207080704020206 280 μs 198.4 μs 1.41
marlowe-role-payout/041a2c3b111139201a3a2c173c392b170e16370d300f2d28342d0f2f0e182e01 279.7 μs 231.9 μs 1.21
marlowe-role-payout/04f592afc6e57c633b9c55246e7c82e87258f04e2fb910c37d8e2417e9db46e5 323.5 μs 293.6 μs 1.10
marlowe-role-payout/057ebc80922f16a5f4bf13e985bf586b8cff37a2f6fe0f3ce842178c16981027 238.5 μs 169.7 μs 1.41
marlowe-role-payout/06317060a8e488b1219c9dae427f9ce27918a9e09ee8ac424afa33ca923f7954 251.8 μs 178.9 μs 1.41
marlowe-role-payout/07658a6c898ad6d624c37df1e49e909c2e9349ba7f4c0a6be5f166fe239bfcae 230.3 μs 164 μs 1.40
marlowe-role-payout/0bdca1cb8fa7e38e09062557b82490714052e84e2054e913092cd84ac071b961 278.7 μs 198.6 μs 1.40
marlowe-role-payout/0c9d3634aeae7038f839a1262d1a8bc724dc77af9426459417a56ec73240f0e0 249.6 μs 176.9 μs 1.41
marlowe-role-payout/0d0f01050a0a0a0b0b050d0404090e0d0506000d0a041003040e0f100e0a0408 246.6 μs 175.7 μs 1.40
marlowe-role-payout/0dbb692d2bf22d25eeceac461cfebf616f54003077a8473abc0457f18e025960 281.8 μs 200.2 μs 1.41
marlowe-role-payout/0e00171d0f1e1f14070d0a00091f07101808021d081e1b120219081312081e15 238.4 μs 170.4 μs 1.40
marlowe-role-payout/0e72f62b0f922e31a2340baccc768104025400cf7fdd7dae62fbba5fc770936d 266.1 μs 189.3 μs 1.41
marlowe-role-payout/0e97c9d9417354d9460f2eb35018d3904b7b035af16ab299258adab93be0911a 263.6 μs 188.2 μs 1.40
marlowe-role-payout/0f010d040810040b10020e040f0e030b0a0d100f0c080c0c05000d04100c100f 277.8 μs 197.7 μs 1.41
marlowe-role-payout/1138a04a83edc0579053f9ffa9394b41df38230121fbecebee8c039776a88c0c 244.1 μs 173.7 μs 1.41
marlowe-role-payout/121a0a1b12030616111f02121a0e070716090a0e031c071419121f141409031d 235.6 μs 167.8 μs 1.40
marlowe-role-payout/159e5a1bf16fe984b5569be7011b61b5e98f5d2839ca7e1b34c7f2afc7ffb58e 241.2 μs 171.8 μs 1.40
marlowe-role-payout/195f522b596360690d04586a2563470f2214163435331a6622311f7323433f1c 234.5 μs 166.8 μs 1.41
marlowe-role-payout/1a20b465d48a585ffd622bd8dc26a498a3c12f930ab4feab3a5064cfb3bc536a 261.9 μs 185.9 μs 1.41
marlowe-role-payout/211e1b6c10260c4620074d2e372c260d38643a3d605f63772524034f0a4a7632 250.8 μs 178.4 μs 1.41
marlowe-role-payout/21a1426fb3fb3019d5dc93f210152e90b0a6e740ef509b1cdd423395f010e0ca 263.6 μs 189.3 μs 1.39
marlowe-role-payout/4121d88f14387d33ac5e1329618068e3848445cdd66b29e5ba382be2e02a174a 279.6 μs 226.7 μs 1.23
marlowe-role-payout/4299c7fcf093a5dbfe114c188e32ca199b571a7c25cb7f766bf49f12dab308be 264.6 μs 199.3 μs 1.33
marlowe-role-payout/452e17d16222a427707fa83f63ffb79f606cc25c755a18b1e3274c964ed5ec99 287.7 μs 204.2 μs 1.41
marlowe-role-payout/46f8d00030436e4da490a86b331fa6c3251425fb8c19556080e124d75bad7bd6 242 μs 172.5 μs 1.40

This comment was automatically generated by workflow using github-action-benchmark.

CC: @IntersectMBO/plutus-core

Please sign in to comment.