How to work with files with State Saving Interpreter Web Service
Tags: prolog, web, app, files
Published: 2022-11-29 13:36:35

---
title: How to work with files with State Saving Interpreter Web Service
published: true
description: Make a Prolog web app that saves, loads files
tags: Prolog, web, app, files
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2022-11-29 13:13 +0000
---

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]]]
]]
]

).
```

<img width="408" alt="Screenshot 2022-11-30 at 12 21 06 am" src="https://user-images.githubusercontent.com/15845542/204540514-9f7ff26d-ca00-43d1-acfe-5218e6b3625d.png">
Figure 1. Web form asks for the user's name, to be stored in a file.

<img width="157" alt="Screenshot 2022-11-30 at 12 22 21 am" src="https://user-images.githubusercontent.com/15845542/204540508-07689c53-a8cd-473c-856f-efe8ae039456.png">
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]]]
]]
]

).
```

<img width="280" alt="Screenshot 2022-11-30 at 12 23 12 am" src="https://user-images.githubusercontent.com/15845542/204540481-85286509-34d5-42e3-879a-b507d3afb219.png">
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