简体   繁体   English

如何为导入或输出列表的谓词声明谓词?

[英]How to declare a pred for a predicate that imports or outputs lists?

Looking for help declaring a pred for a predicate that imports or outputs lists. 寻找帮助来声明谓词的谓词,该谓词可以导入或输出列表。 I tried :- pred name(list::in, integer::out) is multi. 我试过:-pred name(list :: in,integer :: out)是多的。 and compiler error message says that list/0 isn't recognized. 并且编译器错误消息说无法识别list / 0。 Checked library module list and see that I should write something like ...(list(T)::in . . . ), but didn't fully understand what to do. 检查了库模块列表,发现我应该写类似...(list(T):: in .....)的东西,但是并没有完全理解该怎么做。

List is a parametric type, parametric types take one or more parameters. 列表是一种参数类型,参数类型采用一个或多个参数。 In the case of list the parameter says what this is a list of. 对于列表,参数说明这是列表。 You may have a list of numbers, a list of strings, a list of pumpkins or a list of lists of numbers (any valid type). 您可能有一个数字列表,一个字符串列表,一个南瓜列表或一个数字列表(任何有效类型)。 So, if I create a function such as: 因此,如果我创建一个函数,例如:

:- func max(list(int)) = int.

This function takes a list of ints and returns an int (the maximum number found in the list). 此函数获取一个int列表,并返回一个int(列表中找到的最大数字)。

So, what's with list(T)? 那么,list(T)是什么? A token beginning with a capital letter is a variable, even in types, It can stand for any other type (usually). 以大写字母开头的令牌是变量,即使是类型也是如此,它可以代表任何其他类型(通常)。 So "list(T)" means a list of anything, such as a list of numbers or strings. 因此,“ list(T)”表示任何内容的列表,例如数字或字符串列表。 The next predicate is polymorphic, it works for different types depending on the actual values of it's type variable. 下一个谓词是多态的,它根据类型变量的实际值适用于不同的类型。

:- pred first(list(T)::in, T::out) is semidet.

A list of anything can be passed as the first item in the list will be returned, if there is one. 可以传递任何内容的列表,因为列表中的第一项(如果有)将被返回。 If this is used with a list of strings "list(string)" then T will be substituted (during compilation) with "string". 如果将它与字符串“ list(string)”列表一起使用,则T(在编译过程中)将被“ string”替换。

The reference for this part of Mercury's type system is here. 这是Mercury类型系统这部分的参考。 http://www.mercurylang.org/information/doc-release/mercury_ref/Discriminated-unions.html#Discriminated-unions http://www.mercurylang.org/information/doc-release/mercury_ref/Discriminated-unions.html#Discriminated-unions

I have tried :- pred name(...list(T),....) is ... and the compiler is no longer reporting errors related to my pred declaration. 我试过了:-pred name(... list(T),....)是...,编译器不再报告与我的pred声明有关的错误。 Don't yet know that the results will be correct, but one step closer. 尚不知道结果是否正确,但距离目标还差一步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM