0

I have a Z specification and I have to translate it into Prolog code. I try but it doesn't work so someone can help me?

linkedListInit(LinkedList) :-
   LinkedList = {[linked,L],[size, S]} &
   list(L) &
   S is 0.

insert(LinkedList,Node_i,LinkedList_):-
    insertNode(LinkedList,Node_i,LinkedList_).

insertNode(LinkedList,Node_i,LinkedList_):-
   LinkedList = {[linked,L],[size,S]} &
   concat(L,Node_i,L_) &
   S_ is S + 1 &
   LinkedList_ = {[linked,L_],[size,S_]}.


extract(LinkedList,Node_i,LinkedList_):-
   extractHeadNodeOk(LinkedList,Node_i,Msg_o,LinkedList_)
   or
   extractHeadNodeError(LinkedList,Node_i,Msg_o,LinkedList_).

extractHeadNodeOk(LinkedList,Node_i,Msg_o,LinkedList_):-
   LinkedList = {[linked,L],[size,S]} &
   L neq {} &
   tail(L,L_) &
   head(L,Node_i) &
   S_ is S-1 &
   Msg_o = ok &
   LinkedList_ = {[linked,L_],[size,S_]}.

extractHeadNodeError(LinkedList,Node_i,Msg_o,LinkedList_):-
   LinkedList = {[linked,L] / _} &
   slits(L) &
   L = {} &
   (nran(L,D) &
   Node_i in D) &
   Msg_o = error &
   LinkedList_ = LinkedList.


checkNode(LinkedList,Node_i,LinkedList_):-
   checkNodeOk(LinkedList,Node_i,Msg_o,LinkedList_)
   or
   checkNodeError(LinkedList,Node_i,Msg_o,LinkedList_).

checkNodeOk(LinkedList,Node_i,Msg_o,LinkedList_):-
   LinkedList = {[linked,L],[size,S]} &
   (ran(L,D) &
   Node_i in D) &
   Msg = ok &
   LinkedList_ = LinkedList.

checkNodeError(LinkedList,Node_i,Msg_o,LinkedList_):-
   LinkedList = {[linked,L] / _} &
   (nran(L,D) &
   Node_i in D) &
   Msg = error &
   LinkedList_ = LinkedList.


removePartialNode(LinkedList,Node_i,LinkedList_):-
   removePartialNodeOk(LinkedList,Node_i,Msg_o,LinkedList_)
   or
   removePartialNodeError(LinkedList,Node_i,Msg_o,LinkedList_).

removePartialNodeOk(LinkedList,Node_i,Msg_o,LinkedList_):-
   LinkedList = {[linked,L],[size,S]} &
   (ran(L,D) &
   Node_i in D) &
   dares(dom(filter(L,Node_i,R),A),L,L_) &
   Msg_o = ok &
   LinkedList_ = {[linked,L_],[size,S_]}.

removePartialNodeError(LinkedList,Node_i,Msg_o,LinkedList_):-
   LinkedList = {[linked,L] / _} &
   (nran(L,D) &
   Node_i in D) &
   Msg_o = error &
   LinkedList_ = LinkedList.


removePartialData(LinkedList,Node_i,Data_i,LinkedList_):-
   removePartialDataOk(LinkedList,Node_i,Data_i,Msg_o,LinkedList_)
   or
   removePartialDataError(LinkedList,Node_i,Data_i,Msg_o,LinkedList_).

removePartialDataOk(LinkedList,Node_i,Data_i,Msg_o,LinkedList_):-
   LinkedList = {[linked,L],[size,S]} &
   (ran(L,D) &
   Node_i in D) &
   (ran(Data_i,F) &
   Data_i in F) &
   dares(dom(filter(L,rres(Data_i,Node_i,S),A),L,L_) &
   Msg_o = ok &
   LinkedList_ = {[linked,L_],[size,S_]}.   

removePartialDataError(LinkedList,Node_i,Data_i,Msg_o,LinkedList_):-
   LinkedList = {[linked,L] / _} &
   (ran(L,D) &
   Node_i in D) &
   (nran(Data_i,F) &
   Data_i in F) &
   Msg_o = error &
   LinkedList_ = LinkedList.
false
  • 10,264
  • 13
  • 101
  • 209
  • 3
    The reason it doesn't work is simple, this is not valid prolog syntax. – Tasos Papastylianou Feb 10 '20 at 12:49
  • Never heard about [Z Notation](https://en.wikipedia.org/wiki/Z_notation). Aren't modern language concepts based off Martin-Löf Type Theory & Lambda Calculus instead of Zermelo-Fraenkel Set theory & FOL? Anyway, why not post the spec, too? Although the appearance of _LinkedList_ indicates that this is about a space with linkable, stateful structures, which is not Prolog's domain. – David Tonhofer Feb 10 '20 at 15:04
  • I would greatly enjoy the Z notation you're attempting to meet. But even Prolog is not high-level enough to directly execute Z notation, which intentionally omits operational concerns. In the mean time, `&` should be written `,` and `or` should be written `;` if you want Prolog to do something with it. – Daniel Lyons Feb 10 '20 at 23:17
  • This doesn't look like valid Prolog (unless you're using a variant I haven't seen before). What Prolog are you using? Also, you would need to provide the Z-notation for anyone to help you. – Jim Feb 18 '20 at 21:45

0 Answers0