<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">On a second thought, maybe GHCi's silence is a bad thing here? Maybe it should complain loudly as GHC does?<div class=""><br class=""></div><div class="">```hs</div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #5a23f9" class="">λ> </span><span style="font-variant-ligatures: no-common-ligatures" class="">:set -package vector</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">package flags have changed, resetting and loading new packages...</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(90, 35, 249); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">λ> </span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #5a23f9" class="">λ> </span><span style="font-variant-ligatures: no-common-ligatures" class="">import Prelude</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(90, 35, 249); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">λ> </span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #5a23f9" class="">λ> </span><span style="font-variant-ligatures: no-common-ligatures" class="">import qualified Data.Vector.Storable as VS</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(90, 35, 249); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">λ> </span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(90, 35, 249); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">λ> </span><span style="font-variant-ligatures: no-common-ligatures; color: #74e5ca" class="">:{</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(90, 35, 249); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">λ| </span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #5a23f9" class="">λ| </span><span style="font-variant-ligatures: no-common-ligatures" class="">newtype SomeVector = SomeVector (VS.Vector Int)</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(90, 35, 249); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">λ| </span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #5a23f9" class="">λ| </span><span style="font-variant-ligatures: no-common-ligatures" class="">isSameVector :: SomeVector -> SomeVector -> Bool</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #5a23f9" class="">λ| </span><span style="font-variant-ligatures: no-common-ligatures" class="">isSameVector (SomeVector x) (SomeVector y) = </span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #5a23f9" class="">λ| </span><span style="font-variant-ligatures: no-common-ligatures" class=""> x'offset == y'offset && x'fp == y'fp</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #5a23f9" class="">λ| </span><span style="font-variant-ligatures: no-common-ligatures" class=""> where</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #5a23f9" class="">λ| </span><span style="font-variant-ligatures: no-common-ligatures" class=""> (x'fp, x'offset, _x'len) = VS.unsafeToForeignPtr x</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #5a23f9" class="">λ| </span><span style="font-variant-ligatures: no-common-ligatures" class=""> (y'fp, y'offset, _y'len) = VS.unsafeToForeignPtr y</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(90, 35, 249); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">λ| </span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(90, 35, 249); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">λ| </span><span style="font-variant-ligatures: no-common-ligatures; color: #74e5ca" class="">:}</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(90, 35, 249); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">λ> </span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #5a23f9" class="">λ> </span><span style="font-variant-ligatures: no-common-ligatures" class="">let (v :: VS.Vector Int) = VS.fromList [3,2,5] in isSameVector (SomeVector v) (SomeVector v)</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">True</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(90, 35, 249); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">λ> </span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(90, 35, 249); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">λ> </span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #5a23f9" class="">λ> </span><span style="font-variant-ligatures: no-common-ligatures" class="">:set -XMonomorphismRestriction</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(90, 35, 249); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">λ> </span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #5a23f9" class="">λ> </span><span style="font-variant-ligatures: no-common-ligatures" class="">let v = VS.fromList [3,2,5] in isSameVector (SomeVector v) (SomeVector v)</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">True</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(90, 35, 249); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">λ> </span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #5a23f9" class="">λ> </span><span style="font-variant-ligatures: no-common-ligatures" class="">:set -XNoMonomorphismRestriction</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(90, 35, 249); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">λ> </span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #5a23f9" class="">λ> </span><span style="font-variant-ligatures: no-common-ligatures" class="">let v = VS.fromList [3,2,5] in isSameVector (SomeVector v) (SomeVector v)</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(116, 229, 202); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">False</span></div><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(90, 35, 249); background-color: rgb(22, 27, 24);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">λ> </span></div></div><div class=""><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">```</span></div><div class=""><br class=""></div><div class="">Further more, my intuition about GHC's type inference here is proved wrong by it, right. But I still think that with a single piece of `let-in` construct, types are better to be inferred as specific as possible, then the result would not be affected by some extension's semantics modification. Here v's type can obviously be inferred to `VS.Vector Int` according to its usage in the `SomeVector` data constructor, I wonder why GHC is not doing this?</div><div class=""><br class=""></div><div><br class=""><blockquote type="cite" class=""><div class="">On 2021-04-06, at 22:19, YueCompl via ghc-devs <<a href="mailto:ghc-devs@haskell.org" class="">ghc-devs@haskell.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">Thanks very much for the diagnostic and explanation!<br class=""><br class="">I was wrong in assuming the `in isSameVector (SomeVector v) (SomeVector v)` part is enough to have type of v in `let !v = VS.fromList [3,2,5]` inferred as monomorphic, totally unaware about "NoMonomorphismRestriction" before, I've learned it today :D<br class=""><br class=""><blockquote type="cite" class="">On 2021-04-06, at 21:51, Viktor Dukhovni <<a href="mailto:ietf-dane@dukhovni.org" class="">ietf-dane@dukhovni.org</a>> wrote:<br class=""><br class="">On Tue, Apr 06, 2021 at 07:12:51PM +0800, YueCompl via ghc-devs wrote:<br class=""><br class=""><blockquote type="cite" class="">λ> import Control.Monad.ST<br class="">λ> import qualified Data.Vector.Storable as VS<br class="">λ> <br class="">λ> :{<br class="">λ| <br class="">λ| newtype SomeVector = SomeVector (VS.Vector Int)<br class="">λ| <br class="">λ| isSameVector :: SomeVector -> SomeVector -> Bool<br class="">λ| isSameVector (SomeVector !x) (SomeVector !y) = runST $ do<br class="">λ| mx@(VS.MVector !x'offset !x'fp) <- VS.unsafeThaw x<br class="">λ| my@(VS.MVector !y'offset !y'fp) <- VS.unsafeThaw y<br class="">λ| _ <- VS.unsafeFreeze mx<br class="">λ| _ <- VS.unsafeFreeze my<br class="">λ| return $ x'offset == y'offset && x'fp == y'fp<br class="">λ| <br class="">λ| :}<br class="">λ> <br class="">λ> let !v = VS.fromList [3,2,5] in isSameVector (SomeVector v) (SomeVector v)<br class="">False<br class="">λ> <br class="">λ> let !v = SomeVector (VS.fromList [3,2,5]) in isSameVector v v<br class="">True<br class=""></blockquote><br class="">In GHCi, but not in compiled programs, by default the<br class="">`NoMonomorphismRestriction` extension is enabled. If I compile your<br class="">code with that restriction, I can reproduce your results (the values are<br class="">not shared).<br class=""><br class="">If I either skip the extension, or add an explicit type annotation to<br class="">for the vector, then the values are shared.<br class=""><br class=""> {-# LANGUAGE BangPatterns #-}<br class=""> {-# LANGUAGE NoMonomorphismRestriction #-}<br class=""> import Control.Monad.ST<br class=""> import qualified Data.Vector.Storable as VS<br class=""><br class=""> newtype SomeVector = SomeVector (VS.Vector Int)<br class=""><br class=""> isSameVector :: SomeVector -> SomeVector -> Bool<br class=""> isSameVector (SomeVector !x) (SomeVector !y) = runST $ do<br class=""> mx@(VS.MVector !x'offset !x'fp) <- VS.unsafeThaw x<br class=""> my@(VS.MVector !y'offset !y'fp) <- VS.unsafeThaw y<br class=""> _ <- VS.unsafeFreeze mx<br class=""> _ <- VS.unsafeFreeze my<br class=""> return $ x'offset == y'offset && x'fp == y'fp<br class=""><br class=""> main :: IO ()<br class=""> main =<br class=""> let !v = VS.fromList [0..1023] -- :: VS.Vector Int<br class=""> in print $ isSameVector (SomeVector v) (SomeVector v)<br class=""><br class="">Since newtypes are always strict in their argument, I don't think the<br class="">BangPattern does what you'd like it to do, it just makes "main" strict<br class="">in v. As defined with `NoMonomorphismRestriction` v is a polymorphic<br class="">function, and I guess it is specialised at the call site.<br class=""><br class="">-- <br class=""> Viktor.<br class="">_______________________________________________<br class="">ghc-devs mailing list<br class=""><a href="mailto:ghc-devs@haskell.org" class="">ghc-devs@haskell.org</a><br class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs<br class=""></blockquote><br class="">_______________________________________________<br class="">ghc-devs mailing list<br class=""><a href="mailto:ghc-devs@haskell.org" class="">ghc-devs@haskell.org</a><br class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs<br class=""></div></div></blockquote></div><br class=""></div></body></html>