Prototype Prolog Projects

Mocking Up Elementary Parsers and Interpreters in Spec to Algorithm

In Spec to Algorithm (S2A), creating easy parsers and interpreters is simple and efficient. By breaking down complicated expressions into manageable parts, S2A allows developers to…


This content originally appeared on DEV Community and was authored by Lucian Green

Spec to Algorithm
Ideas to Algorithm

Mocking Up Elementary Parsers and Interpreters in Spec to Algorithm

In Spec to Algorithm (S2A), creating easy parsers and interpreters is simple and efficient. By breaking down complicated expressions into manageable parts, S2A allows developers to generate algorithms speedily. For example, think of the instance instance([1,1], [[is, [1+1]]]). Here, instance is a function that takes inputs [1,1] and computes the expression [is, [1+1]] = 2. S2A parses this expression, identifies the operation is (equality check), and performs the computation.

This method can be extended to various parsing and interpretation jobs. Another instance could be instances([[[1, 2], [[is, [1+2]]]],[[3, 4], [[is, [3+4]]]]]), and produces C is A+B where A, B and C are variables. S2A parses the input, assigns values to A, B and C, and computes the sum of new inputs. Such strengths make S2A an outstanding tool for developers to mock up and test easy parsers and interpreters, streamlining the development process and improving productivity.

S2A can break complex patterns into output, making algorithms out of decision trees to do it. For example, the following query returns the object given subject, verb, (determiner), object. Here, off refers to not breaking items into characters, which would be useful for recombining characters.

spec_to_algorithm([[[input,[['A',[s,v,o]]]],[output,[['B',[o]]]]], [[input,[['A',[s,v,d,o]]]],[output,[['B',[o]]]]]],off,_).

This is saved as the following code:

:-include('auxiliary_s2a.pl').
algorithm(In_vars,
Out_var) :-
findall(Var1,(member(Var,In_vars),
characterise1(Var,Var2),
strings_atoms_numbers(Var2,Var21),
term_to_brackets(Var21,Var1)
),In_vars1),
...

In the following, T1_old, the training input is split into two non-deterministic (nd) branches for matching.

...
T1_old=[[nd,[[[s,v,d,o],[output,[[o]]]],[[s,v,o],[output,[[o]]]]]]],
append(In_vars1,[[output,_]],In_vars3),
rs_and_data_to_term(T1_old,In_vars3,_,[],In_vars2,_T2_old,true),
double_to_single_brackets(In_vars2,In_vars21),
append(In_vars41,[[output,T2_old]],In_vars21),
double_to_single_brackets(In_vars41,In_vars4),
...

In the following line, the map is [[]] because there are no variables (from multiple specs) in the output.

...
member(Map2,[[]]),
double_to_single_brackets(T2_old,T2_old3),
move_vars(Map2,In_vars4,T2_old3,[],Out_var2),
findall(Out_var3,remove_nd(Out_var2,Out_var3),Out_var4),
member(Out_var5,Out_var4),
term_to_list(Out_var5,Out_var6),
[Out_var6]=Out_var.

The following query converts Prolog code into terms and could form and calculate B is (1+2)+3 later. "A" and "B" are downcased in the Term Prolog form, to be converted to 'a' and 'b' afterwards.

spec_to_algorithm([[[input,[['A',["A is 1+2,B is A+3."]]]],[output,[['B',[[[+,[1,2,[lower,['A']]]],[+,[[lower,['A']],3,[lower,['B']]]]]]]]]]],off,_).

The following excerpts from the generated algorithm show that the single spec is pattern-matched to the output. Again, there are no output variables to map, but if there were, they (for example, [A,B]->A) would be mapped from (1,1) (item 1 of the second dimension or list level) to (1) (item 1 of the first list level).

T1_old=[[["A is 1+2,B is A+3."],
[output,[["[","[",+,"[",1,2,"[",lower,"[","A","]","]","]","]","[",+,"[","[",lower,"[","A","]","]",3,"[",lower,"[","B","]","]","]","]","]"]]]]],
...
member(Map2,[[]]),

Lucian Green, Writer of Spec to Algorithm

Images: Spec to Algorithm Title in Input Mono Condensed Thin (fonts.adobe.com) and Ideas to Algorithm Image (Canva).


This content originally appeared on DEV Community and was authored by Lucian Green


Print Share Comment Cite Upload Translate Updates
APA

Lucian Green | Sciencx (2024-07-09T14:11:43+00:00) Prototype Prolog Projects. Retrieved from https://www.scien.cx/2024/07/09/prototype-prolog-projects/

MLA
" » Prototype Prolog Projects." Lucian Green | Sciencx - Tuesday July 9, 2024, https://www.scien.cx/2024/07/09/prototype-prolog-projects/
HARVARD
Lucian Green | Sciencx Tuesday July 9, 2024 » Prototype Prolog Projects., viewed ,<https://www.scien.cx/2024/07/09/prototype-prolog-projects/>
VANCOUVER
Lucian Green | Sciencx - » Prototype Prolog Projects. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/09/prototype-prolog-projects/
CHICAGO
" » Prototype Prolog Projects." Lucian Green | Sciencx - Accessed . https://www.scien.cx/2024/07/09/prototype-prolog-projects/
IEEE
" » Prototype Prolog Projects." Lucian Green | Sciencx [Online]. Available: https://www.scien.cx/2024/07/09/prototype-prolog-projects/. [Accessed: ]
rf:citation
» Prototype Prolog Projects | Lucian Green | Sciencx | https://www.scien.cx/2024/07/09/prototype-prolog-projects/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.