DEV Community

loading...
Cover image for What is Internationalised List Prolog Interpreter?

What is Internationalised List Prolog Interpreter?

luciangreen profile image Lucian Green ・4 min read

I read on Quora that "Prolog ... uses sentences formed in any language as a base for logic programming". See Quora discussion I decided to write a version of my List Prolog Interpreter (LPI) that could run in any language, including system calls.

This article describes installing languages in LPI, generating documentation for LPI in that language and translating algorithms between languages.

List Prolog in English

First, List Prolog in English looks like this, i.e. the reverse predicate:

test(7,[[n,reverse],[[1,2,3],[],[v,l]]],
[
[[n,reverse],[[],[v,l],[v,l]]],
[[n,reverse],[[v,l],[v,m],[v,n]],":-",
[ [[n,head],[[v,l],[v,h]]],
[[n,tail],[[v,l],[v,t]]],
[[n,wrap],[[v,h],[v,h1]]],
[[n,append],[[v,h1],[v,m],[v,o]]],
[[n,reverse],[[v,t],[v,o],[v,n]]]
]
]
],[[[v,l], [3, 2, 1]]]).

List Prolog, originally made for generating algorithms, needs the verbose n's (for predicate names) and v's (for variables) to differentiate them when used in grammars. LP in other languages is the same, except n's, v's and the names themselves are phrases in other languages in double quotes.

Figure 1. An image of the listprologinterpreter/languages folder.

Figure 1. An image of the listprologinterpreter/languages folder.

Installing languages in LPI

So, an example of a language database that enables the interpreter to run algorithms written in French is below:

[...,["any","any","fr","tout"],["append","append","fr","ajouter"],["atom","atom","fr","atome"],["brackets","brackets","fr","supports"],["call","call","fr","appel"],["ceiling","ceiling","fr","plafond"],..., ["stringconcat","concatenate strings","fr","concaténer des chaînes"],["stringtonumber","string to number","fr","chaîne en nombre"],...]

The database entries have four items, 1. the English command name (without numbers), 2. the English command name as a phrase, without underscores, 3. the destination language, French and 4. the French command name.

Databases may be installed in many languages, and translations don't need to be downloaded once the database is installed and the native speaker starts programming.

Generating documentation for LPI in French

First, the documentation in English:

List Prolog Interpreter

Documentation

  • The interpreter is called in the form: international_interpret([lang,en],debug,query,type statements,mode statements,functions,result).

Where:
debug - on or off for trace,
query - the query e.g. [[n,reverse],[[1,2,3],[],[v,l]]]
type statements - e.g. [[n,reverse],[[[t,list],[[t,number]]],[[t,list],[[t,number]]],[[t,list],[[t,number]]]]]
mode statements - e.g. [[n,reverse],[input,input,output]]
functions - the algorithm e.g. see reverse above
result - the result, e.g. [[[v,l], [3, 2, 1]]] - [] indicates failed and [[]] indicates the empty list.

And the documentation in French:

Interpréteur List Prolog

Documentation

  • L'interprète est appelé sous la forme: international_interpret([lang, en], debug, query, instructions de type, instructions de mode, fonctions, result).

Où:
debug - activé ou désactivé pour la trace,
requête - la requête, par exemple [[n, inverse], [[1,2,3], [], [v, l]]]
instructions de type - par exemple [[n, inverse], [[[t, liste], [[t, nombre]]], [[t, liste], [[t, nombre]]], [[t, liste], [[t ,nombre]]]]]
instructions de mode - par exemple [[n, reverse], [input, input, output]]
fonctions - l'algorithme, par ex. voir le verso ci-dessus
résultat - le résultat, par ex. [[[v, l], [3, 2, 1]]] - [] indique un échec et [[]] indique la liste vide.

It's like the chicken and the egg, that is whether the user can generate documentation without instructions in their language about how to install a language and generate documentation, (they would need to Google translate the English instructions). For students, a teacher may set the whole repository as an assignment (only joking) - they might prepare the language and docs, including commands, for students.

Translating algorithms between languages

Below I have translated reverse into French:

trans_alg(
[
[[n,reverse],[[],[v,l],[v,l]]],
[[n,reverse],[[v,l],[v,m],[v,n]],":-",
[ [[n,head],[[v,l],[v,h]]],
[[n,tail],[[v,l],[v,t]]],
[[n,wrap],[[v,h],[v,h1]]],
[[n,append],[[v,h1],[v,m],[v,o]]],
[[n,reverse],[[v,t],[v,o],[v,n]]]
]
]
],"en","fr",A),writeln1(French_reverse).

French_reverse=
[
[["n","inverser"],[["v","l"],["v","l"]]],
[["n","inverser"],[["v","l"],["v","m"],["v","n"]],":-",
[ [["n","tête"],[["v","l"],["v","h"]]],
[["n","queue"],[["v","l"],["v","t"]]],
[["n","emballage"],[["v","h"],["v","h 1"]]],
[["n","ajouter"],[["v","h 1"],
["v","m"],["v","o"]]],
[["n","inverser"],[["v","t"],["v","o"],["v","n"]]]
]
]
]

I wondered why it was translating many words if they were mainly in the database, then I realised all the variable names were being translated using the engine.

Some available languages and codes

Any installed languages may be translated to and from. Borrowing from Translate Shell, the languages are many, below, including Armenian and Scots Gaelic. Translate Shell is a merger of different translation engines, quite interesting.

See also
List Prolog Interpreter
Language Repository for LPI

Image of beach: Stephanie Jensen (einahpets-36020), FreeImages

Discussion (0)

pic
Editor guide