You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inconsistency/unsoundness in type system due to channels #889
Probably this is well known, but I couldn't find an issue on here. Because there's no value restriction on channels in Elm, the type system is unsound (for an appropriate notion of unsoundness) in the usual way. The following incorrect program compiles.
importSignalimportHtmlimportHtml.Attributes(style)
importHtml.Events(..)
importMaybec =Signal.channel Nothings:SignalInts =Signal.map (Maybe.withDefault 0)(Signal.subscribe c)draw x =Html.div
[ onClick (Signal.send c (Just"I am not an Int")), style [("width","100px"),("height","100px")]][Html.text (toString (x +1))]|>Html.toElement 100100main =Signal.map draw s
Whoa nice catch. Bug with type unification
On Feb 5, 2015 9:50 AM, "imeckler" notifications@github.com wrote:
Probably this is well known, but I couldn't find an issue on here. Because
there's no value restriction on channels in Elm, the type system is unsound
(for an appropriate notion of unsoundness) in the usual way. The following
incorrect program compiles.
import Signalimport Htmlimport Html.Attributes(style)import Html.Events(..)import Maybe
c = Signal.channel Nothing
s : Signal Ints = Signal.map (Maybe.withDefault 0) (Signal.subscribe c)
draw x =
Html.div
[ onClick (Signal.send c (Just "I am not an Int"))
, style [("width", "100px"), ("height", "100px")]
]
[ Html.text (toString (x + 1)) ]
|> Html.toElement 100 100
main = Signal.map draw s
—
Reply to this email directly or view it on GitHub #889.
Actually calling this a unification bug isn't quite right. It's because the
type of c is "Channel (Maybe a)" which unifies with both. So it's both
polymorphic and has a side effect of creating a channel. Maybe top level
definition for channels isn't a good idea - requiring them to be defined
with input ports (promises proposal) might just fix this.
On Feb 5, 2015 7:47 PM, "John Mayer" john.p.mayer.jr@gmail.com wrote:
Whoa nice catch. Bug with type unification
On Feb 5, 2015 9:50 AM, "imeckler" notifications@github.com wrote:
Probably this is well known, but I couldn't find an issue on here.
Because there's no value restriction on channels in Elm, the type system is
unsound (for an appropriate notion of unsoundness) in the usual way. The
following incorrect program compiles.
import Signalimport Htmlimport Html.Attributes(style)import Html.Events(..)import Maybe
c = Signal.channel Nothing
s : Signal Ints = Signal.map (Maybe.withDefault 0) (Signal.subscribe c)
draw x =
Html.div
[ onClick (Signal.send c (Just "I am not an Int"))
, style [("width", "100px"), ("height", "100px")]
]
[ Html.text (toString (x + 1)) ]
|> Html.toElement 100 100
main = Signal.map draw s
—
Reply to this email directly or view it on GitHub #889.
This is actually a well understood and solved problem. It's essentially the same issue as would arise with mutable references in SML or OCaml if not for the value restriction (a value restriction does bring issues of its own, but they're not that bad as they can generally be solved by eta expanding).
Is it possible to do this with any other functions in Elm? Am I correct that this is related to functions with effects, but otherwise it is not needed?
This is literally the only function in Elm that has side-effects, so if it is tied to effects, then I'd rather just get rid of channel. But does the same issue arise when you manage effects like in Haskell?
I doubt it's possible to do this with anything else in Elm. This issue is generally tied to mutation. One might worry that one could try and pull the same trick with signals, but I guess it shouldn't be possible since there is some kind of pure model for the signature of Elm Signals provided by a stream type.
Things are quite a bit different in Haskell since the only place one "has" something of type (for example) IORef a for some a is in the second argument to bind and plain old unification can keep us from doing naughty things since we can never obtain something of type forall a. IORef (f a) (for some f) as we can in Elm. It's hard to imagine what the type of newIORef would even have to be to get it to return a polymorphic ref (the return value would have to be IO (forall a. IORef (...)), but there's nothing sensible to put in for ...).
Consider the following program (which is rejected by GHC) for example.
importData.IORef
main = newIORef Nothing>>=\r -> writeIORef r (Just()) >> writeIORef r (Just"no")
The lambda is typed as follows:
r is given the type IORef (Maybe a) for a fresh type variable a. Note this is monomorphic!
writeIORef r (Just ()) imposes the constraint a ~ ()
writeIORef r (Just "no") imposes the constraint a ~ String, which can't be satisfied
If we use unsafePerfomIO, you can get a value of type forall a. IORef (Maybe a) and you get the same issue as in Elm.
Does the current promises proposal provide a way to write, e.g., the TodoMVC program without using channels?
The program still compiles when annotated with c : Signal.Channel (Maybe a). Why we accept monomorphic type variables in annotations is another discussion, but requiring a fully specified annotation would fix the issue. Ports require annotations, so that dovetails with the promises proposal, as John mentioned.
It's not a bug in the implementation. It's a bug in the type system resulting from the fact that Elm doesn't have a sort of value restriction, as mentioned above. One solution that I think Evan mentioned at some point would be to require mailboxes to have monomorphic types, which seems quite reasonable.
Yeah, a Signal a doesn't make a whole lot of sense. So this would be special casing mailboxes? I can see two cases. If there's no annotation, it should tell you to add one. If there is an annotation, it should tell you not to use type variables.
A problem with the proposal to require mailboxes to have monomorphic types: What to do about cases like the implementation of start in start-app/StartApp.elm? See https://github.com/evancz/start-app/blob/2.0.1/src/StartApp.elm#L79-L117. There's no way to actually provide a type signature, let alone a monomorphic one, for the mailbox used in there.
A problem with the proposal to require mailboxes to have monomorphic types: What do do about cases like the implementation of start in start-app/StartApp.elm? See https://github.com/evancz/start-app/blob/2.0.1/src/StartApp.elm#L79-L117. There's no way to actually provide a type signature, let alone a monomorphic one, for the mailbox used in there.
I do know that one could make this monomorphic in the sense you mention, if one could write that down as a type annotation. But one cannot, given that Elm has no equivalent of Haskell's ScopedTypeVariables.
And when you cannot write it down, you do get it interpreted as a polymorphic type, and then suffer from the type unsoundness. Happened in the wild, see here.
Activity
[-]Inconsistency/unsoundness in type system[/-][+]Inconsistency/unsoundness in type system due to channels[/+]johnpmayer commentedon Feb 6, 2015
Whoa nice catch. Bug with type unification
On Feb 5, 2015 9:50 AM, "imeckler" notifications@github.com wrote:
johnpmayer commentedon Feb 6, 2015
Actually calling this a unification bug isn't quite right. It's because the
type of c is "Channel (Maybe a)" which unifies with both. So it's both
polymorphic and has a side effect of creating a channel. Maybe top level
definition for channels isn't a good idea - requiring them to be defined
with input ports (promises proposal) might just fix this.
On Feb 5, 2015 7:47 PM, "John Mayer" john.p.mayer.jr@gmail.com wrote:
imeckler commentedon Feb 6, 2015
This is actually a well understood and solved problem. It's essentially the same issue as would arise with mutable references in SML or OCaml if not for the value restriction (a value restriction does bring issues of its own, but they're not that bad as they can generally be solved by eta expanding).
evancz commentedon Feb 6, 2015
Is it possible to do this with any other functions in Elm? Am I correct that this is related to functions with effects, but otherwise it is not needed?
This is literally the only function in Elm that has side-effects, so if it is tied to effects, then I'd rather just get rid of
channel. But does the same issue arise when you manage effects like in Haskell?imeckler commentedon Feb 6, 2015
I doubt it's possible to do this with anything else in Elm. This issue is generally tied to mutation. One might worry that one could try and pull the same trick with signals, but I guess it shouldn't be possible since there is some kind of pure model for the signature of Elm
Signals provided by a stream type.Things are quite a bit different in Haskell since the only place one "has" something of type (for example)
IORef afor someais in the second argument to bind and plain old unification can keep us from doing naughty things since we can never obtain something of typeforall a. IORef (f a)(for some f) as we can in Elm. It's hard to imagine what the type ofnewIORefwould even have to be to get it to return a polymorphic ref (the return value would have to beIO (forall a. IORef (...)), but there's nothing sensible to put in for...).Consider the following program (which is rejected by GHC) for example.
The lambda is typed as follows:
IORef (Maybe a)for a fresh type variablea. Note this is monomorphic!writeIORef r (Just ())imposes the constrainta ~ ()writeIORef r (Just "no")imposes the constrainta ~ String, which can't be satisfiedIf we use
unsafePerfomIO, you can get a value of typeforall a. IORef (Maybe a)and you get the same issue as in Elm.Does the current promises proposal provide a way to write, e.g., the TodoMVC program without using channels?
mgold commentedon Feb 8, 2015
The program still compiles when annotated with
c : Signal.Channel (Maybe a). Why we accept monomorphic type variables in annotations is another discussion, but requiring a fully specified annotation would fix the issue. Ports require annotations, so that dovetails with the promises proposal, as John mentioned.polytypic commentedon Aug 11, 2015
I assume this is well known, but, for reference, here is the same unsoundness exposed using
mailbox:rtfeldman commentedon Aug 11, 2015
I don't think this is well known, but it's definitely a reproducible bug!
I pasted that code into the current (0.15.1) Try Elm; it compiled and ran, and when I clicked the "1", it turned into "I am not an Int1".
rtfeldman commentedon Aug 11, 2015
To rule out a type inference bug, here is a minimal example with complete annotations that reproduces the bug in 0.15.1 if you paste it into Try Elm:
If you try to change the type annotation on
maybeIntsfrom(Maybe a)to(Maybe Int)you do get a compile error.imeckler commentedon Aug 11, 2015
It's not a bug in the implementation. It's a bug in the type system resulting from the fact that Elm doesn't have a sort of value restriction, as mentioned above. One solution that I think Evan mentioned at some point would be to require mailboxes to have monomorphic types, which seems quite reasonable.
mgold commentedon Aug 11, 2015
Yeah, a
Signal adoesn't make a whole lot of sense. So this would be special casing mailboxes? I can see two cases. If there's no annotation, it should tell you to add one. If there is an annotation, it should tell you not to use type variables.jvoigtlaender commentedon Nov 11, 2015
A problem with the proposal to require mailboxes to have monomorphic types: What to do about cases like the implementation of
startinstart-app/StartApp.elm? See https://github.com/evancz/start-app/blob/2.0.1/src/StartApp.elm#L79-L117. There's no way to actually provide a type signature, let alone a monomorphic one, for the mailbox used in there.imeckler commentedon Nov 11, 2015
That is monomorphic in the type environment containing the type variable "action".
jvoigtlaender commentedon Nov 11, 2015
I do know that one could make this monomorphic in the sense you mention, if one could write that down as a type annotation. But one cannot, given that Elm has no equivalent of Haskell's
ScopedTypeVariables.And when you cannot write it down, you do get it interpreted as a polymorphic type, and then suffer from the type unsoundness. Happened in the wild, see here.
evancz commentedon May 12, 2016
Yay 0.17!