Prolog type definition in swi-prolog
in visual prolog there is "domains" section in a prolog program in which you can define types. Is there any si开发者_开发问答milar thing in swi-prolog?
In visual prolog a type is defined like:
domains
NewType = thing1; thing2
No. But there is mode declaration in Mercury. Mercury is more than Prolog; it is a functional-logic language. Mercury still has a lot of Prolog syntax.
SWI-Prolog is a standard Prolog. It only uses mode declarations in documentation1 as information for the users. Such declarations can be placed in comments of modules for documentation-parsing programs to compile.
In standard Prologs, outside of comments, such declarations are only allowed (and reqired) in very special situations. The block/1 predicate of Sicstus Prolog for instance requires them.
Block/1 is used for co-routing(lazy evaluation, delay etc.). I've only seen block
used in one program in my life, PAKCS2, an interpreter for another functional-logic language. When the interpreter was ported to SWI, block/1
was not used.
1 Type and mode declarations in SWI Source Documentation
2 PAKCS, a program using the block/1
predicate (PAKCS is an implementation of the curry language.)
While SWI-Prolog doesn't support types in a capacity quite like what you're asking for, it is worth noting that it does indeed support a simple 'type' mechanism through the use of term specification via the record/1 predicate in the record
library.
This predicate allows you to specify a reasonably complex term 'type' (pattern) using a particular term specification language, and interprets it to automatically generate predicates used to perform creation (via a constructor predicate), modification via 'setter' predicates and accessors via 'getter' predicates on term instances, all by way of term-expansion with a particular predicate naming convention.
This is particularly useful when writing Prolog code that passes around reasonably complex term structures, as it provides you with a rudimentary type checking capability which Prolog typically lacks (natively). I've used this on many a large-scale Prolog project where interfaces are designed before implementation.
I remembered reading about types for Swi & Yap prolog. Here's a website with a "Hindley-Milner Type Checker for Prolog:"
Types for Prolog - Mercury-style type declarations and predicate signatures
Prolog Type Checker library
精彩评论