meaning of warning and type in ML

浪尽此生 提交于 2019-12-13 01:23:24

问题


fun a(list) = 
   let
   val num = length(hd(list))
   fun inner(list) = 
      if num = length(hd(list)) then
         if tl(list) = nil then true
         else inner(tl(list))
      else false
   in
   if length(hd(list))-1 = length(tl(list)) then inner(tl(list))
   else false
   end;

this is ml code and I got this warning and type.

stdIn:6.16 Warning: calling polyEqual
val a = fn : ''a list list -> bool

I don't understand about the warning. why it appear and the type. ''a why it has two '? ''? what is the difference between 'a list list and ''a list list?


回答1:


To answer your second question,

why it has two '? ''? what is the difference between 'a list list and ''a list list?

''a is the same as 'a, but requires it to be an equality type. An equality type in SML is a type that can be compared using =. Non-equality types cannot be compared using =. When you create a datatype, you can specify whether it is an equality type or not.




回答2:


Excerpted from ML Hints:

Warning: calling polyEqual [may occur] whenever you use = to compare two values with polymorphic type.

For example, fun eq(x,y) = (x = y); will cause this warning to be generated, because x and y will have polymorphic type ''a. This is perfectly fine and you may ignore the warning. It is not reporting any kind of semantic error or type error in your code. The compiler reports the warning because there can be a slight ineffeciency in how ML tests whether two values of a polymorphic type are equal. In particular, to perform the equality test, the run-time system must first determine what types of values you are currently using and then determine whether the values are equal. The first part (checking the run-time types) can make the = test slightly slower than if the types are known ahead of time (such as when we test 3 = 4 and know that the = test is being applied to integers). However, that is not something most users of ML ever need to worry about...




回答3:


dict=val a =[("a",[1,2]),("b",[2,3])] ;

here is the code which has implementation of look up in dictionary

 fun look key [] = []
| look key ((a,b)::xs) = if (key =a ) then b else look key xs ;

which gives output as

 test1.sml:8.36 Warning: calling polyEqual

It is because it does not know what are the types which are compared so the below code says that both are string type .

fun look (key:string) [] = []
| look (key:string) ((a:string,b)::xs) = if (key =a ) then b else look (key:string) xs ;


来源:https://stackoverflow.com/questions/12444149/meaning-of-warning-and-type-in-ml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!