r/ProgrammingLanguages • • 3h ago

Kiki Lang - query, markup, coding

I have been working on this for a while, and would like to share and get some feedback.

It started as a query language, then grows to add markup features, to be useful for building interactive data apps.

Below is a sample Kiki program. The idea of mixing markup and coding, is in the same category of LaTex or Typst, but Kiki focus more on data/query, and uses a different syntax design.

# Kiki Lang

Kiki is literal, meaning that the program is a prose. Lines start with "> " or inline \(12 + 34) are code blocks.

The two \(bold ~ core ideas) are:
- Lisp like syntax, which is nice across query, markup and coding.
- Algebric effect, which is used for errors, I/O, optionals, and state.

Here are the collections: list, record, and table.

> names = ["alice" "bob"]
> alice = {name = "alice" age = 20}
> users = |name  age|
           alice 20
           bob   30

"=>" is a form of inline unit test:

> type-of names => :[string]
> type-of alice => :{name = string age = int}
> type-of users => :|name = string age = int|

Query is an important part of the language:

> users |> filter age >= 18 
        |> select name

Here is a function with pattern matching:

> fib = fn 0 -> 1
        fn 1 -> 1 
        fn n -> fib (n - 1) + fib (n - 2)

And it can be called inline as \(fib 10).
6 Upvotes

13 comments sorted by

3

u/NomagnoIsNotDead 3h ago

I love this, thanks for sharing!
However, how is the syntax 'Lisp-like'? Lisp syntax is characterized, first and foremost, by being extremely uniform. In this short demo you have shown a value sub-language, a function pattern matching sub language, a stream sub-language, a type definition sub-language, and a unit testing sub-language, all with different rules. Which isn't bad at all, just not lispy IMO :P

2

u/terryio 2h ago

Fair point. I meant to say the function calling convention is lisp like, function name first and argument followed by space. Like:

fib 10
select name
bold ~ hi there // where "~" turns the rest text to be prose rather than code

Kiki is statically typed, so in many area as you pointed out, not very lisp like.

2

u/NomagnoIsNotDead 2h ago

Oh! It's statically typed? Interesting!

What do the unit tests with the type-of represent, then? I thought it was testing that the type of the variable is what is expected

2

u/terryio 2h ago

It is indeed testing the type of the variable. Like some functional language, types are first class values, so :int, :[string], etc are normal values. type-of is a special form that evaluate to a type. So this is true too:

> type-of :int => :type

1

u/NomagnoIsNotDead 2h ago

:O

So
> type-of var => :int

Is basically a static assertion/does not need any computation, whereas

> var => 3.5

Actually requires the code to be ran?

1

u/NomagnoIsNotDead 2h ago

This language idea is so cute, it'd be so good to combine with a static site generator to knit some cool stuff effortlessly

1

u/terryio 2h ago

All "=>" test needs to be evaluated (i.e, ran, as in runtime). Static type check can only tell that:
- "type-of var" is a type value, so is ":int".
- "var" is a int value, so is "3.5"

So the test is allowed to run.

2

u/titaniumalt 3h ago
> users = |name  age|
           alice 20
           bob   30

confusing table syntax

i love the simplicity and functional design, but it feels a bit redundant to have both tables and records

3

u/terryio 3h ago

Well table is the most important part of kiki since it is to handle spreadsheet like data. Record is less important and only useful for writing certain functions. That syntax is layout (indent) aware. Any better idea for table syntax?

3

u/sphen_lee 2h ago

A table is logically equivalent to a list of records with the same fields? I assume you can store it more efficiently (maybe columnar?)

In terms of syntax, it probably needs separators between columns. Like || for the column names and then | between each column. Otherwise it could get hard to read if the values are not literals.

(It's also nice to include a non-indentation variant. Like JSON? Some people prefer it, but it's also easier for code generators)

2

u/terryio 2h ago

> A table is logically equivalent to a list of records with the same fields? I assume you can store it more efficiently (maybe columnar?)

Both are yes. The compiler uses some columnar encoding in memory.

> needs separators between columns

Fair point. My plan was to rely on editor and/or formatter to keep the table aligned nice. Though adding an optional column/field divider could be useful I think.

1

u/evincarofautumn 2h ago

Would you mind elaborating on what you mean by “confusing”? Are you saying it’s hard to tell what this code does, or easy to misread it as doing something different than it actually does, or something else?

1

u/titaniumalt 2h ago

Like, i can't see how the table is parsed (why do the pipes only surround the first element) and for longer tables you might get confused if you can't see the first line of definition.