Prolog - Numbers greater than x

I work with Prolog arithmetic and have a program that generates an abstract syntax tree like plus(num(1),num(2)) , which is just 1+2 . This is done using DCG. In this example, plus(num(1),num(2)) matches the list of prefixes [+,1,2] .

My problem is that I want to allow num(x) greater than 3. For example, num(4) allowed, but not num(1) .

I'm doing it:

 num(num(4)) --> [4]. num(num(5)) --> [5]. num(num(6)) --> [6]. num(num(7)) --> [7]. 

etc .. but would like to do something like num(num(x)) --> [x]. for numbers greater than 3. Any idea on how to approach this problem?

+2
source share
1 answer

What about:

 num(num(X)) --> [X], {X > 3} 

{} / 1 can be used to embed conditions and side effects in DCG grammar rules. DCGs are found on many Prolog systems, but there is no mature standard yet. But most Prolog systems have {} / 1. It is defined here:

Grammar rules for specific rules
Klaus Daessler
August 20, 2013

http://www.complang.tuwien.ac.at/ulrich/iso-prolog/dcgs/dcgsdin130820.pdf

In the glossary, {} / 1 goes as "the goal of the grammatical body." In older publications, the construction was called an “auxiliary action”, “calling procedure” or “additional conditions”, which is probably more reasonable than simply calling it the goal of the grammatical body, since we would like to see all the components of the grammatical body as the goal of the grammar. See an example here:

Grammars with specific parameters for language analysis
Fernando S.N. Pereira and David H. D. Warren
Artificial Intelligence 13 (1980), 231-278
http://cgi.di.uoa.gr/~takis/pereira-warren.pdf

Bye

+2
source

Source: https://habr.com/ru/post/957439/


All Articles