3

I am building some test cases for integer/1 using SWI-Prolog.

ISO/IEC 13211-1 gives a BNF definition for integer and one of the alternatives for integer is for a character code constant.

I am able to create and test examples of all the other alternatives using integer/1 but for character code constant I cannot create a valid example. (see below)

How is an integer as a character code constant created that will return true using integer/1?


Answer

Thanks to @false.

integer(0'0).
true.

integer(0'9).
true.

integer(0'a).
true.

integer(0'\n).
true.

Usefulness

X is 0'\n.
X = 10.

X is 0b010101.
X = 21.

X is 0xFFF1.
X = 65521.

X is 0o7423.
X = 3859.

and thanks to j4n bur53 via link from @false

with SWI-Prolog other radix can be used besides 2,8, or 16.

X is 5'1234012340.
X = 3032220.

X is 32'123456789ABCDEFGHIJKLMNOPQRSTU.
X = 47525417447024678661670292427038339608998846.

What I tried

integer("0").
false.

integer('0').
false.

integer(`0`).
false.

integer("1").
false.

integer('1').
false.

integer(`1`).
false.

ISO

INTERNATIONAL STANDARD ISO/IEC 13211-1 First edition 1995-06-01
Information technology - Programming languages - Prolog
Part 1: General Core

INTERNATIONAL STANDARD ISO/IEC 13211-1:1995
TECHNICAL CORRIGENDUM 1
Published 2007-11-15

INTERNATIONAL STANDARD ISO/IEC 13211-1:1995
TECHNICAL CORRIGENDUM 2
Published 2012-02-15

BNF for integer

integer token (* 6.4.4 *) =  
    integer constant (* 6.4.4 *)
  | character code constant (* 6.4.4 *)
  | binary constant (* 6.4.4 *)
  | octal constant (* 6.4.4 *)
  | hexadecimal constant (* 6.4.4 *) ;

BNF for character code constant

character code constant (* 6.4.4 *) =  
  "0" , single quote char (* 6.5.5 *), single quoted character (* 6.4.2.1 *) 

I suspect the BNF is wrong in ISO/IEC 13211-1 but checking the CORRIGENDUM shows no corrections.


Integer test cases

% <integer constant> examples

number(1). 
% true.

number(0).
% true.

number(01).
% true.

number(12345678901234567890123456789012345678901234567890).
% true.

% <character code constant> examples

% ???

% <binary constant> examples

number(0b0).  
% true.

number(0b10101010101010101010101010101010101010101010101010). 
% true.

integer(0b2).
% ERROR: Syntax error: Illegal number
% ERROR: integer
% ERROR: ** here **
% ERROR: (0b2) .

% <octal constant> examples

integer(0o7). 
% true.

integer(0o1234567012345670123456701234567012345670123456701234567). 
% true.

integer(0o8).
% ERROR: Syntax error: Illegal number
% ERROR: integer
% ERROR: ** here **
% ERROR: (0o8) . 

% <hexadecimal constant>

integer(0x0). 
% true.

integer(0xF). 
% true.

integer(0xf). 
% true.

integer(0x123456789ABCDEF012345670123456789ABCDEF012345670123456789ABCDEF). 
% true.

integer(0xG).
% ERROR: Syntax error: Illegal number
% ERROR: integer
% ERROR: ** here **
% ERROR: (0xG) . 
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
  • 1
    The character code constant for zero is `0'0`. Not sure what you do not understand here. – false Jan 13 '17 at 15:11
  • Thanks. Then single `'` is throwing me off. – Guy Coder Jan 13 '17 at 15:13
  • Post the answer and I will give you the votes. – Guy Coder Jan 13 '17 at 15:14
  • With `integer(0'a).` it now makes more sense. – Guy Coder Jan 13 '17 at 15:16
  • 1
    Will do, but there is a little bit more to note :-) In particular, from [#212](http://www.complang.tuwien.ac.at/ulrich/iso-prolog/conformity_assessment#212) onward. – false Jan 13 '17 at 15:17
  • 1
    [Related](https://groups.google.com/forum/#!topic/comp.lang.prolog/8hYtN-mZox4). – false Jan 13 '17 at 16:22
  • 1
    Related: https://github.com/LogtalkDotOrg/logtalk3/blob/master/tests/prolog/NOTES.md – Paulo Moura Jan 13 '17 at 16:32
  • @PauloMoura: Which of your test cases contain`0'`? – false Jan 13 '17 at 17:28
  • @false I don't know Logtalk, but there are a few files with this [line](https://github.com/LogtalkDotOrg/logtalk3/blob/a1cb0eb2a34b6265f580420730f783546f97f98d/coding/tests/source.lgt#L236) <- The link in the comment is hard to see. – Guy Coder Jan 13 '17 at 17:48
  • @GuyCoder: Thanks for the reference! Actually the rule starting with `numbers :- ...` is invalid Prolog syntax, because `... C2 is 0'', ...` is invalid. This has to be either `... C2 is 0''', ...` or `... C2 is 0'\', ...` – false Jan 13 '17 at 18:15
  • @GuyCoder The specific file you linked, `source.lgt`, is there *just* for testing syntax coloring support. Nothing to do with unit tests. – Paulo Moura Jan 13 '17 at 21:07
  • @false `0''` is supported syntax in B-Prolog, CxProlog, Ciao, GNU Prolog, ECLiPSe, JIProlog, Qu-Prolog, SWI-Prolog, XSB, YAP. Lean Prolog doesn't support the `0'` notation. SICStus Prolog requires `0'''`. – Paulo Moura Jan 13 '17 at 21:13
  • @false Given that we're talking about number syntax, they are in the obvious place: https://github.com/LogtalkDotOrg/logtalk3/tree/master/tests/prolog/syntax/numbers We could use a few more tests there, however. Contributions welcome. – Paulo Moura Jan 13 '17 at 21:19
  • 1
    @PauloMoura: The question is about standard-conforming behavior. See [#114](http://www.complang.tuwien.ac.at/ulrich/iso-prolog/conformity_assessment#114) ... #117: B and GNU reject`0''` - you must have a very old version of those. – false Jan 13 '17 at 22:59
  • @PauloMoura: [See this table](http://www.complang.tuwien.ac.at/ulrich/iso-prolog/conformity_assessment) for further test cases. – false Jan 13 '17 at 23:01
  • @false Latest stable versions of both. B-Prolog 8.1 and GNU Prolog 1.4.4. GNU Prolog only rejects `0''` when the `strict_iso` flag is on. – Paulo Moura Jan 13 '17 at 23:35
  • @PauloMoura: What is of relevance is default behavior, not some special mode. – false Jan 13 '17 at 23:43

1 Answers1

0

This was answered by false in a comment.
Reposting here so that others can see an answer exist.

integer(0'0).
true.

integer(0'9).
true.

integer(0'a).
true.

integer(0'\n).
true.
Guy Coder
  • 24,501
  • 8
  • 71
  • 136