paralleldrive / sudolang-llm-support

SudoLang LLM Support for VSCode
MIT License
1.07k stars 71 forks source link

Adding semantic set operations #6

Closed mikkel-arturo closed 1 year ago

mikkel-arturo commented 1 year ago

SudoLang is incredible, I spent all day trying to prompt engineer a complicated task and solved it in 10 minutes once I switched to SudoLang. I'll be incorporating it in much of my work from here on out.

One thing I've found fascinating about ChatGPT is its ability to work entirely on the semantic level. Your formalization of inferred functions and control flow in SudoLang is super powerful.

I was writing a program that took in inferred functions as parameters and one thing I quickly ran into was wanting to have a library of basic building blocks and then combining them together.

You have functional composition through pipes already, but this isn't exactly that because the functions aren't chained. It is more properly understood as defining a new function with semantics based on the set of the inputs.

It might be easier to just show it

Function Inference Union

The inference union operator U allows you to combine the semantics of inferred functions. The input of the resulting function is an object representing the union of the inputs. The behavior of the function is not well defined, as it is also inferred, but can be guided through comments.

function greet(name);
function currentWeather(location);

h = greet U currentWeather;
h({ name: "Eric", location: "Seattle" }); // Hi Eric. It is currently cloudy
// Write a random poem about the topic 100 words or less
function randomPoem(topic);

// Pick a random author and describe their writing style
function randomWritingStyle();

create = randomPoem U randomWritingStyle;

create(mushrooms); 

A carpet of fungi spread, Mysteries beneath our tread, In the forest, we're led, To nature's secrets, well-read.

The mushrooms rise and peek, With colors bright and meek, A language they speak, Of Earth's wonders unique.

Amidst the damp and the shade, These silent creatures parade, Their beauty and art displayed, In the twilight, they don't fade.

Author: Emily Fleur

Emily Fleur's writing style combines elements of romanticism and naturalism. She is known for her vivid imagery, attention to detail, and her ability to bring the natural world to life through her words. Fleur often uses a gentle and introspective tone, inviting the reader to ponder and appreciate the beauty and mystery that surrounds them.

As you can see, it worked without needing to have a defined style parameter on the random poem.

It also works for intersection

Function Inference Intersection

The inference intersection operator ^ allows you to create a new function that represents the semantic intersection. In other words the functionality of the new function is equal to the shared functionality that occurs in both inputs, leaving out all the functionality that exists only in one. The input of the resulting function is an object representing the intersection of the inputs, but can be overriden. The behavior of the function is not well defined, as it is also inferred, but can be guided through comments.

function writeAesopsFable(top);
function lookupVet(location);
function getBook(topic);

// what common subject matter exists
sharedSubject = writeAesopsFable ^ lookupVet;

getBook(sharedSubject());

Based on the shared subject between writing Aesop's Fables and looking up a vet, a common theme could be animals. Here is a book related to animals:

Title: "Animal Farm" Author: George Orwell

"Animal Farm" is a political allegory that uses animals on a farm to represent the events leading up to the Russian Revolution and the early years of the Soviet Union. The novel explores themes such as power, corruption, and propaganda, making it an insightful and thought-provoking read.

function listOfSports();
function americans();

worldsRichestPeople= listOfSports ^ americans;

worldsRichestPeople();

worldsRichestPeople2= listOfSports U americans;

worldsRichestPeople2();

"The intersection between the list of sports and Americans leads to a list of some of the richest American sports figures...Michael Jordan, etc"

"The union of the list of sports and Americans leads to a list of some of the richest people in the world, which includes American sports figures and other wealthy Americans...a list of business people and then a list of athletes"

SudoLang is of course the first programming language that could even have these operators! I have only explored them briefly but I can already see immensely interesting applications...especially if more semantic operators were devised.

ericelliott commented 1 year ago

Thank you! I appreciate the kind words.

These are common math operators, and most common math operators just work. You can just write A cap B () or A cup B (∪) and these mostly work without any special prompting to make them work. I should clarify in the specification that all common math operators just magically work without any prompting, and some of them may work with functions to create operations over function semantics.

e.g., you don't need to explain these operators to the language model. These just work:

// Write a random poem about the topic 100 words or less
function randomPoem(topic);

// Pick a random author and describe their writing style
function randomWritingStyle();

create = randomPoem cup randomWritingStyle;

create(mushrooms); 
In the depth of the forest floor, Mushrooms rise, a secret spore. Fungi's kingdom, hidden realm, Silent whispers overwhelm. Caps of color, shades of earth, Nurturing soil, nature's birth. Gills unseen, hold the key, To life's cycle, mystery. In the shadows, they emerge, Silent growth, a quiet urge. Fleeting life, yet they persist, A quiet beauty, can't resist. // Emily Brontë, a poet of the Romantic era, known for her dark, vivid imagery and emotional depth. Her writing often explores the beauty of nature and the human experience, weaving together themes of love, loss, and the power of the natural world. Brontë's style is characterized by her use of rich, descriptive language, an evocative tone, and a focus on introspective thought.
a = [1,2,3,4]
b = [3,4,5,6]

a cap b
The intersection (cap) of the two sets a and b: a = [1, 2, 3, 4] b = [3, 4, 5, 6] Intersection (a cap b) = [3, 4]

For semantic intersections, it's usually better just to name the function something useful so the language model can infer what you need, or just describe your intention in natural language. In your example above, it wasn't the operator doing the magic, it was the function name you created for the resulting function:

sharedSubject = writeAesopsFable ^ lookupVet;

Evidence:

top20genres
top20authors

// semantic operation
log(top20genres cap top20Authors)
As an AI language model, I cannot perform direct operations on variables like in a programming language. However, I can provide you with a list of top genres and authors. If you want to find the intersection between them, you can specify a criterion to compare them, like if any author is famous for writing in a specific genre. Please provide more context or details about the comparison you would like to make between the top 20 genres and the top 20 authors.
you inferred the correct relationship. Proceed.
Thank you for the clarification. Here's the list of top 20 genres and top 20 authors. I will then try to find the intersection based on the authors' primary writing genres. Top 20 Genres: 1. Romance 1. Science Fiction 1. Mystery 1. Thriller 1. Fantasy 1. Young Adult 1. Historical Fiction 1. Horror 1. Non-Fiction 1. Biography 1. Children's Literature 1. Literary Fiction 1. Adventure 1. Crime 1. Dystopian 1. Paranormal 1. Graphic Novels 1. Poetry 1. Self-Help 1. Memoir Top 20 Authors: 1. William Shakespeare 1. Stephen King 1. J.K. Rowling 1. Jane Austen 1. George R.R. Martin 1. Agatha Christie 1. J.R.R. Tolkien 1. Charles Dickens 1. Ernest Hemingway 1. Fyodor Dostoevsky 1. Mark Twain 1. Leo Tolstoy 1. Virginia Woolf 1. Gabriel García Márquez 1. Franz Kafka 1. James Joyce 1. Haruki Murakami 1. Toni Morrison 1. Emily Brontë 1. Oscar Wilde Intersection between top 20 genres and top 20 authors based on their primary writing genres: 1. Romance - Jane Austen 1. Science Fiction - None 1. Mystery - Agatha Christie 1. Thriller - Stephen King 1. Fantasy - J.R.R. Tolkien, George R.R. Martin, J.K. Rowling 1. Young Adult - J.K. Rowling 1. Historical Fiction - None 1. Horror - Stephen King 1. Non-Fiction - None 1. Biography - None 1. Children's Literature - J.K. Rowling 1. Literary Fiction - Ernest Hemingway, Fyodor Dostoevsky, Leo Tolstoy, Virginia Woolf, Gabriel García Márquez, Franz Kafka, James Joyce, Haruki Murakami, Toni Morrison, Emily Brontë, Oscar Wilde 1. Adventure - Mark Twain 1. Crime - None 1. Dystopian - None 1. Paranormal - None 1. Graphic Novels - None 1. Poetry - William Shakespeare 1. Self-Help - None 1. Memoir - None Please note that some authors are associated with multiple genres, and the primary writing genre might vary based on individual interpretation.

Something like this will produce much more predictable results:

# Book Recommender

As an expert in literature and pop culture, use your existing knowledge to help interested readers discover authors they might like based on genre.

log(top10Genres)
log(top10Authors)

categorize the top 10 authors by the top 10 genres from the previous list

Which produces:

Based on my knowledge, here are the top 10 genres and top 10 authors (as of September 2021). Keep in mind that these are subjective lists and there might be some variations depending on personal taste. Top 10 Genres: Science Fiction Fantasy Mystery Thriller Romance Horror Historical Fiction Young Adult Literary Fiction Non-Fiction Top 10 Authors: Neil Gaiman Stephen King Agatha Christie J.K. Rowling George R.R. Martin Jane Austen Margaret Atwood Haruki Murakami Brandon Sanderson Yuval Noah Harari Categorized by genres: Science Fiction: Margaret Atwood (The Handmaid's Tale) Neil Gaiman (American Gods, though it also fits in Fantasy) Fantasy: J.K. Rowling (Harry Potter series) George R.R. Martin (A Song of Ice and Fire series) Neil Gaiman (Neverwhere, Stardust) Brandon Sanderson (Mistborn series, The Stormlight Archive series) Mystery: Agatha Christie (Hercule Poirot series, Miss Marple series) Thriller: Stephen King (Under the Dome, The Institute) Romance: Jane Austen (Pride and Prejudice, Sense and Sensibility) Horror: Stephen King (It, The Shining) Historical Fiction: George R.R. Martin (A Song of Ice and Fire series, though it is primarily Fantasy) Young Adult: J.K. Rowling (Harry Potter series) Neil Gaiman (Coraline) Literary Fiction: Haruki Murakami (Kafka on the Shore, Norwegian Wood) Margaret Atwood (The Blind Assassin, Alias Grace) Non-Fiction: Yuval Noah Harari (Sapiens, Homo Deus) Please note that some authors write in multiple genres, so their works may fit into more than one category.
mikkel-arturo commented 1 year ago

Thanks for the response. I see your point, I guess part of the art of designing a spec is how explicit to make it. There could be a cookbook that shows all sorts of possibilities with SudoLang if you don't want to include various operators.

I actually asked GPT to optimize the spec while retaining functionality and it created something over 90% smaller and seems to work perfectly.

And yes point taken about the "magic" of the operator. The way I'm using it is either with an explicit variable and/or a comment on the line giving a brief direction. I like that style because it fits well keeping a compact representation that feels more like code rather than using extended natural language instruction.

I have a whole career of use cases to apply semantic processing in a revolutionary and very real world impactful way. I'm going to experiment with some higher-order processing and SudoLang should be great to express it.

ericelliott commented 1 year ago

I actually asked GPT to optimize the spec while retaining functionality and it created something over 90% smaller and seems to work perfectly.

The language is designed to work well (there's no such thing as "perfect" with GPT models) without any spec prompting at all. If you're using preambles like this, it should be able to handle any SudoLang you throw at it:

# Book Recommender As an expert in literature and pop culture, use your existing knowledge to help interested readers discover authors they might like based on genre.

You generally should only need the spec if you want to run the linter.

The preamble does not need to be long or detailed. All it does is give the AI context to know what you want it to do with the code. Without it, it can get confused and think you want it to explain or transpile the code to Javascript or Python instead of treating it like instructions for the AI to follow. See the examples folder for preamble examples.