DEV Community

Lucian Green
Lucian Green

Posted on

How to work with files with State Saving Interpreter Web Service

State Saving Interpreter Web Service is a Prolog compiler written in Prolog that can create a multi-page web app from a single Prolog algorithm. The following List Prolog programs save and load files using SSI-WS.

The following program takes data from an HTML form and stores it in a file.

testopen_cases(5,[[n,test]],

[[[n,test],":-",[[[n,writeln],["Enter your name:"]],[[n,read_string],[[v,s]]],
[[n,write_file],['file.txt',["Name",[v,s]]]]
]],
[[n,write_file],[[v,path],[v,file_term]],":-",
[
    [[n,list_to_string],[[v,file_term],[v,file_terma]]],
    [[n,stringconcat],[[v,file_terma],".",[v,file_termb]]],
    [[n,open],[[v,path],write,[v,stream1]]],
    [[n,write],[[v,stream1],[v,file_termb]]],
    [[n,close],[[v,stream1]]]
]],

[[n,list_to_string],[[v,a],[v,b]],":-",
[
    [[n,"->"],
    [
        [[n,string],[[v,a]]],

        [[n,wrap_if_string],[[v,a],[v,b]]]
    ]],
    [[n,cut]]
]],

[[n,list_to_string],[[v,a],[v,b]],":-",
[
    [[n,list_to_string],[[v,a],"",[v,b]]]
]],
[[n,list_to_string],["",[v,b],[v,c]],":-",
[
    [[n,concat],[[v,b],"\"\"","",[v,c]]]
]],
[[n,list_to_string],[[],[v,b],[v,c]],":-",
[
    [[n,concat],["[",[v,b],"]",[v,c]]]
]],
[[n,list_to_string],[[v,a],"",[v,c]],":-",
[
    [
    [[n,"->"],
    [
        [[n,atom],[[v,a]]],

        [[n,true]],

        [
        [[n,number],[[v,a]]]
        ]
    ]],
    [[n,atom_string],[[v,a],[v,c]]]
    ]
]],
[[n,list_to_string],[[v,a],[v,b],[v,c]],":-",
[
    [[n,"->"],
    [
        [
        [[n,not],[[[n,"->"],[[[n,equals4],[[v,a],[[v,a1],"|",[v,a2]]]],[[n,true]],
        [[n,equals4],[[v,a],[]]]]]
        ]]
        ],

        [
        [[n,"->"],
        [
            [[n,equals4],[[v,b],""]],

            [[n,equals4],[[v,g],""]],

            [[n,equals4],[[v,g],[","]]]
        ]],
        [[n,concat],[[v,b],[v,g],[v,a],[v,c]]]
        ],

        [
        [[n,equals4],[[v,a],[[v,d],"|",[v,e]]]],
        [[n,wrap_if_string],[[v,d],[v,d1]]],
        [[n,list_to_string],[[v,d1],"",[v,f]]],
        [[n,"->"],
        [
            [[n,equals4],[[v,b],""]],

            [
            [[n,equals4],[[v,g],""]],
            [[n,equals4],[[v,f],[v,f2]]]
            ],

            [
            [[n,equals4],[[v,g],","]],[[n,equals4],[[v,f],[v,f2]]]
            ]
        ]],
        [[n,concat],[[v,b],[v,g],[v,f2],[v,f1]]],
        [[n,list_to_string],[[v,e],[v,f1],[v,c]]]
        ]
    ]]
]],
[[n,wrap_if_string],[[v,a],[v,b]],":-",
[
    [[n,"->"],
    [
        [
        [[n,not],[[[n,equals4],[[v,a],""]]]],
        [[n,string],[[v,a]]]
        ],

        [[n,maplist],[[n,string_concat],["\"",[v,a],"\""],"",[v,b]]],

        [[n,equals4],[[v,a],[v,b]]]
    ]]
]],
[[n,concat],[[v,b],[v,g],[v,a],[v,c]],":-",
[
    [[n,maplist],[[n,string_concat],[[v,b],[v,g],[v,a]],"",[v,c]]]
]]
]

).
Enter fullscreen mode Exit fullscreen mode

Screenshot 2022-11-30 at 12 21 06 am
Figure 1. Web form asks for the user's name, to be stored in a file.

Screenshot 2022-11-30 at 12 22 21 am
Figure 2. The file file.txt contains ["Name","Lucian"].

The following program loads data from the file and displays it on the page.

testopen_cases(6,[[n,test]],

[
[[n,test],":-",[[[n,open_file],['file.txt',["Name",[v,t]]]],
[[n,writeln],[["Name",[v,t]]]]]],

[[n,open_file],[[v,path],[v,file_term]],":-",
[
    [[n,open],[[v,path],read,[v,stream]]],
    [[n,read],[[v,stream],[v,file_term]]],
    [[n,close],[[v,stream]]]
]],

[[n,list_to_string],[[v,a],[v,b]],":-",
[
    [[n,"->"],
    [
        [[n,string],[[v,a]]],

        [[n,wrap_if_string],[[v,a],[v,b]]]
    ]],
    [[n,cut]]
]],

[[n,list_to_string],[[v,a],[v,b]],":-",
[
    [[n,list_to_string],[[v,a],"",[v,b]]]
]],
[[n,list_to_string],["",[v,b],[v,c]],":-",
[
    [[n,concat],[[v,b],"\"\"","",[v,c]]]
]],
[[n,list_to_string],[[],[v,b],[v,c]],":-",
[
    [[n,concat],["[",[v,b],"]",[v,c]]]
]],
[[n,list_to_string],[[v,a],"",[v,c]],":-",
[
    [
    [[n,"->"],
    [
        [[n,atom],[[v,a]]],

        [[n,true]],

        [
        [[n,number],[[v,a]]]
        ]
    ]],
    [[n,atom_string],[[v,a],[v,c]]]
    ]
]],
[[n,list_to_string],[[v,a],[v,b],[v,c]],":-",
[
    [[n,"->"],
    [
        [
        [[n,not],[[[n,"->"],[[[n,equals4],[[v,a],[[v,a1],"|",[v,a2]]]],[[n,true]],
        [[n,equals4],[[v,a],[]]]]]
        ]]
        ],

        [
        [[n,"->"],
        [
            [[n,equals4],[[v,b],""]],

            [[n,equals4],[[v,g],""]],

            [[n,equals4],[[v,g],[","]]]
        ]],
        [[n,concat],[[v,b],[v,g],[v,a],[v,c]]]
        ],

        [
        [[n,equals4],[[v,a],[[v,d],"|",[v,e]]]],
        [[n,wrap_if_string],[[v,d],[v,d1]]],
        [[n,list_to_string],[[v,d1],"",[v,f]]],
        [[n,"->"],
        [
            [[n,equals4],[[v,b],""]],

            [
            [[n,equals4],[[v,g],""]],
            [[n,equals4],[[v,f],[v,f2]]]
            ],

            [
            [[n,equals4],[[v,g],","]],[[n,equals4],[[v,f],[v,f2]]]
            ]
        ]],
        [[n,concat],[[v,b],[v,g],[v,f2],[v,f1]]],
        [[n,list_to_string],[[v,e],[v,f1],[v,c]]]
        ]
    ]]
]],
[[n,wrap_if_string],[[v,a],[v,b]],":-",
[
    [[n,"->"],
    [
        [
        [[n,not],[[[n,equals4],[[v,a],""]]]],
        [[n,string],[[v,a]]]
        ],

        [[n,maplist],[[n,string_concat],["\"",[v,a],"\""],"",[v,b]]],

        [[n,equals4],[[v,a],[v,b]]]
    ]]
]],
[[n,concat],[[v,b],[v,g],[v,a],[v,c]],":-",
[
    [[n,maplist],[[n,string_concat],[[v,b],[v,g],[v,a]],"",[v,c]]]
]]
]

).
Enter fullscreen mode Exit fullscreen mode

Screenshot 2022-11-30 at 12 23 12 am
Figure 3. The web page displays [Name,Lucian], loaded from the file.

Download SSI at https://github.com/luciangreen/SSI

Convert Prolog to List Prolog https://github.com/luciangreen/Prolog-to-List-Prolog

Top comments (0)