1
votes

I realised that my first question was too long and no one would understand it.

Here is the link to a condensed simpler question that exhibits my problem in a shorter program.

[Condened Question Link] Prolog - Not returning desired value

==================================================================================

I am trying to write a Brainf*ck interpreter in Prolog.

The function bf will take 3 strings. Only relevant parts of the code is as follows:

%Prog - string of BF program
%Input- string of inputs for the BF program
%Output - what the BF program will output

bf(Prog, Input,Output):- run(Prog, Input, Output).

run(Prog, Input, Output) :-
    create_mem(Mem),
    iterateBF(Prog,[],Mem,0, Input, Output).

%start by creating the memory space
create_mem(Memory) :-
    var(Memory), %check if it is uninstantiated variable
    create_mem(Memory,10000).

create_mem([],0) :- !.
create_mem([0|Tail],Size) :-
    NewSize is Size - 1,
    create_mem(Tail,NewSize).

%get memory value at ptr location
get_mem([Value|_],Value,0). %base case
get_mem([_|Tail],Value,Ptr) :-
  Ptr > 0,
    NewPtr is Ptr - 1,
    get_mem(Tail,Value,NewPtr).

%for adding a value to memory space
%need to mod with 256 due to the it being 8 bits to get the actual value.

increase_mem([Value|MemTail],[NewValue|MemTail],0) :-
    NewValue is (Value + 1) mod 256.
increase_mem([Head|Tail],[Head|NewTail],Ind) :-
    Ind > 0,
    NewInd is Ind - 1,
    increase_mem(Tail,NewTail,NewInd).

%iterateBF(Prog,[],Mem,0, Input, []).
%when reach the end of the the

iterateBF([],_,_,_,_,[]) :- !.
iterateBF([],_,_,_,_,_) :-!.
iterateBF(Instruction,Inst_Iterated,Mem,Ptr,Input,Output) :-

   checkInstruction(Instruction,Inst_Iterated,Mem,Ptr,Input,Output,
      NewInstruction,NewInst_Iterated,NewMem,NewPtr,NewInput,NewOutput),
    iterateBF(NewInstruction,NewInst_Iterated,NewMem,NewPtr,NewInput,NewOutput).


checkInstruction([Plus|ProgTail],Inst_Iterated,Mem,Ptr,Input,Output,ProgTail,Inst_Iterated,NewMem,Ptr,Input,Output) :-
        char_code('+',Plus),
        increase_mem(Mem,NewMem,Ptr), !.

%get the memory value at ptr
%Put into Output
checkInstruction([Period|ProgTail],Inst_Iterated,Mem,Ptr,Input,Output,ProgTail,Inst_Iterated,Mem,Ptr,Input,NewOutput) :-
    char_code('.',Period),
    NewOutput = [Output|Value],
    get_mem(Mem,Value,Ptr),!.

The above code will compile and only work for:

bf("+.","",Out).

SWI-Prolog will just return true, while I want it to return the value of Out.

Tracing it in SWI, whenever it returns to the call of iterateBF(Prog,[],Mem,0, Input, Output) in run, Output just loses its previous value.

You just need to pay attention to the following functions:

  1. Run
  2. iterateBF
  3. checkInstructions

The rest are just for compilation sake.

1
@iccthedral thanks, im working on it.ali
@iccthedral here is the SSCCE version of my problem. I hope you can help. Thanks.ali

1 Answers

1
votes

your checkInstruction([Plus|ProgTail],Inst_Iterated,Mem,Ptr,Input,Output,ProgTail,Inst_Iterated,NewMem,Ptr,Input,Output) it's way too complex.

And indeed your problem it's that Output doesn't get correctly bound. But instead of a 'banal' correction, what about a general improvement:

Try to comment your code, what you expect it will do, and after done, possibly simplify it as far as possible.