Skip to content

Add toLinearN; narrow the coercion scope#346

Merged
aspiwack merged 3 commits intotweag:masterfrom
treeowl:toLinear
Oct 6, 2021
Merged

Add toLinearN; narrow the coercion scope#346
aspiwack merged 3 commits intotweag:masterfrom
treeowl:toLinear

Conversation

@treeowl
Copy link
Copy Markdown
Collaborator

@treeowl treeowl commented Oct 4, 2021

  • Make toLinear, etc., explicitly coerce only multiplicities,
    rather than whole arrow types. This is just a cleanup; it shouldn't
    have any practical impact.

  • Add toLinearN and toLinearL to generalize toLinear, toLinear2, and toLinear3.

@treeowl
Copy link
Copy Markdown
Collaborator Author

treeowl commented Oct 4, 2021

Should we make at least some of these functions more polymorphic? I would think something like

toLinear
  :: forall (r1 :: RuntimeRep) (r2 :: RuntimeRep)
     (a :: TYPE r1) (b :: TYPE r2) p m x.
     (a %p-> b) %m-> (a %x-> b)

toLinear2
  :: forall (r1 :: RuntimeRep) (r2 :: RuntimeRep) (r3 :: RuntimeRep)
     (a :: TYPE r1) (b :: TYPE r2) (c :: TYPE r3) p q m x y.
     (a %p-> b %q-> c) %m-> (a %x-> b %y-> c)

toLinear3
  :: forall (r1 :: RuntimeRep) (r2 :: RuntimeRep)
     (r3 :: RuntimeRep) (r4 :: RuntimeRep)
     (a :: TYPE r1) (b :: TYPE r2) (c :: TYPE r3) (d :: TYPE r4) p q r m x y z.
     (a %p-> b %q-> c %r-> d) %m-> (a %x-> b %y-> c %z-> d)

This doesn't do much for toLinear, but for toLinear2 and toLinear3 I imagine something like this would be a considerable improvement.

@treeowl
Copy link
Copy Markdown
Collaborator Author

treeowl commented Oct 4, 2021

Since toLinearN is actually capable of changing multiplicities in any and all directions, should it have a different name?

@treeowl treeowl changed the title Narrow the unsafe coercion scope Add toLinearN; narrow the coercion scope Oct 4, 2021
@treeowl
Copy link
Copy Markdown
Collaborator Author

treeowl commented Oct 4, 2021

Might we want a version that takes a full list of multiplicity changes? Something like this?

toLinearList @'[ 'One :=> 'Many, _ :=> 'One, 'Many :=> n] ...

@treeowl treeowl force-pushed the toLinear branch 2 times, most recently from ae770f5 to b0bf1a4 Compare October 4, 2021 21:28
@treeowl treeowl changed the title Add toLinearN; narrow the coercion scope Add toLinearN/L; narrow the coercion scope Oct 5, 2021
Copy link
Copy Markdown
Member

@aspiwack aspiwack left a comment

Choose a reason for hiding this comment

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

I have a bunch of questions below. But I'm favourable to the change as a whole. As well as making the toLinearX functions returning functions of an arbitrary multiplicity (though maybe their name isn't ideal anymore, but it's not too serious).

I've got to admit, I'm feeling a bit of a mix of amazement and dismay that the Unsafe module is seeing such love 🙂 .

Comment on lines +152 to +162
-- | The actual implementation of 'ToLinearN', using the inductive natural
-- number it's handed.
type ToLinearN' :: forall {rep :: RuntimeRep}. INat -> TYPE rep -> TYPE rep -> Constraint
class ToLinearN' arrs f g where
prf :: UnsafeEquality f g
instance ToLinearN' k fb gb => ToLinearN' ('S k) ((a :: TYPE repa) %p-> (fb :: TYPE repb)) (a %q-> (gb :: TYPE repb)) where
prf = case prf @k @fb @gb of
UnsafeRefl -> case unsafeEqualityProof @p @q of
UnsafeRefl -> UnsafeRefl
instance ToLinearN' 'Z (a :: TYPE rep) (a :: TYPE rep) where
prf = UnsafeRefl
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does this produce an O(n) irreducible proof? I'm not super up to date with the new unsafeEqualityProof mechanism.

And if so: is it bad?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The proof can get a bit long, but I don't think that we can do anything about it. It'll be erased in Core Prep or so.

Comment on lines +131 to +142
-- | @ToLinearL l f g@ means that @f@ and @g@ are the same with the
-- possible exception of the multiplicities of some of the first arrows,
-- as described by the list @l@.
type ToLinearL :: forall {rep :: RuntimeRep}. [Change] -> TYPE rep -> TYPE rep -> Constraint
class ToLinearL l f g where
-- | @unsafeLinearityProofL \@l \@f \@g @ is a fake proof that @f@ and @g@ are
-- the same with the possible exception of the multiplicities of the first
-- arrows given by the list @l@.
unsafeLinearityProofL :: UnsafeEquality f g
instance ( ToLinearN' ni f g, UnifyL ni f g, UnifyL ni g f
, ni ~ Length l, UseChanges l f g) => ToLinearL l f g where
unsafeLinearityProofL = prf @ni
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this really necessary? This can be achieved with a ToLinearN and a type annotation. I think that I'm essentially questioning the need for exporting an operator from this Unsafe module.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, you're right. Type application with plenty of underscores should be just as good.

@aspiwack
Copy link
Copy Markdown
Member

aspiwack commented Oct 5, 2021

@treeowl there appears to be an issue with CI. But I think I've fixed it in #348 , so you just need to rebase this PR and CI should start.

Make `toLinear`, etc., explicitly coerce only *multiplicities*,
rather than whole arrow types.
This is just a cleanup; it shouldn't have any practical impact.
@treeowl treeowl force-pushed the toLinear branch 2 times, most recently from 0871ef8 to 466ace3 Compare October 5, 2021 19:54
@treeowl
Copy link
Copy Markdown
Collaborator Author

treeowl commented Oct 5, 2021

@aspiwack, I implemented your suggestions. Your suggested approach to ToLinearN' (with a heterogeneous equality tweak) is much better than what I was doing before. I also generalized the toLinear, toLinear2, and toLinear3 in a separate commit. I hope that doesn't wreak too much havoc on inference.

@treeowl
Copy link
Copy Markdown
Collaborator Author

treeowl commented Oct 5, 2021

Commits are now squashed to logical units.

@treeowl treeowl force-pushed the toLinear branch 3 times, most recently from 9c88b2d to 3128aff Compare October 5, 2021 20:14
@treeowl treeowl changed the title Add toLinearN/L; narrow the coercion scope Add toLinearN; narrow the coercion scope Oct 5, 2021
@treeowl treeowl force-pushed the toLinear branch 3 times, most recently from 1703838 to 812286f Compare October 5, 2021 22:04
Copy link
Copy Markdown
Member

@aspiwack aspiwack left a comment

Choose a reason for hiding this comment

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

Let's address the small comment below, then when you are happy, I'll merge.

@treeowl
Copy link
Copy Markdown
Collaborator Author

treeowl commented Oct 6, 2021

I think this is ready to merge.

@treeowl
Copy link
Copy Markdown
Collaborator Author

treeowl commented Oct 6, 2021

Oh, I guess I should remove the word TODO...

Use the class system and some type-level hacking to
generalize @toLinear@, @toLinear2@, and @toLinear3@.
This is mostly useful for `toLinear2` and `toLinear3`, where
we may want to make *some* of the arrows linear but leave others
as they are.
@treeowl
Copy link
Copy Markdown
Collaborator Author

treeowl commented Oct 6, 2021

OK, now it's really done.

Copy link
Copy Markdown
Member

@aspiwack aspiwack left a comment

Choose a reason for hiding this comment

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

Fantastic. Thanks a lot!

CI is green, which does mean that generalising the types of the unsafe operations hasn't been a problem in practice.

Let me merge now.

@utdemir it would be interesting to see if this PR has any effect on your benchmarks (I don't expect so, but you never know).

@aspiwack aspiwack merged commit 48816cb into tweag:master Oct 6, 2021
@treeowl treeowl deleted the toLinear branch October 6, 2021 09:33
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.

2 participants