>From 1c6709eaf1d91c981521a9a8e2af3ec45e1066b5 Mon Sep 17 00:00:00 2001
From: Michael Hanus <mh@informatik.uni-kiel.de>
Date: Sat, 9 Feb 2019 18:28:34 +0100
Subject: [PATCH 1/2] Loading of external Prolog files fixed for local modules

---
 Makefile             |  4 ++--
 src/loader.pl        | 11 ++++++++---
 src/sicstusbasics.pl | 20 +++++++++++++++++---
 src/swibasics.pl     | 20 +++++++++++++++++---
 4 files changed, 44 insertions(+), 11 deletions(-)

diff --git a/Makefile b/Makefile
index dcf6bef..bfe1b8f 100644
--- a/Makefile
+++ b/Makefile
@@ -49,9 +49,9 @@ export MAJORVERSION=2
 # The minor version number:
 export MINORVERSION=1
 # The revision version number:
-export REVISIONVERSION=0
+export REVISIONVERSION=1
 # The build version number (if >0, then it is a pre-release)
-BUILDVERSION=0
+BUILDVERSION=1
 # Complete version:
 VERSION=$(MAJORVERSION).$(MINORVERSION).$(REVISIONVERSION)
 # The version date:
diff --git a/src/loader.pl b/src/loader.pl
index bad6170..f266c95 100644
--- a/src/loader.pl
+++ b/src/loader.pl
@@ -5,10 +5,10 @@
 :- module(loader,
 	  [loadedModule/2, initializationsInProg/1,
 	   currentCostCenters/1, curryModule/1, costCenters/1,
-	   initializeBeforeLoad/0, initializationsInModule/1,
+	   initializationsInModule/1,
 	   loadAndCompile/3, importModule/1, checkPrologTarget/2]).
 
-:- dynamic loadedModule/2, importedModule/1,
+:- dynamic loadedModule/2, importedModule/1, currentPrologLoadFile/1,
 	   initializationsInProg/1, currentCostCenters/1.
 
 :- use_module(prologbasics).
@@ -16,13 +16,16 @@
 :- use_module(basics).
 %:- use_module(compiler). % for generateMainPlFile in interactive mode
 
+currentPrologLoadFile(''). % name of Prolog file loaded by loadAndCompile/1
 initializationsInProg(true). % intializations to be done after loading Curry program
 currentCostCenters(['']). % list of current cost centers
 
 
 % set name of current Curry module:
 curryModule(ModName) :-
-        (loadedModule(ModName,ModFile) -> true ; ModFile=''),
+        (loadedModule(ModName,ModFile)
+         -> true
+          ; currentPrologLoadFile(ModFile)),
         retract(currentModuleFile(_,_)),
         asserta(currentModuleFile(ModName,ModFile)).
 
@@ -52,6 +55,8 @@ initializationsInModule(ModInit) :-
 
 % load a complete translated application by compiling the Prolog programs:
 loadAndCompile(PrologFile,AddImports,CreateMain) :-
+        retract(currentPrologLoadFile(_)),
+        assert(currentPrologLoadFile(PrologFile)),
 	initializeBeforeLoad,
 	compilePrologFileAndSave(PrologFile),
 	currentModuleFile(MainMod,_),
diff --git a/src/sicstusbasics.pl b/src/sicstusbasics.pl
index 14eea6e..b74a941 100644
--- a/src/sicstusbasics.pl
+++ b/src/sicstusbasics.pl
@@ -842,13 +842,16 @@ ensure_lib_loaded(Lib) :-
         % second, look into the directory of the current module:
         currentModuleFile(Mod,PMod),
         % drop last Prolog file name:
-        atom_codes(PMod,PModS), atom_codes(Mod,ModS),
+        atom_codes(PMod,PModS),
+        atom_codes(Mod,ModS), modNameFileName(ModS,ModFS),
         append(PModwopl,".pl",PModS),
-        append(PModDirS,ModS,PModwopl),
+        append(PModDirS,ModFS,PModwopl),
         atom_codes(PModDir,PModDirS),
         % compute module directory by going up in the hierarchy:
         appendAtom(PModDir,'../../',ModDir),
-        appendAtom(ModDir,Lib,ModDirLib),
+        baseDirName(ModFS,BaseDirS), atom_codes(BaseDir,BaseDirS),
+        appendAtom(ModDir,BaseDir,ModBaseDir),
+        appendAtom(ModBaseDir,Lib,ModDirLib),
 	appendAtom(ModDirLib,'.pl',ModDirLibPl),
 	existsFile(ModDirLibPl), !,
 	(verbosity(3) -> write('>>> Load Prolog library: '),
@@ -863,6 +866,17 @@ ensure_lib_loaded(Lib) :-
                        ; true),
 	ensure_loaded(user:DirLib).
 
+% get base directory name of a hierachical file name
+baseDirName(FileS,BaseDirS) :-
+        append(Base1,[47|File1S],FileS), !,
+        baseDirName(File1S,BaseDir1S),
+        append(Base1,[47|BaseDir1S],BaseDirS).
+baseDirName(_,[]).
+
+% relation between (hierarchical) module names and their file names
+modNameFileName(MN,FN) :- map2M(prologbasics:dotSlash,MN,FN).
+dotSlash(46,47) :- !.
+dotSlash(N,N).
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % call a goal and return list of suspended goals:
diff --git a/src/swibasics.pl b/src/swibasics.pl
index d6bd7f9..a2367c5 100644
--- a/src/swibasics.pl
+++ b/src/swibasics.pl
@@ -618,13 +618,16 @@ ensure_lib_loaded(Lib) :-
         % second, look into the directory of the current module:
         currentModuleFile(Mod,PMod),
         % drop last Prolog file name:
-        atom_codes(PMod,PModS), atom_codes(Mod,ModS),
+        atom_codes(PMod,PModS),
+        atom_codes(Mod,ModS), modNameFileName(ModS,ModFS),
         append(PModwopl,[46,112,108],PModS), % append suffix ".pl"
-        append(PModDirS,ModS,PModwopl),
+        append(PModDirS,ModFS,PModwopl),
         atom_codes(PModDir,PModDirS),
         % compute module directory by going up in the hierarchy:
         appendAtom(PModDir,'../../',ModDir),
-        appendAtom(ModDir,Lib,ModDirLib),
+        baseDirName(ModFS,BaseDirS), atom_codes(BaseDir,BaseDirS),
+        appendAtom(ModDir,BaseDir,ModBaseDir),
+        appendAtom(ModBaseDir,Lib,ModDirLib),
 	appendAtom(ModDirLib,'.pl',ModDirLibPl),
 	existsFile(ModDirLibPl), !,
 	(verbosity(3) -> write('>>> Load Prolog library: '),
@@ -639,6 +642,17 @@ ensure_lib_loaded(Lib) :-
                        ; true),
 	ensure_loaded(user:DirLib).
 
+% get base directory name of a hierachical file name
+baseDirName(FileS,BaseDirS) :-
+        append(Base1,[47|File1S],FileS), !,
+        baseDirName(File1S,BaseDir1S),
+        append(Base1,[47|BaseDir1S],BaseDirS).
+baseDirName(_,[]).
+
+% relation between (hierarchical) module names and their file names
+modNameFileName(MN,FN) :- map2M(prologbasics:dotSlash,MN,FN).
+dotSlash(46,47) :- !.
+dotSlash(N,N).
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % Define hook predicate to suppress warnings for redefined procedures
-- 
2.11.0

