:- module(programCache,[]).  % converted djd
:- style_check(-singleton). % added djd
%% :- module('programCache', []).

:- discontiguous 'programCache.unCacheProgram.p2'/2. %% added djd
:- discontiguous '~programCache.unCacheProgram.p2'/2. %% added djd


:- op(500, yfx, and).
:- op(400, yfx, or).

'programCache.unCacheProgram.p2'('progDefs.cachedProgram.f1'(A), B) :-
        'programCache.getCachedProgram.p2'(A, B).
'~programCache.unCacheProgram.p2'('progDefs.cachedProgram.f1'(A), B) :-
        '~programCache.getCachedProgram.p2'(A, B).
'programCache.unCacheProgram.p2'('progDefs.program.f4'(A,B,C,D), 'progDefs.program.f4'(A,B,C,D)).
'~programCache.unCacheProgram.p2'('progDefs.program.f4'(A,B,C,D), 'progDefs.program.f4'(A,B,C,D)).
%------------------------------------------------------------------------------
% Program cache
%------------------------------------------------------------------------------

'programCache.getCachedProgram.p2'(Main, Program) :-
   ( user:cached_program(Main, Cached) ->
     Program = Cached
   ; find_program(Main, Program),
     assert(user:cached_program(Main, Program))
   ).

'~programCache.getCachedProgram.p2'(Main, Program) :-
   'programCache.getCachedProgram.p2'(Main, Program).


find_program(Main, Program) :-
   'strings':concat(Main, '".prm', GFile),
   user:gstring2string(GFile, File),
   ( open(File, read, Stream),
     user:readin_program(Stream, Object) ->
     Program = Object
   ; 'strings':'strings.stringInts.p2'(Main, Chars),
     format(user_error, 'ERROR: Couldn''t find cached object program ~s~n'
        , [Chars]),
     raise_exception(catch_in_query)  
   ).

