Adding more types to my stdlib functions. Cleaning up type checker

This commit is contained in:
Dustin Swan 2026-03-27 21:58:11 -06:00
parent 850219cdec
commit 2b710602d3
No known key found for this signature in database
GPG key ID: 30D46587E2100467
2 changed files with 30 additions and 51 deletions

View file

@ -60,12 +60,8 @@ entries : a \ List { key : String, value : b };
keys : a \ List String;
nth : Int \ a \ Maybe a;
# myTest = gt 3 "hi";
# myTest2 = cat [3, 4] [4, 5];
# myTest3 = debug! "myTest2" myTest2;
# nth : Int \ List a \ Maybe a
# in host at the moment, until we get typeclasses or something and this can work on strings too
# defined in host for now
# nth = i list \ [i, list]
# | [_, []] \ None
# | [0, [x, ...xs]] \ (Some x)
@ -75,7 +71,7 @@ map : (a \ b) \ List a \ List b = f list \ list
| [] \ []
| [x, ...xs] \ [f x, ...map f xs];
mapWithIndex : (a \ Number \ b) \ List a \ List b = f list \
mapWithIndex : (a \ Int \ b) \ List a \ List b = f list \
init = { i = 0, result = [] };
f2 = acc x \ { i = acc.i + 1, result = [...acc.result, f x acc.i] };
(fold f2 init list).result;
@ -105,99 +101,84 @@ insertCharAt : String \ Int \ String \ String = text pos char \
after = slice text pos (len text);
before & char & after;
deleteCharAt = text pos \
deleteCharAt : String \ Int \ String = text pos \
before = slice text 0 pos;
after = slice text (pos + 1) (len text);
before & after;
# updateAt
updateAt = i f list \ mapWithIndex (x j \
updateAt : Int \ (a \ a) \ List a \ List a = i f list \ mapWithIndex (x j \
(i == j | True \ f x | False \ x)
) list;
# fold : (b \ a \ b) \ b \ List a \ b
fold = f acc list \ list
fold : (b \ a \ b) \ b \ List a \ b = f acc list \ list
| [] \ acc
| [x, ...xs] \ fold f (f acc x) xs;
# reverse : List a \ List a
reverse = list \ list
reverse : List a \ List a = list \ list
| [] \ []
| [x, ...xs] \ (reverse xs) & [x];
# take : Int \ List a \ List a
take = n list \ { n = n, list = list }
take : Int \ List a \ List a = n list \ { n = n, list = list }
| { n = _, list = [] } \ []
| { n = 0, list = _ } \ []
| { n = _, list = [x, ...xs] } \ [x, ...take (n - 1) xs];
# drop : Int \ List a \ List a
drop = n list \ { n = n, list = list }
drop : Int \ List a \ List a = n list \ { n = n, list = list }
| { n = _, list = [] } \ []
| { n = 0, list = _ } \ list
| { n = _, list = [x, ...xs] } \ drop (n - 1) xs;
# join : String \ List String \ String
join = s list \ list
join : String \ List String \ String = s list \ list
| [] \ ""
| [x] \ x
| [x, ...xs] \ (x & s & (join s xs));
# repeatStr : Int \ String \ String
repeatStr = n str \ n
repeatStr : Int \ String \ String = n str \ n
| 0 \ ""
| m \ str & (repeatStr (m - 1) str);
# repeat : Int \ a \ List a
repeat = n a \ n
repeat : Int \ a \ List a = n a \ n
| 0 \ []
| m \ [a, ...repeat (m - 1) a];
# zipWith : (a \ b \ c) \ List a \ List b \ List c
zipWith = f l1 l2 \ l1
# zipWith
zipWith : (a \ b \ c) \ List a \ List b \ List c = f l1 l2 \ l1
| [] \ []
| [x, ...xs] \ (l2
| [] \ []
| [y, ...ys] \ [f x y, ...zipWith f xs ys]);
# contains : a \ List a \ Bool
contains = x list \ list
contains : a \ List a \ Bool = x list \ list
| [] \ False
| [y, ...ys] \ (x == y
| True \ True
| False \ contains x ys);
# find : (a \ Bool) \ List a \ Maybe a
find = f list \ list
find : (a \ Bool) \ List a \ Maybe a = f list \ list
| [] \ None
| [x, ...xs] \ (f x
| True \ Some x
| False \ find f xs);
# flatten : List (List a) \ List a
flatten = lists \ fold cat [] lists;
flatten : List (List a) \ List a = lists \ fold cat [] lists;
# and : Bool \ Bool \ Bool
and = x y \ x
and : Bool \ Bool \ Bool = x y \ x
| False \ False
| True \ y;
# or : Bool \ Bool \ Bool
or = x y \ x
or : Bool \ Bool \ Bool = x y \ x
| True \ True
| False \ y;
not : bool \ bool = x \ x
not : Bool \ Bool = x \ x
| True \ False
| False \ True;
# isSome : Maybe a \ Bool
isSome = a \ a
isSome : Maybe a \ Bool = a \ a
| Some _ \ True
| _ \ False;
# isNone : Maybe a \ Bool
isNone = a \ a
isNone : Maybe a \ Bool = a \ a
| Some _ \ False
| _ \ True;
@ -205,23 +186,18 @@ unwrapOr : a \ Maybe a \ a = default x \ x
| Some val \ val
| _ \ default;
# if : Bool \ a \ a \ a
if = test a b \ test
if : Bool \ a \ a \ a = test a b \ test
| True \ a
| _ \ b;
# range : Int \ Int \ List Int
# [start, start+1, ..., end-1]
range = start end \ start >= end
range : Int \ Int \ List Int = start end \ start >= end
| True \ []
| False \ [start, ...range (start + 1) end];
# sum : List Int \ Int
# TODO sum floats too? wait for typeclasses?
sum = list \ fold add 0 list;
# TODO Number
sum : List Int \ Int = list \ fold add 0 list;
# any : (a \ Bool) \ List a \ Bool
any = f list \ fold (acc x \ or acc (f x)) False list;
any : (a \ Bool) \ List a \ Bool = f list \ fold (acc x \ or acc (f x)) False list;
# all : (a \ Bool) \ List a \ Bool
all = f list \ fold (acc x \ and acc (f x)) True list;
all : (a \ Bool) \ List a \ Bool = f list \ fold (acc x \ and acc (f x)) True list;

View file

@ -32,6 +32,9 @@ function unify(t1: TypeAST, t2: TypeAST, subst: Subst): string | null {
// Same type name
if (a.kind === 'type-name' && b.kind === 'type-name' && a.name === b.name) return null;
// Same type var
if (a.kind === 'type-var' && b.kind === 'type-var' && a.name === b.name) return null;
// Type var binds to anything
if (a.kind === 'type-var') {
if (occursIn(a.name, b, subst)) return `Infinite type: ${a.name} occurs in ${prettyPrintType(b)}`;