Skip to content

Commit

Permalink
Add examples of planned features (#401)
Browse files Browse the repository at this point in the history
* Add examples for planned features

* Make example syntax more consistent

* Add gadts examples with functional dependencies

* Make example syntax more consistent

* Chmod examples for the interpreter

* Add shebangs

* Add instances examples
  • Loading branch information
Cypher1 authored Apr 21, 2024
1 parent ce6526c commit a8bcc97
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 7 deletions.
27 changes: 27 additions & 0 deletions examples/contexts.tk
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env tako

test() = {
allocator = import("allocator")
ptr = allocator.allocate(sizeof(Char[100]))
if (ptr == nullptr) {
print("Success")
exit(0)
} else {
print("Failure")
}
}

main() = {
allocator = import("allocator")
logger = import("logger")

test_logger = logger.new_logger()
with {
allocator.allocate = allocator.report_oom,
system.out = test_logger,
system.error = test_logger,
run = test
}

print(test_logger)
}
27 changes: 27 additions & 0 deletions examples/enums.tk
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env tako

boolean = Enum.new(
true=1,
false=0,
)

boolean.import_literals()

color = Enum
.based_on(
byte[3]
)
.open(
red=#ff0000,
green=#00ff00,
blue=#0000ff,
)

color.add_values(
purple=#ff00ff,
cyan=#00ffff,
)

main() = {
print("Color {red} is red")
}
4 changes: 3 additions & 1 deletion examples/fib.tk
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
fib<T: Nat>(n: T): T=if(n<=1, 1, fib(n-1)+fib(n-2));
#!/usr/bin/env tako

fib(n: T, {T: Nat}): T=if(n<=1, 1, fib(n-1)+fib(n-2))

print(fib(80))
5 changes: 0 additions & 5 deletions examples/fibAcc.tk

This file was deleted.

7 changes: 7 additions & 0 deletions examples/fib_acc.tk
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env tako

fib_helper(n: T, a: T, b: T, {T: Nat}): T=if(n==0, a, fib_helper(n-1, b, a+b))

fib(n: T, {T: Nat}): T=fib_helper(n, 0, 1)

print(fib(80))
35 changes: 35 additions & 0 deletions examples/generic_abstract_data_types.tk
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env tako

// A simple, untyped implementation of S expressions.
SExpr: Type = struct.new(
func: (args: Value[n]),
args: SExpr[n],
{n: Int},
)

// A well typed, but little, language using GADTs.
Expr(
SubExpr(R: AllowedType): Reference(Expr(SubExpr, R))=Expr,
R: AllowedType,
with {AllowedType = Integer|Boolean}
) = gadt.new(

// Control flow:
if(
cond: SubExpr(R=Boolean),
then: SubExpr(R=T),
else: SubExpr(R=T),
{T: AllowedType}
): Expr(SubExpr, R=T),

// Binary operations:
or (left: SubExpr(R=Boolean), right: SubExpr(R=Boolean)): Expr(SubExpr, R=Boolean),
and (left: SubExpr(R=Boolean), right: SubExpr(R=Boolean)): Expr(SubExpr, R=Boolean),
add (left: SubExpr(R=Integer), right: SubExpr(R=Integer)): Expr(SubExpr, R=Integer),
mul (left: SubExpr(R=Integer), right: SubExpr(R=Integer)): Expr(SubExpr, R=Integer),
eq (left: SubExpr(R=Integer), right: SubExpr(R=Integer)): Expr(SubExpr, R=Boolean),

// Constants:
integer(value: Integer): Expr(SubExpr, R=Integer),
boolean(value: Boolean): Expr(SubExpr, R=Boolean),
)
44 changes: 44 additions & 0 deletions examples/instances.tk
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Bounded = Enum
.open()
.based_on({
min: (T: Type): T,
max: (T: Type): T,
})

Bounded.add_values(
boolean = {
min(T)=false,
max(T)=true,
}
integer = {
min(T)=0,
max(T)=2**64-1,
}
)

Showable = Enum.based_on({
to_string: (t: Type): String
}).open()

print(t: Type, {end: String="\n"}): String = system.out.write(t.to_string()+end)

to_base(value: integer, {base: integer=10}) = {
out: Char[math.log(Integer.max, base)]="0";
i: integer = 0;
while(
cond = t > 0,
loop = {
out[-i] = value%10,
value=value/10,
i=i+1
},
result = out[-i..],
)
}

Showable.add_values(
char(t: Char)=String.from_chars([t]),
chars(t: Char[n], {n: Integer})=String.from_chars(t),
string(t: String)=t,
integer(t: Integer)=t.to_base(base=10)
)
File renamed without changes.
2 changes: 2 additions & 0 deletions examples/vector_transpose.tk
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env tako

main = {
x3by2 = Vector.new([
[ 0, 1 ],
Expand Down
4 changes: 3 additions & 1 deletion examples/vector_transpose_failing.tk
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
main = {
#!/usr/bin/env tako

main() = {
x3by2 = Vector.new([
[ 0, 1 ],
[ 2, 3 ],
Expand Down

0 comments on commit a8bcc97

Please sign in to comment.