DEV Community

Lucian Green
Lucian Green

Posted on

Prototype Prolog Projects

Spec to Algorithm
Ideas to Algorithm
Thousands of coloured blocks “partying” as popcorn as they transform from ideas to algorithms

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), and object. Here, off refers to not breaking items into characters, which would be helpful for recombining characters.

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

This algorithm 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),
...
Enter fullscreen mode Exit fullscreen mode

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),
...
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

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,_).
Enter fullscreen mode Exit fullscreen mode

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,[[]]),
Enter fullscreen mode Exit fullscreen mode

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).

Top comments (0)