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

test: proptest Type validation and substitution #1744

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from

Conversation

acl-cqc
Copy link
Contributor

@acl-cqc acl-cqc commented Dec 9, 2024

  • Add proptest::Strategys that produce valid/well-formed Types/etc.
    • CustomTypes being correct wrt. the TypeDef's in the ExtensionRegistry passed in
    • such that any TypeArgs are correct for the TypeParams into which they must fit (e.g. in those TypeDefs)
    • containing variables, with the strategy also returning an appropriate "environment" i.e. Vec<TypeParam> declaring all variables that are used
    • (all this done by a fold or "State monad"-like mechanism of passing in an environment to each Strategy that then returns a copy of that environment with potentially more variables.)

Using std_reg(), thus produce types and valid substitutions for the variables in those types; check the type is valid in the original environment, and after substitution, is valid in the environment for the substituted-in values.

Coverage of making TypeArgs for TypeParam::{List, Tuple, Extensions} is missing because the std_reg() does not declare any types taking those kinds of type arguments (!)....hence, TODOs:

  • add/generate row variables, this should give coverage of TypeParam::List
  • add some tests with strategies that prop_filter for "interesting" cases, to check such are actually generated. This may allow tweaking/improving probabilities/distributions.
  • decide how to organise code. Question: what do folks think about removing old Type proptests (should be subsumed by this, but potentially simpler/easier-to-understand)

And, extend this to test substitution of nodes/operations - in time, that will probably be a second PR.

Copy link

codecov bot commented Dec 9, 2024

Codecov Report

Attention: Patch coverage is 82.10117% with 46 lines in your changes missing coverage. Please review.

Project coverage is 86.64%. Comparing base (21ac35c) to head (c43062b).
Report is 23 commits behind head on main.

Files with missing lines Patch % Lines
hugr-core/src/types.rs 82.03% 46 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1744      +/-   ##
==========================================
+ Coverage   86.52%   86.64%   +0.11%     
==========================================
  Files         188      192       +4     
  Lines       34205    35021     +816     
  Branches    31076    31834     +758     
==========================================
+ Hits        29597    30343     +746     
- Misses       2923     2976      +53     
- Partials     1685     1702      +17     
Flag Coverage Δ
python 92.31% <ø> (-0.28%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

TypeBase(Function(FuncTypeBase {
    input: TypeRowBase { types: [
        TypeBase(Extension(CustomType { extension: IdentList("prelude"), extension_ref: (Weak), id: "string", args: [], bound: Copyable }), Copyable)] },
    output: TypeRowBase { types: [
        TypeBase(Variable(0, Any), Any),
        TypeBase(Function(FuncTypeBase {
            input: TypeRowBase { types: [TypeBase(Sum(Unit { size: 1 }), Copyable), TypeBase(Sum(Unit { size: 1 }), Copyable)] },
            output: TypeRowBase { types: [] },
            runtime_reqs: ExtensionSet({}) }), Copyable)] },
    runtime_reqs: ExtensionSet({IdentList("arithmetic.conversions")}) }), Copyable)

with environment [Type { b: Any }]
@acl-cqc acl-cqc changed the title tests: [Very WIP] Better proptests for Type test: [Very WIP] Better proptests for Type Dec 23, 2024
Example original type TypeBase(Extension(CustomType { extension: IdentList("collections.list"), extension_ref: (Weak), id: "List", args: [Type { ty: TypeBase(Variable(0, Any), Any) }], bound: Any }), Any)
Original environment [Type { b: Any }]
Substituted variable 0: Type { ty: TypeBase(Sum(General { rows: [TypeRowBase { types: [TypeBase(Function(FuncTypeBase { input: TypeRowBase { types: [TypeBase(Extension(CustomType { extension: IdentList("prelude"), extension_ref: (Weak), id: "usize", args: [], bound: Copyable }), Copyable)] }, output: TypeRowBase { types: [TypeBase(Variable(0, Any), Any)] }, runtime_reqs: ExtensionSet({IdentList("logic")}) }), Copyable), TypeBase(Extension(CustomType { extension: IdentList("prelude"), extension_ref: (Weak), id: "qubit", args: [], bound: Any }), Any), TypeBase(Extension(CustomType { extension: IdentList("arithmetic.float.types"), extension_ref: (Weak), id: "float64", args: [], bound: Copyable }), Copyable)] }] }), Any) }
Substitution result: TypeBase(Extension(CustomType { extension: IdentList("collections.list"), extension_ref: (Weak), id: "List", args: [Type { ty: TypeBase(Sum(General { rows: [TypeRowBase { types: [TypeBase(Function(FuncTypeBase { input: TypeRowBase { types: [TypeBase(Extension(CustomType { extension: IdentList("prelude"), extension_ref: (Weak), id: "usize", args: [], bound: Copyable }), Copyable)] }, output: TypeRowBase { types: [TypeBase(Variable(0, Any), Any)] }, runtime_reqs: ExtensionSet({IdentList("logic")}) }), Copyable), TypeBase(Extension(CustomType { extension: IdentList("prelude"), extension_ref: (Weak), id: "qubit", args: [], bound: Any }), Any), TypeBase(Extension(CustomType { extension: IdentList("arithmetic.float.types"), extension_ref: (Weak), id: "float64", args: [], bound: Copyable }), Copyable)] }] }), Any) }], bound: Any }), Any)
@acl-cqc acl-cqc changed the title test: [Very WIP] Better proptests for Type test: proptest Type validation and substitution Dec 23, 2024
@acl-cqc
Copy link
Contributor Author

acl-cqc commented Dec 23, 2024

Here's an example:

  • original type TypeBase(Extension(CustomType { extension: IdentList("collections.list"), extension_ref: (Weak), id: "List", args: [Type { ty: TypeBase(Variable(0, Any), Any) }], bound: Any }), Any)
  • original environment [Type { b: Any }]
  • Substituted variable 0: Type { ty: TypeBase(Sum(General { rows: [TypeRowBase { types: [TypeBase(Function(FuncTypeBase { input: TypeRowBase { types: [TypeBase(Extension(CustomType { extension: IdentList("prelude"), extension_ref: (Weak), id: "usize", args: [], bound: Copyable }), Copyable)] }, output: TypeRowBase { types: [TypeBase(Variable(0, Any), Any)] }, runtime_reqs: ExtensionSet({IdentList("logic")}) }), Copyable), TypeBase(Extension(CustomType { extension: IdentList("prelude"), extension_ref: (Weak), id: "qubit", args: [], bound: Any }), Any), TypeBase(Extension(CustomType { extension: IdentList("arithmetic.float.types"), extension_ref: (Weak), id: "float64", args: [], bound: Copyable }), Copyable)] }] }), Any) }
  • result: TypeBase(Extension(CustomType { extension: IdentList("collections.list"), extension_ref: (Weak), id: "List", args: [Type { ty: TypeBase(Sum(General { rows: [TypeRowBase { types: [TypeBase(Function(FuncTypeBase { input: TypeRowBase { types: [TypeBase(Extension(CustomType { extension: IdentList("prelude"), extension_ref: (Weak), id: "usize", args: [], bound: Copyable }), Copyable)] }, output: TypeRowBase { types: [TypeBase(Variable(0, Any), Any)] }, runtime_reqs: ExtensionSet({IdentList("logic")}) }), Copyable), TypeBase(Extension(CustomType { extension: IdentList("prelude"), extension_ref: (Weak), id: "qubit", args: [], bound: Any }), Any), TypeBase(Extension(CustomType { extension: IdentList("arithmetic.float.types"), extension_ref: (Weak), id: "float64", args: [], bound: Copyable }), Copyable)] }] }), Any) }], bound: Any }), Any)
    Which is to say, List<Var(0)> substituted with variable 0 = some-crazy-sum produces List<some-crazy-sum>, ho hum.

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.

Add proptests for Optrait::substitute and DataflowOpTrait::substitute
1 participant