whoi
whoi

Reputation: 3431

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 similar thing in swi-prolog?

In visual prolog a type is defined like:

domains
NewType = thing1; thing2

Upvotes: 6

Views: 3430

Answers (3)

frayser
frayser

Reputation: 1772

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 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.)

Upvotes: 7

Roman
Roman

Reputation: 1188

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

Upvotes: 2

user206428
user206428

Reputation:

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.

Upvotes: 7

Related Questions