Skip to content

Support Any type, Operations, and Kwargs handling for Python->Laurel#489

Open
thanhnguyen-aws wants to merge 42 commits intomainfrom
laureltrans
Open

Support Any type, Operations, and Kwargs handling for Python->Laurel#489
thanhnguyen-aws wants to merge 42 commits intomainfrom
laureltrans

Conversation

@thanhnguyen-aws
Copy link
Contributor

Description of changes:
This PR Any type, Operations, and Kwargs handling for Python->Laurel:

  1. Define type Any and ListAny in PythonLaurelCorePrelude as inductive types.
  2. Change the encoded type for all variables, functions' inputs and outputs to Any type.
  3. Encode Python's operations in PythonLaurelCorePrelude so that they are consistent with Python's semantic.
  4. Support Kwargs handling for Python->Laurel

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@andrewmwells-amazon
Copy link
Contributor

I asked Thanh to create this branch to replace #457 since I can't push to his fork

@keyboardDrummer
Copy link
Contributor

Exciting stuff. Is the Any encoding is used for all values now? What happened to using the statically provided Python types to optimize away the Any?

@andrewmwells-amazon
Copy link
Contributor

Exciting stuff. Is the Any encoding is used for all values now? What happened to using the statically provided Python types to optimize away the Any?

We plan to improve the partial evaluator to reduce them as much as possible. (Initial experiments suggested a 4% slowdown with this approach, which I think is worth the reduced soundness risk)

@keyboardDrummer
Copy link
Contributor

keyboardDrummer commented Mar 2, 2026

We plan to improve the partial evaluator to reduce them as much as possible.

Until you do that, would this change mean you're able to prove less since the verification condition become more complex?

(Initial experiments suggested a 4% slowdown with this approach, which I think is worth the reduced soundness risk)

A 4% slowdown of the compilation when the partial evaluator is able to do the optimization? Are there cases where the partial evaluator won't be able to do the optimization and verification results change?

}


inline function PMul (v1: Any, v2: Any) : Any
Copy link
Contributor

Choose a reason for hiding this comment

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

There are a bunch of bugs here from copying PAdd. E.g.,
(Any..isfrom_string(v1) && Any..isfrom_string(v2)) does concat.

from_float(int_to_real(Any..as_int(v1)) + Any..as_float(v2))
uses + instead of *

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I fixed them

let mut translatedArgs ← finalArgs.mapM (translateExpr ctx)

-- Check if function has a model
if !hasModel ctx funcName then
Copy link
Contributor

Choose a reason for hiding this comment

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

We want to keep this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added it back

| .FloorDiv _ => .ok Operation.Div -- Python // maps to Laurel Div
| .Mod _ => .ok Operation.Mod
| .Div _ => .ok Operation.Div -- Python / (true division)
| .BitAnd _ => .ok Operation.And -- Bitwise & - abstract as logical And
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably want to keep these

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added it back

| .LtE _ => .ok Operation.Leq
| .Gt _ => .ok Operation.Gt
| .GtE _ => .ok Operation.Geq
| .In _ => return mkStmtExprMd .Hole -- Abstract: arbitrary bool (sound)
Copy link
Contributor

Choose a reason for hiding this comment

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

We also want to keep these

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added it back

@andrewmwells-amazon
Copy link
Contributor

andrewmwells-amazon commented Mar 3, 2026

Until you do that, would this change mean you're able to prove less since the verification condition become more complex?

Yes

@andrewmwells-amazon
Copy link
Contributor

A 4% slowdown of the compilation when the partial evaluator is able to do the optimization? Are there cases where the partial evaluator won't be able to do the optimization and verification results change?

This was timing from Core -> SMT (including solver time).

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like to request comments (similar to docString style) in the prelude code below, especially since this is hand-written and has to be maintained manually, and as such, should be held to the same high standard as Lean code in this repo.

Copy link
Contributor

Choose a reason for hiding this comment

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

Preferably, please link to relevant Python documentation in these comments that justifies the design choices that were made.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, please divide the file into sections, with descriptions of what constructs/operations.. etc. are being handled in each section.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added the documents into the prelude.

if (Any..isfrom_string(v)) then !(Any..as_string!(v) == "") else
if (Any..isfrom_int(v)) then !(Any..as_int!(v) == 0) else
false
//WILL BE ADDED
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this another instance of false being a placeholder value?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This function is guarded by the requires clause, so in case v is not one of the 4 types, it will returns false, but the assertion of this function will fail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants