Iβm on
$ swipl --version
SWI-Prolog version 9.3.32 for x86_64-linux
And I wrote this code
:- use_module( library( 'clpfd')). % transpose
xmas( `XMAS`).
direction8( IDX_R_C) :- IDX_R_C = ( ROW, COL)
, FACTORS=[-1,0,1]
, member( ROW, FACTORS)
, member( COL, FACTORS)
, IDX_R_C \= ( 0, 0)
.
stretch8( IDX_R_C_OFFSET, IDX_R_C, L) :- IDX_R_C_OFFSET = ( ROW_O, COL_O), IDX_R_C = ( ROW, COL)
, TERM = ( between( 1, 3, F), R is ROW_O + F*ROW, C is COL_O + F*COL)
, findall( (R, C), TERM, L)
.
check_mas( IDX_R_C, COUNT) :- true
, xmas( [_|MAS])
, TERM1 = ( direction8( D) , stretch8( IDX_R_C, D, IDX_R_C_LIST),
transpose( [IDX_R_C_LIST,MAS], IDX_R_C__CHAR__LIST))
, TERM2 = forall( member( [IDX_R_C_INNER, CHAR], IDX_R_C__CHAR__LIST),
char_index( CHAR, IDX_R_C_INNER))
, findall( 1, ( TERM1, TERM2), L)
, length( L, COUNT)
.
count_database(COUNT) :- true
, xmas( [X|_])
, TERM = ( char_index( X, IDX_R_C), check_mas( IDX_R_C, COUNT) )
, findall( COUNT, TERM, L)
, sum_list(L, COUNT)
.
fill_database3 :- true
, xmas( XMAS)
, TERM1 = ( between( 0, 139, ROW), between( 0, 139, COL))
, TERM2 = ( CHAR_IDX is ( ROW + COL ) mod 4, nth0( CHAR_IDX, XMAS, CHAR))
% , abolish( char_index/2)
% , dynamic( char_index/2)
, forall( (TERM1, TERM2), assertz( char_index( CHAR, (ROW, COL))))
.
print_contents_of_char_index :- true
, TERM1 = ( between( 0, 139, ROW) )
, TERM2 = ( between( 0, 139, COL), char_index( CHAR, (ROW,COL)))
, TERM3 = findall( CHAR, TERM2, LINE)
, forall( (TERM1, TERM3), format( '~s\n', [ LINE]))
.
demo_001 :- true
% , fill_database3
, aggregate_all( count, char_index(_,_), CI_COUNT)
, writeln( CI_COUNT)
, count_database(COUNT)
, writeln( COUNT)
.
I have currently no idea how to reduce it.
Now look at the execution times:
?- fill_database3, time( demo_001).
19600
9590
% 2,922,053 inferences, 5.115 CPU in 5.133 seconds (100% CPU, 571319 Lips)
true.
?- compile_predicates( [char_index/2]).
true.
?- time( demo_001).
19600
9590
% 2,919,998 inferences, 0.254 CPU in 0.254 seconds (100% CPU, 11506448 Lips)
true.
?- abolish( char_index/2), fill_database3, time( demo_001).
19600
9590
% 2,919,998 inferences, 14.312 CPU in 14.324 seconds (100% CPU, 204018 Lips)
true.
?- compile_predicates( [char_index/2]).
true.
?- time( demo_001).
19600
9590
% 2,919,998 inferences, 22.935 CPU in 22.942 seconds (100% CPU, 127318 Lips)
true.
This looks weird.
Thanks in advance,
Frank Schwidom