为什么ghci输出(Num a)=> a for:t 4而不是(Ord a)=>一个?

时间:2016-10-31 22:54:11

标签: haskell types type-inference typeclass

当我在ghci中输入<body> <form onlogin="return checkForm(this);"> <p><input type="text" name="username" placeholder="Enter Username"></p> <p><input type="password" name="pwd1" placeholder="Enter Password"></p> <p><button type="login"placeholder="Login"></p> <p> <a href="#">Forgot your password?</a></p> </form> <script type="text/javascript"> function checkForm(form) { if(form.username.value == "") { alert("Error: Username cannot be blank!"); form.username.focus(); return false; } re = /^\w+$/; if(!re.test(form.username.value)) { alert("Error: Username must contain only letters, numbers and underscores!"); form.username.focus(); return false; } if(form.pwd1.value != "") { if(form.pwd1.value.length < 6) { alert("Error: Password must contain at least six characters!"); form.pwd1.focus(); return false; } if(form.pwd1.value == form.username.value) { alert("Error: Password must be different from Username!"); form.pwd1.focus(); return false; } re = /[0-9]/; if(!re.test(form.pwd1.value)) { alert("Error: password must contain at least one number (0-9)!"); form.pwd1.focus(); return false; } re = /[a-z]/; if(!re.test(form.pwd1.value)) { alert("Error: password must contain at least one lowercase letter (a-z)!"); form.pwd1.focus(); return false; } re = /[A-Z]/; if(!re.test(form.pwd1.value)) { alert("Error: password must contain at least one uppercase letter (A-Z)!"); form.pwd1.focus(); return false; } } else { alert("Error: Please check that you've entered and confirmed your password!"); form.pwd1.focus(); return false; } alert("You entered a valid password: " + form.pwd1.value); return true; } 时,我得到了

:t 4

我理解为什么Prelude> :t 4 4 :: Num t => t 不仅仅是一个整数或一个整数而且它是自下而上的,但我不明白为什么4没有显示为4或更多纠正这样的事情:

Ord t => t

因为4 :: (Ord t || Num t) => t 既是4又是Ord,但NumOrd没有联系......

那么Num为什么只输出:t 4

1 个答案:

答案 0 :(得分:9)

并非所有包含Num实例的类型也都有Ord的实例,并且您只需要fromInteger的{​​{1}}部分来获得Haskell所具有的重载数字文字。例如,Data.Complex中的NumComplex个实例,但没有Num。在这种情况下,Ord 4

Ord

正如@Lee评论的那样,这是report中概述的行为。