Skip to content

Add specTypeToLaurelActual for unified PySpec type mapping#665

Draft
joehendrix wants to merge 2 commits intomainfrom
jhx/spec-type-mapping
Draft

Add specTypeToLaurelActual for unified PySpec type mapping#665
joehendrix wants to merge 2 commits intomainfrom
jhx/spec-type-mapping

Conversation

@joehendrix
Copy link
Contributor

Summary

  • New specTypeToLaurelActual: Unified function that maps PySpec types directly to Laurel types with preconditions. Value types (int, str, bool, float, list, dict) become Core(Any) with isfrom_X preconditions. Heap types (user-defined classes) become UserDefined(Composite).
  • Updated funcDeclToLaurel: Now uses specTypeToLaurelActual for all parameter and return types, generating typed preconditions. Opaque procedures store postconditions in Body.Opaque.
  • Union/Optional support: Optional[T] patterns generate Or(isfrom_none, isfrom_T) preconditions. Homogeneous literal unions (all strings, all ints) get single-type constraints.
  • Helper functions: anyConstructorForType and anyDestructorForType for Any↔typed wrapping/unwrapping.
  • List validation: Rejects List[CompositeType] since composites live on the heap and can't be embedded in Any.

Known limitation

The self parameter for instance methods is not added in this PR. Adding it here would cause arity mismatches since PythonToLaurel callers don't yet prepend self. This is tracked for a follow-up PR.

Test plan

  • lake build passes cleanly (no warnings)
  • All ToLaurelTest guard_msgs expectations updated and passing
  • All AnalyzeLaurelTest cases pass (26 tests including precondition verification)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

🤖 Generated with Claude Code

Replace the two-step specTypeToLaurelType/preconditionForType pattern
with specTypeToLaurelActual that directly maps PySpec types to their
Laurel representation:
- Value types (primitives, lists, dicts) → Core(Any) with isfrom_X preconditions
- Heap types (user-defined classes) → UserDefined(Composite)
- Optional/union patterns → Or(isfrom_none, isfrom_X) preconditions

funcDeclToLaurel now uses specTypeToLaurelActual for parameters and
return types, generating typed preconditions and postconditions.
Opaque procedures without preconditions use postconditions in Body.Opaque.

Also adds anyConstructorForType/anyDestructorForType helpers for
Any↔typed wrapping/unwrapping, and validates List element types
to reject composites incompatible with Any.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@joehendrix joehendrix requested a review from a team March 25, 2026 23:48
PySpec instance methods now include a `self: Composite` first parameter.
translateCall in PythonToLaurel prepends the target object as self when
calling resolved instance methods, fixing arity mismatches.

Also removes the transparent-only guard on Attribute calls — all resolved
procedures (not just those with preconditions) now emit StaticCall.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@joehendrix joehendrix marked this pull request as draft March 26, 2026 07:04
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.

1 participant