亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

一元限制是什么?

一元限制是什么?

一元限制是什么?我很困惑Haskell編譯器有時如何推斷出不像我預期的那樣多態的類型,例如在使用無點定義時。問題似乎是“單形限制”,默認情況下,這是在較早版本的編譯器上設置的??紤]以下Haskell程序:{-# LANGUAGE MonomorphismRestriction #-}import Data.List(sortBy)plus = (+)plus' x = (+ x)sort = sortBy compare main = do   print $ plus' 1.0 2.0   print $ plus 1.0 2.0   print $ sort [3, 1, 2]如果我用ghc我沒有獲得任何錯誤,可執行文件的輸出如下:3.03.0[1,2,3]如果我更改main尸體:main = do   print $ plus' 1.0 2.0   print $ plus (1 :: Int) 2   print $ sort [3, 1, 2]我沒有編譯時錯誤,輸出變成:3.03[1,2,3]如預期的那樣。但是,如果我試圖將其更改為:main = do   print $ plus' 1.0 2.0   print $ plus (1 :: Int) 2   print $ plus 1.0 2.0   print $ sort [3, 1, 2]我得到一個類型錯誤:test.hs:13:16:     No instance for (Fractional Int) arising from the literal ‘1.0’     In the first argument of ‘plus’, namely ‘1.0’     In the second argument of ‘($)’, namely ‘plus 1.0 2.0’     In a stmt of a 'do' block: print $ plus 1.0 2.0產生以下錯誤:test.hs:14:17:     No instance for (Num Char) arising from the literal ‘3’     In the expression: 3     In the first argument of ‘sort’, namely ‘[3, 1, 2]’     In the second argument of ‘($)’, namely ‘sort [3, 1, 2]’為什么ghc突然覺得plus不是多態的,需要Int爭吵?唯一提到Int在應用程序的plus如果定義顯然是多態的,這又有什么關系呢?為什么ghc突然覺得sort需要Num Char舉個例子?編譯時會出現以下錯誤:TestMono.hs:10:15:     No instance for (Ord a0) arising from a use of ‘compare’     The type variable ‘a0’ is ambiguous     Relevant bindings include       sort :: [a0] -> [a0] (bound at TestMono.hs:10:1)     Note: there are several potential instances:       instance Integral a => Ord (GHC.Real.Ratio a)         -- Defined in ‘GHC.Real’       instance Ord () -- Defined in ‘GHC.Classes’       instance (Ord a, Ord b) => Ord (a, b) -- Defined in ‘GHC.Classes’       ...plus 23 others     In the first argument of ‘sortBy’, namely ‘compare’     In the expression: sortBy compare     In an equation for ‘sort’: sort = sortBy compare為什么ghc能夠使用多態類型Ord a => [a] -> [a]為sort?為什么ghc治療plus和plus'不一樣?plus應該具有多態類型Num a => a -> a -> a我看不出這和sort但只有sort引發錯誤。
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 571 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號