Skip to content
Snippets Groups Projects
Commit d2ac8d99 authored by Michael Hanus's avatar Michael Hanus
Browse files

Prolog implementation of primitives integrated into package

parent aeaa5efa
Branches master
Tags v1.1.0
No related merge requests found
{
"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",
......
......@@ -29,7 +29,6 @@
---
--- @author Michael Hanus
--- @version March 2010
--- @category general
------------------------------------------------------------------------------
module GlobalVariable (GVar, gvar, writeGVar, readGVar )
......
......@@ -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>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 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).
......@@ -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 :: PropIO
testGlobalVariableTest2 = m2 `returns` (42, Just 99, Just 99)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment