purescript-safe-coerce icon indicating copy to clipboard operation
purescript-safe-coerce copied to clipboard

Add visible type vars to `coerce`

Open JordanMartinez opened this issue 3 years ago • 1 comments

See https://github.com/purescript/purescript/pull/4235#discussion_r829263552 once that PR gets merged.

JordanMartinez avatar Mar 17 '22 18:03 JordanMartinez

Perhaps a few other functions should be added here. A few variants would be:

  • a suffix indicating how many containers to keep the same, which indicates the number of kinds to bypass.
    • coerce @(Array a) @(Array b) becomes coerce1 @a @b since the container type doesn't change.
    • coerce @(Array (Maybe a)) @(Array (Maybe b)) becomes coerce2 @a @b since the nested container type doesn't change.
  • a "Wrap" suffix indicating the type should be wrapped in some newtype. coerceWrap is the same as Data.Newtype.wrap but the shared suffix means we could use the same naming convention for coerce1/coerce2.
    • coerce @a @(Disj a) becomes coerceWrap @Disj.
    • coerce @(Array a) @(Array (Disj a)) becomes coerce1Wrap @Disj.
    • coerce @(Array (Maybe a)) @(Array (Maybe (Disj a))) becomes coerce2Wrap @Disj.
  • a "Unwrap" suffix indicates a newtyped value should be unwrapped. coerceUnwrap is the same as Data.Newtype.unwrap but the shared suffix means we could use the same naming convention for coerce1/coerce2.
    • coerce @(Disj a) @a becomes coerceUnwrap @Disj.
    • coerce @(Array (Disj a)) @(Array a) becomes coerce1Unwrap @Disj.
    • coerce @(Array (Maybe (Disj a))) @(Array (Maybe a)) becomes coerce2Unwrap @Disj.
coerce1 :: forall f @a @b. Coercible (f a) (f b) => f a -> f b
coerce1 = coerce @(f a) @(f b)

coerce2 :: forall f g @a @b. Coercible (f (g a)) (f (g b)) => f (g a) -> f (g b)
coerce2 = coerce @(f (g a)) @(f (g b))

coerce1Wrap :: forall f @n a. Newtype n a => Coercible (f a) (f (n a)) => f a -> f (n a)
coerce1Wrap = coerce @(f a) @(f (n a))

JordanMartinez avatar Mar 17 '22 19:03 JordanMartinez