diff --git a/package.json b/package.json index 6f3f8336da93c0addf15da9fcdab168ab5694b1e..34705245da6fa048e982650c2ac430f44e6e3997 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,16 @@ { "name": "global-variables", - "version": "1.0.0", + "version": "1.1.0", "author": "Michael Hanus <mh@informatik.uni-kiel.de>", "synopsis": "Library for handling global variables in PAKCS", "category": [ "Programming" ], + "dependencies": { + "base" : ">= 1.1.0, < 2.0.0" + }, "dependencies": { }, "exportedModules": [ "GlobalVariable" ], "compilerCompatibility": { - "pakcs": ">= 2.0.0, < 3.0.0" + "pakcs": ">= 2.2.1, < 3.0.0" }, "testsuite": { "src-dir": "test", diff --git a/src/GlobalVariable.curry b/src/GlobalVariable.curry index 154288aee6c7366221820d8455b7af7ff2b9008b..8a431897acee46a861356403e233a5a4c4dc73f1 100644 --- a/src/GlobalVariable.curry +++ b/src/GlobalVariable.curry @@ -29,7 +29,6 @@ --- --- @author Michael Hanus --- @version March 2010 ---- @category general ------------------------------------------------------------------------------ module GlobalVariable (GVar, gvar, writeGVar, readGVar ) diff --git a/src/GlobalVariable.pakcs b/src/GlobalVariable.pakcs index 5981b47718c5a508c8c17dd41981bd0d2ce705ac..fd3635f2c592925770a3f4bb736fa0da3b9a05cb 100644 --- a/src/GlobalVariable.pakcs +++ b/src/GlobalVariable.pakcs @@ -2,11 +2,9 @@ <!DOCTYPE primitives SYSTEM "http://www.informatik.uni-kiel.de/~pakcs/primitives.dtd"> <primitives> <primitive name="prim_readGVar" arity="1"> - <library>prim_globalvar</library> <entry>prim_readGVar[raw]</entry> </primitive> <primitive name="prim_writeGVar" arity="2"> - <library>prim_globalvar</library> <entry>prim_writeGVar[raw]</entry> </primitive> </primitives> diff --git a/src/GlobalVariable.pakcs.pl b/src/GlobalVariable.pakcs.pl new file mode 100644 index 0000000000000000000000000000000000000000..5fbb5f3557893a11dd894e1515eaebda6e5b5b01 --- /dev/null +++ b/src/GlobalVariable.pakcs.pl @@ -0,0 +1,48 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% Prolog implementation of builtins of module GlobalVariable: +% + +% initialize the predicate containing the global value if called for the +% first time: +initGlobalVariable(GlobNameInProg,Exp,GlobResult,E0,E) :- + appendAtom('$GLOBAL_',GlobNameInProg,GlobName), + user:nf(Exp,Val,E0,E1), + GlobalHead =.. [GlobNameInProg,_,_,_], + user:retractClause(GlobalHead,_), + GlobResult = 'GlobalVariable.GVarValue'(GlobName,Val), + NewGlobalFact =.. [GlobNameInProg,GlobResult,Ev,Ev], + % redefine initial clause for global variable: + asserta(user:NewGlobalFact), + E=E1, + !. + +% get the value associated to a global variable: +?- block prim_readGVar(?,?,-,?). +prim_readGVar(GV,partcall(1,prim_readGVarWorld,[GV]),E,E). + +?- block prim_readGVarWorld(?,?,?,-,?). +prim_readGVarWorld('GlobalVariable.GVarValue'(GV,IVal),_World, + '$io'(Val),E0,E) :- + E0 = eval(Bs), + (findGVar(Bs,GV,Val) -> E=E0 ; Val=IVal, E = eval([GV/Val|Bs])). + +findGVar([Var/Value|_],Var,Value) :- !. +findGVar([_|Bs],Var,Value) :- findGVar(Bs,Var,Value). + + +% set the value associated to a global value: +?- block prim_writeGVar(?,?,?,-,?). +prim_writeGVar(GV,Val,partcall(1,prim_writeGVarWorld,[Val,GV]),E,E). + +?- block prim_writeGVarWorld(?,?,?,?,-,?). +prim_writeGVarWorld('GlobalVariable.GVarValue'(GV,IVal),Val,_World, + '$io'('Prelude.()'),E0,E) :- + E0 = eval(Bs), + updateGVar(Bs,GV,Val,NewBs), + E = eval(NewBs). + +updateGVar([],Var,Value,[Var/Value]). +updateGVar([Var/_|Bs],Var,Value,[Var/Value|Bs]) :- !. +updateGVar([B|Bs],Var,Value,[B|NBs]) :- updateGVar(Bs,Var,Value,NBs). + diff --git a/test/TestGlobalVariable.curry b/test/TestGlobalVariable.curry index f61d84749ffe499d8623554af2bb027331aee779..03944a11054f0063db9879ee1303a750ecfc8172 100644 --- a/test/TestGlobalVariable.curry +++ b/test/TestGlobalVariable.curry @@ -6,6 +6,7 @@ import Test.Prop g :: GVar Int g = gvar 42 +m1 :: IO (Int,Int) m1 = do writeGVar g 42 v1 <- readGVar g @@ -16,6 +17,7 @@ m1 = do h :: GVar (Maybe Int) h = gvar Nothing +m2 :: IO (Int, Maybe Int, Maybe Int) m2 = do writeGVar g 42 let x free @@ -26,6 +28,8 @@ m2 = do v3 <- readGVar h return (v1,v2,v3) +testGlobalVariableTest1 :: PropIO testGlobalVariableTest1 = m1 `returns` (42,99) -testGlobalVariableTest2 = m2 `returns` (42,Just 99, Just 99) +testGlobalVariableTest2 :: PropIO +testGlobalVariableTest2 = m2 `returns` (42, Just 99, Just 99)