[英]Why do I get an error type with an argument that is used twice for the same purpose
First of all, I have these types :首先,我有这些类型:
type position = float * float
type node = position
type path = position list
Here are the two pieces of code causing the error :这是导致错误的两段代码:
let build_path map source target =
let rec build_aux acc map source x initial_target =
if (((DistMap.find_opt x map) = None) || x = source) then acc@[initial_target]
else build_aux ((DistMap.find x map)::acc) map source (DistMap.find x map) initial_target
in build_aux [] map source target target
let shortest_path graph source target : path =
build_path (snd (dijkstra graph source target)) source target
path
has type position list
for clarity.为了清楚起见, path
具有类型position list
。
Here's the error :这是错误:
361 | build_path (snd (dijkstra graph source target)) source target
^^^^^^
Error: This expression has type position list
but an expression was expected of type position = float * float
I just don't get it.我只是不明白。 I've tried the build_path function in Utop, by having a Map filled like this :我已经在 Utop 中尝试了 build_path 函数,方法是像这样填充地图:
DistMap.bindings prevMap;;
- : (node * (float * float)) list =
[((1., 1.), (7., 7.)); ((2., 2.), (1., 1.)); ((3., 3.), (2., 2.));
((4., 4.), (3., 3.)); ((5., 5.), (4., 4.))]
let l = build_list prevMap (1.,1.) (5.,5.);;
val l : node list = [(1., 1.); (2., 2.); (3., 3.); (4., 4.); (5., 5.)]
shortest_path
has to, with 100% certainty, receive target
with type node
. shortest_path
必须 100% 确定地接收类型为node
target
。 The thing is, no error is raised when target is used as an argument for the dijsktra
function, which requires a graph
and two nodes source
and target
.问题是,当 target 用作dijsktra
函数的参数时不会引发错误,这需要一个graph
和两个节点source
和target
。
So I'm really confused why target suddenly has the wrong type for build_path
and not dijkstra
.所以我真的很困惑为什么 target 突然有错误的build_path
类型而不是dijkstra
。
Anyway to resolve this issue?无论如何要解决这个问题?
感谢@Pierre G. 的帮助,我们确定target
类型被dijkstra
函数约束到一个position list
,因为我正在将dijkstra
的列表与目标进行比较,一旦错误被修复并且target
与dijkstra
另一个node
进行比较,问题就解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.