| | 71 | '''Comment''': A polymorphic pattern binding isn't required in the above example, but the original code seems more elegant. Why not make monomorphism the default and permit polymorphism only when there is an explicit type signature on the binding? |
| | 72 | |
| | 73 | === Second message === |
| | 74 | Tristan Allwood (tora at doc.ic.ac.uk) said this. |
| | 75 | I was recently bitten by the fact pattern bindings are monomorphic; |
| | 76 | admitadly I'm writing code to learn about theoretical typing issues and |
| | 77 | nothing mainstream/production; but (as I hope my ghci session should |
| | 78 | illustrate) the cause of the problem is difficult to isolate if you're |
| | 79 | not aware of the property (which I wasn't until Igloo on #haskell |
| | 80 | pointed me this way); |
| | 81 | {{{ |
| | 82 | Prelude> :m +Data.Maybe |
| | 83 | |
| | 84 | Prelude Data.Maybe> let f = (fromJust (Just id)) in (f 3, f False) |
| | 85 | (3,False) |
| | 86 | |
| | 87 | Prelude Data.Maybe> let (Just f) = (Just id) in (f 3, f False) |
| | 88 | |
| | 89 | <interactive>:1:31: |
| | 90 | No instance for (Num Bool) |
| | 91 | arising from the literal `3' at <interactive>:1:31 |
| | 92 | Possible fix: add an instance declaration for (Num Bool) |
| | 93 | In the first argument of `f', namely `3' |
| | 94 | In the expression: f 3 |
| | 95 | In the expression: let (Just f) = (Just id) in (f 3, f False) |
| | 96 | }}} |
| | 97 | Ok, fine: except the reported type signatures are the same vis: |
| | 98 | {{{ |
| | 99 | Prelude Data.Maybe> :t let f = (fromJust (Just id)) in f |
| | 100 | let f = (fromJust (Just id)) in f :: forall a. a -> a |
| | 101 | |
| | 102 | Prelude Data.Maybe> :t let (Just f) = (Just id) in f |
| | 103 | let (Just f) = (Just id) in f :: forall a. a -> a |
| | 104 | }}} |
| | 105 | And just to cap it off, adding a layer of indirection... |
| | 106 | {{{ |
| | 107 | Prelude Data.Maybe> let q = ( let (Just f) = (Just id) in f ) in (q 3, q False) |
| | 108 | (3,False) |
| | 109 | }}} |
| | 110 | Simon's comment: yes, I guess this is a bit confusing. But the same will happen in H98 if the type of f was overloaded. And the example seems quite contrived. |
| | 111 | |
| | 112 | == Summary == |
| | 113 | |