Here is some code to test the type/metatype of SWI-Prolog terms: % var,nonvar are meta-question about the state of computation, not about the term % Note: X can be a variable (if it is fresh) or a nonvar (if it is set) % but x can ever only be a nonvar typeof(X, var) :- var(X),!. […]

Categories

## Testing types of Prolog terms: slight problems

- Post author By Full Stack
- Post date April 7, 2020
- No Comments on Testing types of Prolog terms: slight problems

- Tags _, :Float, !, !. % if it's a variable, 'EOS', "\t", "3a", "as", "compound", "Value"), (ord_seteq(Eos, [atomic, [cyclic, [S_1=[1, [S_1=a(1, [var]). test('integer') :- is_this_right(4377, {atom, #TS, % A->As list_to_ord_set(B, 1). % % TEST CODE % :- begin_tests(type_inspection). test('empty list') :- is_this_right([], 1). is_disjunction((_;B), 2, 2)] Also, 2)] but got [cyclic, 2)] but got [ground, 2)] For [1, 2)] For 1.234 we have: [atomic, 2)]). setify(A, 2)]). test('float') :- is_this_right(1.234, 2)]). test('nonempty list') :- is_this_right([1, 3, 3|S_1]]), 4)] But there there is a problem with the compound term with the | functor. What is the notation that I have to use to make the test pass?, 4)]). test('dict') :- is_this_right(_{a:1, 5)] but got [nonvar, 5)] For @(S_1, 5)]). test('cyclic list') :- A = [1, a, A). typeof(X, A)) :- compound(X), atom) :- atom(X). typeof(X, atomic) :- atomic(X). typeof(X, B, b]$. As, b2, blob(reserved_symbol), blob(T)) :- blob(X, blob(text)] For foo_the_string we have: [atomic, blob(text)]). test('a string') :- is_this_right("foo_the_string", Bs list_to_ord_set(A, Bs) :- % A, Bs). % B->Bs is_this_right(Value, clause). is_conjunction((_, compound(a, compound(dict, compound(F, conj(Len)) :- nonvar(X), cyclic) :- cyclic_term(X). typeof(X, directive). typeof(_H:-_B, disj(Len)) :- nonvar(X), e, expected ", expected [cyclic, expected [ground, expected [nonvar, expected ~w but got ~w\n", Expected) :- setof(T, fail)). :- end_tests(type_inspection). rt :- run_tests(type_inspection). If we run goal rt, float) :- float(X). % IEEE 754 floats; magnitude: always double (what if long doublr arrives?) typeof(X, G -> b? setify(Expected, Gos), Gos) -> format("For ~w we have: ~w\n", Gos]) ; (format("For ~w, got ", Got) :- % V, ground, ground) :- ground(X). typeof(X, Here is some code to test the type/metatype of SWI-Prolog terms: % var, impl). typeof(:-_A, int, int) :- integer(X). % is there a way to distinguish "machine ints" from "bigints"? typeof(X, is there a way to distinguish "big integers" and "machine integers" (except by checking magnitude?), is_conjunction(X, is_disjunction(X, is_this_right(A, is_this_right(R, LB, Len is Lb+1. is_conjunction((_, Len is Lb+1. is_disjunction((_;_), Len) :- is_conjunction(B, Len) :- is_disjunction(B, Len). % being a "list" is specific nonlocal phenomenon typeof(X, Len). typeof((_A->_B), Len). typeof(X, length(Args, length(X, list([3, list(0)] For foo_the_atom we have: [atom, list(0)]). test('an atom') :- is_this_right(foo_the_atom, list(Len)) :- is_list(X), nonvar, nonvar are meta-question about the state of computation, nonvar) :- nonvar(X). % nonvar(X) is the complement of var(X) typeof(X, not about the term % Note: X can be a variable (if it is fresh) or a nonvar (if it is set) % but x can ever only be a nonvar typeof(X, Number, number) :- number(X). % rationals are not (yet) numbers typeof(X, number] For 0.3333333333333333 we have: [atomic, number] For 2/6 we have: [ground, number] For 5r6 we have: [atomic, number]). test('/ compound') :- is_this_right(2/6, number]). test('float') :- A is 2/6, number]). test('rational') :- is_this_right(5r6, rational) :- rational(X). % covers "big decimals" too typeof(X, rational] For @(S_1, rational] For 43759624750933342417 we have: [atomic, rational] For 5600.556 we have: [atomic, rational] For 5r6 we have: [atomic, rational]). test('big integer') :- is_this_right(43759624750933342417, rational]). test('cyclic compound') :- A = a(1, rational]). test('decimal') :- is_this_right(5600.556, rational]). test('rational') :- R is 5 rdiv 6, S_1)]) we have: [cyclic, string) :- string(X). typeof(X, string] For _15852 we have: [var] For 4377 we have: [atomic, string]). test('a variable') :- is_this_right(_VAR, T). typeof(X, Ts). verdict(Value, typeof(Value, var) :- var(X), verdict(Value, we don't look for any other match typeof(X, we get this (not as well formatted): These tests pass: For [] we have: [atomic, X =.. [F|Args]