Skip to content

Commit 3ea7eb2

Browse files
committed
stuff
1 parent 5735934 commit 3ea7eb2

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

KIND2_GUIDE_AI.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Since Kind2's core is so simple, it comes with many syntax sugars.
3939
Every .kind2 file must define ONE top-level function:
4040

4141
```
42-
name <p0: P0> <p1: P1> ...
42+
func <p0: P0> <p1: P1> ...
4343
- arg0: typ0
4444
- arg1: typ1
4545
- ...
@@ -74,7 +74,7 @@ Where:
7474
Top-Level datatypes desugar to λ-encodings. The λ-encoded constructors must be
7575
created manually, in separate files. See examples below.
7676

77-
## Names, Paths and Use Aliases
77+
### Names, Paths and Use Aliases
7878

7979
Kind2 doesn't need imports. Every file defines a single top-level definition,
8080
which can be addressed from any other file via its full path. Example:
@@ -145,13 +145,40 @@ switch x = expr {
145145
}: motive
146146
```
147147

148+
### Note on Parameters and Metavars
149+
150+
Top-level definitions can have N parameters, or erased arguments. Example:
151+
152+
```
153+
// Pair/swap.kind2
154+
swap <A> <B>
155+
- pair: (Pair A B)
156+
...
157+
```
158+
159+
There are two ways to call these functions.
160+
161+
1. Filling the parameters explicitly:
162+
163+
```
164+
(swap Nat (List Nat) (Pair/new Nat (List Nat) Nat/zero (List/nil Nat)))
165+
```
166+
167+
2. Using metavars (`_`) to fill the parameters:
168+
169+
```
170+
(swap _ _ (Pair/new _ _ Nat/zero (List/nil _)))
171+
172+
As you can see, using metavars is much more concise. As a rule of thumb, always
173+
use metavars on the function body, but write it fully on its arglist. Remember
174+
to always count the arguments: you need one metavar (`_`) per parameter (`<>`).
175+
148176
### Other Sugars
149177
150178
- Lists: `[a, b, c]` (desugars to cons/nil)
151179
- Strings: `"foo"` (desugars to lists of u48 codepoints)
152180
- Equality: `{a = b}` (desugars to `(Equal _ a b)`)
153181
- Function: `A -> B` (desugars to `∀(x_: A) B`)
154-
- Implicit: `(foo/ x y)` (desugars to `(foo _ _ ... x y)`, auto-filling erased args)
155182
- Comments: `// comment here`
156183
157184
## Kind2 Examples
@@ -416,7 +443,7 @@ List/fold <A> (P: *)
416443
: P
417444

418445
match xs {
419-
cons: (c xs.head (List/fold/ P c n xs.tail))
446+
cons: (c xs.head (List/fold _ P c n xs.tail))
420447
nil: n
421448
}
422449
```
@@ -500,6 +527,10 @@ Take a moment to notice how EVERY constructor and function application in Kind2
500527
requires a parenthesis, just like Lisp. Trying to write `(foo x y)` without the
501528
surrounding parenthesis is a syntax error. Confirm this is true and move on.
502529
530+
Also, note that ERASED arguments can usually be inferred, so, a metavar ('_')
531+
can be used. Example: '(func _ _ ... arg0 arg1 ...)' can be writen instead of
532+
'(func A B ... arg0 arg1 ...)'. Prefer metavars on the body of the function.
533+
503534
### Sigma/_.kind2
504535
505536
```

kindcoder.mjs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,15 @@ Optionally, you can also include a SHORT, 1-paragraph <JUSTIFICATION/>.
167167
- map
168168
- fold
169169
- filter
170+
- equal
170171
- zip
171172
- length
172173
- Nat/
173174
- match
174175
- fold
175176
- succ
176177
- zero
178+
- compare
177179
- add
178180
- sub
179181
- mul
@@ -200,7 +202,8 @@ Optionally, you can also include a SHORT, 1-paragraph <JUSTIFICATION/>.
200202
Nat/equal is likely to be a pairwise comparison between Nats. As such, it must
201203
include Nat (obviously), as well as its constructor and match. It returns a
202204
Bool, so, it must also include its constructors and match. For completion, I've
203-
also added boolean AND and OR, since these are commonly used in comparison.
205+
also added bool AND and OR, since these are often used in comparison. Finally,
206+
Nat/compare and List/equal might be similar algorithms, so, I included them.
204207
</JUSTIFICATION>
205208
<DEPENDENCIES>
206209
Nat
@@ -213,18 +216,23 @@ Bool/false
213216
Bool/match
214217
Bool/and
215218
Bool/or
219+
Nat/compare
220+
List/equal
216221
</DEPENDENCIES>
217222
218223
# HINTS
219224
220225
- Attempt to include ALL files that might be relevant, directly or not.
221226
227+
- Always include files that might be similar algorithms to the current one.
228+
Example: 'Map/set' MUST include 'Mat/get'
229+
222230
- If the file is the constructor of an ADT, then, INCLUDE its type.
223231
Example: 'List/cons' MUST include 'List'
224232
225233
- When in doubt, prefer to include MORE, rather than LESS, potencial dependencies.
226234
227-
- Try to include AT LEAST 8 dependencies, and AT MOST 32.
235+
- Try to include AT LEAST 4 dependencies, and AT MOST (only if needed) 16.
228236
229237
- Sometimes the user will give hints in the file. Follow them.
230238
`.trim();
@@ -244,7 +252,7 @@ async function predictDependencies(name, fileContent) {
244252
}
245253
return null;
246254
}));
247-
return files.filter(file => file !== null);
255+
return files.filter(file => file !== null).map(file => ({...file, name: file.name.replace(/\/_$/, '')}));
248256
}
249257

250258
// Function to build a tree structure from files
@@ -261,7 +269,7 @@ async function predictDependencies(name, fileContent) {
261269
return result;
262270
}
263271

264-
const allFiles = await getAllKind2Files(path.join(path.dirname(name), '..', '..', 'book'));
272+
const allFiles = await getAllKind2Files("./");
265273
const defsTree = buildTree(allFiles);
266274

267275
const aiInput = [
@@ -274,7 +282,7 @@ async function predictDependencies(name, fileContent) {
274282
'</DEFINITIONS>'
275283
].join('\n').trim();
276284

277-
const aiOutput = await chat("h")(aiInput, { system: system_DepsPredictor, model: "h" });
285+
const aiOutput = await chat("s")(aiInput, { system: system_DepsPredictor, model: "s" });
278286
console.log("");
279287

280288
const dependenciesMatch = aiOutput.match(/<DEPENDENCIES>([\s\S]*)<\/DEPENDENCIES>/);
@@ -302,7 +310,7 @@ async function typeCheck(file) {
302310
async function main() {
303311
// Check for correct usage and parse command-line arguments
304312
if (process.argv.length < 3) {
305-
console.log("Usage: kind-refactor <file> <request> [<model>]");
313+
console.log("Usage: kindcoder <file> <request> [<model>]");
306314
process.exit(1);
307315
}
308316

@@ -315,7 +323,12 @@ async function main() {
315323

316324
// Get directory and file information
317325
let dir = path.dirname(file);
318-
let fileContent = await fs.readFile(file, 'utf-8');
326+
let fileContent;
327+
try {
328+
fileContent = await fs.readFile(file, 'utf-8');
329+
} catch (e) {
330+
fileContent = "";
331+
}
319332
let dirContent = await fs.readdir(dir);
320333

321334
// If the request is empty, replace it by a default request.

0 commit comments

Comments
 (0)