| 1 | {-# LANGUAGE GeneralizedNewtypeDeriving, GADTs #-} |
|---|
| 2 | |
|---|
| 3 | data SameType a b where |
|---|
| 4 | Refl :: SameType a a |
|---|
| 5 | |
|---|
| 6 | coerce :: SameType a b -> a -> b |
|---|
| 7 | coerce Refl = id |
|---|
| 8 | |
|---|
| 9 | trans :: SameType a b -> SameType b c -> SameType a c |
|---|
| 10 | trans Refl Refl = Refl |
|---|
| 11 | |
|---|
| 12 | sameUnit :: SameType () () |
|---|
| 13 | sameUnit = Refl |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | class IsoUnit a where |
|---|
| 17 | iso1 :: SameType () b -> SameType a b |
|---|
| 18 | iso2 :: SameType b () -> SameType b a |
|---|
| 19 | |
|---|
| 20 | instance IsoUnit () where |
|---|
| 21 | iso1 = id |
|---|
| 22 | iso2 = id |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | newtype Tagged a b = Tagged b deriving IsoUnit |
|---|
| 26 | |
|---|
| 27 | sameTagged :: SameType (Tagged a b) (Tagged a' b') -> SameType a a' |
|---|
| 28 | sameTagged Refl = Refl |
|---|
| 29 | |
|---|
| 30 | unsafe' :: SameType (Tagged a ()) (Tagged a' ()) |
|---|
| 31 | unsafe' = (iso1 sameUnit) `trans` (iso2 sameUnit) |
|---|
| 32 | |
|---|
| 33 | unsafe :: SameType a b |
|---|
| 34 | unsafe = sameTagged unsafe' |
|---|
| 35 | |
|---|
| 36 | --once again inferred type is a -> b |
|---|
| 37 | unsafeCoerce = coerce unsafe |
|---|