Life Goes On

まあまあだけど楽しんでる方です

Monomorphism restriction

同じ問題で悩んでました。
monomorphism restriction - bonotakeの日記
Monomorphism restriction - HaskellWiki
まだ分からないのが、単相性制約の存在意義です。
自分は「型宣言をしっかり書こう派」なので実用上の問題はありません。ただ、↓のようなコードが『動かなくなる』くらいなら、変な書き方をしたときに『遅くなる』方がマシだと思うのですが‥。

fs = ((*), (*))

vs = ([3, 2, 1], [1.5, 1.0, 0.5])

aps (f1, f2) (v1, v2) = (foldl1 f1 v1, foldl1 f2 v2)

-- 以下の1行を追加すると、型が特定されるのでエラーにはならない
-- calc = aps fs vs
*Main> :t fs
fs :: (Integer -> Integer -> Integer,
      Integer -> Integer -> Integer)
*Main> aps fs vs

<interactive>:1:7:
   Couldn't match expected type `Integer'
          against inferred type `Double'
     Expected type: ([Integer], [Integer])
     Inferred type: ([Integer], [Double])
   In the second argument of `aps', namely `vs'
   In the expression: aps fs vs
*Main> :set -XNoMonomorphismRestriction
*Main> :l mr
[1 of 1] Compiling Main             ( mr.hs, interpreted )
Ok, modules loaded: Main.
Main> aps fs vs
(6,0.75)