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

Use less unsafeCoerce #321

Closed
wants to merge 1 commit into from
Closed

Conversation

treeowl
Copy link
Contributor

@treeowl treeowl commented Jul 9, 2019

Get rid of a large fraction of calls to unsafeCoerce. Several
of the ones that remain are removed in my merge generalization
PR.

Get rid of a large fraction of calls to `unsafeCoerce`. Several
of the ones that remain are removed in my merge generalization
PR.
@@ -296,9 +297,14 @@ class ( MonadHold t (PushM t)
-- | Construct a 'Coercion' for a 'Dynamic' given an 'Coercion' for its
-- occurrence type
dynamicCoercion :: Coercion a b -> Coercion (Dynamic t a) (Dynamic t b)
-- | Construct a 'Coercion' for an 'Incremental'' given an 'Coercion' for its
-- occurrence type
incrementalCoercion :: Coercion t1 t2 -> Coercion p1 p2 -> Coercion (Incremental' t t1 p1) (Incremental' t t2 p2)
Copy link
Member

Choose a reason for hiding this comment

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

I take it something goes wrong if you do this instead?

incrementalCoercion :: Coercion (PatchTarget p1) (PatchTarget p2) -> Coercion p1 p2 -> Coercion (Incremental t p1) (Incremental t p2)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a very good question. That probably will work, and it's much less horrible.

updatedIncremental (Incr (Incremental_Profiled i)) = coerce $ profileEvent $ updatedIncremental (Incr i)
incrementalToDynamic (Incr (Incremental_Profiled i)) = coerce $ incrementalToDynamic (Incr i)
behaviorCoercion (c :: Coercion a b) =
Coercion `trans` (behaviorCoercion c :: Coercion (Behavior t a) (Behavior t b)) `trans` Coercion
Copy link
Member

Choose a reason for hiding this comment

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

Can you do

case fooCoercion c :: ... of Coercion -> Coercion

for all of these to make GHC work more?

Copy link
Contributor Author

@treeowl treeowl Jul 9, 2019

Choose a reason for hiding this comment

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

Can you give an example? Facing these somewhat involved coercion types that I couldn't prove directly with Coercion, I reached for the toolbox. It should certainly be possible to do it all with case, but I don't know if it'll be any nicer. You're more than welcome to try!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I think I see what you mean now. Coercion . fancy . Coercion should be case fancy of Coercion -> Coercion. But it seems potentially less maintainable.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah that's what I mean. I was thinking more maintainable, since GHC can figure different things out there as needed. I guess I'm also bullish in GHC becoming smarter in the future. (I'm planning on writing a proposal for getting rid of roles.)

@@ -144,16 +145,32 @@ instance Reflex t => Reflex (ProfiledTimeline t) where
unsafeBuildIncremental (ProfiledM a0) (Event_Profiled a') = coerce $ unsafeBuildIncremental a0 a'
mergeIncremental = Event_Profiled . mergeIncremental . (unsafeCoerce :: Incremental (ProfiledTimeline t) (PatchDMap k (Event (ProfiledTimeline t))) -> Incremental t (PatchDMap k (Event t)))
mergeIncrementalWithMove = Event_Profiled . mergeIncrementalWithMove . (unsafeCoerce :: Incremental (ProfiledTimeline t) (PatchDMapWithMove k (Event (ProfiledTimeline t))) -> Incremental t (PatchDMapWithMove k (Event t)))
Copy link
Member

Choose a reason for hiding this comment

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

I think these can be derived with:

case (incrementalCoercion, eventCoercion) of
  (Coercion, Coercion) -> Coercion

I'd certainly hope the newtype coercion axioms are defined for data families too!

@treeowl
Copy link
Contributor Author

treeowl commented Jul 9, 2019 via email

@treeowl
Copy link
Contributor Author

treeowl commented Jul 9, 2019

Closing in favor of #322, based on @Ericson2314's suggestion regarding Dynamic.

@treeowl treeowl closed this Jul 9, 2019
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.

3 participants