1KAV is a statically typed, purely functional systems language making use of linear types, and dependent types.
2
3
4// line comment
5/* block comment */
6/% doc comment %/
7
8list.kav
9--------
10
11/* doc comments are only allowed above pub functions */
12/% a linearly typed singly linked list of either linear or non linear elements, of length n %/
13pub linear datatype List[a: type, n: nat] {
14 | Nil : List[a, 0]
15 | [m: nat] Cons(a, List[a, m]) : List[a, m+1]
16}
17
18/* you could actually do this much easier by just doing List[a, n + 1] , but example for examples sake */
19/% a linearly typed singly linked list of either linear or non linear elements, of length n where n > 0 %/
20pub linear datatype List1[a: type, n: nat | n > 0] { // prob will have a pos sort as shorthand for [n: nat | n > 0 ], so just [n: pos]
21 | One(a) : List1[a, 0]
22 | [m: nat] More(a, List1[a, m]) : List1[a, m+1]
23}
24
25
26/% free a linearly typed singly linked list of non-linear elements %/
27pub fn list_free[a: utype, n: nat](list: List[a, n]) -> one {
28 case list of {
29 /* ~ consumes the list wrapper */
30 | ~Nil => ()
31 | ~Cons(_, rest) => list_free(rest)
32 }
33}
34
35
36main.kav
37--------
38
39/* import pub functions from a local file. */
40use "list.kav"
41/* you could also do 'use "list.kav" as list' to namespace it*/
42/* std library search: 'use <std.kav>' */
43
44/* i think it will be optional to use all the sorts. like you could just remove n and m from here and it will still be allowed.
45 * but in a case where you need to be presice, you can have them. */
46fn reverse[a: type,n: nat, m: nat](l: List[a, n], acc: List[a, m]) -> List[a, n + m] {
47 case l of {
48 | ~Nil => acc
49 | ~Cons(entry, rest) => reverse(rest, Cons(entry, acc))
50 }
51}
52
53/* one is similar to void in that it produces no result but it's not called void because it can't have side effects on any mutable
54 * global state, its more like a bool except with only one option */
55/% main has to be public so the language runtime can reach it. %/
56pub fn main() -> one {
57 /* we dont care about restricting the list length here so hopefully the checker will fill it for us */
58 /* we could even drop the type declaration entirely and it would recognise something constucted of 3 cons with ints
59 * is List[int, 3] */
60 val list: List[int, _] = Cons(1, Cons(2, Cons(3, Nil)))
61 val reversed = reverse(list, Nil) /* again, we dont need to declare the type of reversed, it will be inferred from reverse's sig*/
62 list_free(reversed)
63}
64
65other langauge features
66-----------------------
67
68/% a type alias for int. int's and numbers are interchangeable. a function that takes one as an argument can also take the other. %/
69pub typedef number = int
70
71/% a nominal type. int and id have the same internal representation, an id is at it's core an int, but
72 % functions that take id's cannot take ints and vice versa. %/
73pub newtype id = int
74/* constuctor to make an id of int 42 */
75/* val u = id(42) */
76/* unwrap
77 case u of {
78 | id(n) => // do something
79 }
80*/
81
82/* guard syntax */
83fn howbigisx(x: int) -> int {
84 guard {
85 | x == 0 => 0
86 | x < 5 => 5
87 | _ => 100 /* else */
88 }
89}
90
91/* comptime execution of a pure function with a value known at compile time */
92fn factorial(n: int) -> int {
93 if n <= 1 then 1
94 else n * factorial(n - 1)
95}
96
97fn main() -> int {
98 comptime factorial(5)
99}
100
101/* structs */
102/% a utype struct of utype elements %/
103pub struct Point {
104 x: int,
105 y: int
106}
107
108fn right(p: Point) -> Point {
109 p with { x = p.x + 1 } // construct a new Point based on the one taken as an argument and return it.
110}
111
112pub fn main() -> one {
113 val p: Point = Point { x = 5, y = 10 }
114 val q = right(p)
115 ()
116}
117
118
119cli commands
120------------
121
122kav build main.kav | -> lexes -> parses -> resolve imported funcs -> AST -> type check -> QBE -> native exe
123kav doc list.kav | ->
124
125 pub linear datatype List[a: type, n: nat]
126 a linearly typed singly linked list of either linear or non linear elements, of length n
127
128
129 pub fn list_free[a: utype, n: nat](list: List[a, n]) -> one
130 free a linearly typed singly linked list of non-linear elements, of length n
131