Adding more types to my stdlib functions. Cleaning up type checker
This commit is contained in:
parent
850219cdec
commit
2b710602d3
2 changed files with 30 additions and 51 deletions
|
|
@ -60,12 +60,8 @@ entries : a \ List { key : String, value : b };
|
||||||
keys : a \ List String;
|
keys : a \ List String;
|
||||||
nth : Int \ a \ Maybe a;
|
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
|
# 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]
|
# nth = i list \ [i, list]
|
||||||
# | [_, []] \ None
|
# | [_, []] \ None
|
||||||
# | [0, [x, ...xs]] \ (Some x)
|
# | [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];
|
| [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 = [] };
|
init = { i = 0, result = [] };
|
||||||
f2 = acc x \ { i = acc.i + 1, result = [...acc.result, f x acc.i] };
|
f2 = acc x \ { i = acc.i + 1, result = [...acc.result, f x acc.i] };
|
||||||
(fold f2 init list).result;
|
(fold f2 init list).result;
|
||||||
|
|
@ -105,99 +101,84 @@ insertCharAt : String \ Int \ String \ String = text pos char \
|
||||||
after = slice text pos (len text);
|
after = slice text pos (len text);
|
||||||
before & char & after;
|
before & char & after;
|
||||||
|
|
||||||
deleteCharAt = text pos \
|
deleteCharAt : String \ Int \ String = text pos \
|
||||||
before = slice text 0 pos;
|
before = slice text 0 pos;
|
||||||
after = slice text (pos + 1) (len text);
|
after = slice text (pos + 1) (len text);
|
||||||
before & after;
|
before & after;
|
||||||
|
|
||||||
# updateAt
|
updateAt : Int \ (a \ a) \ List a \ List a = i f list \ mapWithIndex (x j \
|
||||||
updateAt = i f list \ mapWithIndex (x j \
|
|
||||||
(i == j | True \ f x | False \ x)
|
(i == j | True \ f x | False \ x)
|
||||||
) list;
|
) list;
|
||||||
|
|
||||||
# fold : (b \ a \ b) \ b \ List a \ b
|
fold : (b \ a \ b) \ b \ List a \ b = f acc list \ list
|
||||||
fold = f acc list \ list
|
|
||||||
| [] \ acc
|
| [] \ acc
|
||||||
| [x, ...xs] \ fold f (f acc x) xs;
|
| [x, ...xs] \ fold f (f acc x) xs;
|
||||||
|
|
||||||
# reverse : List a \ List a
|
reverse : List a \ List a = list \ list
|
||||||
reverse = list \ list
|
|
||||||
| [] \ []
|
| [] \ []
|
||||||
| [x, ...xs] \ (reverse xs) & [x];
|
| [x, ...xs] \ (reverse xs) & [x];
|
||||||
|
|
||||||
# take : Int \ List a \ List a
|
take : Int \ List a \ List a = n list \ { n = n, list = list }
|
||||||
take = n list \ { n = n, list = list }
|
|
||||||
| { n = _, list = [] } \ []
|
| { n = _, list = [] } \ []
|
||||||
| { n = 0, list = _ } \ []
|
| { n = 0, list = _ } \ []
|
||||||
| { n = _, list = [x, ...xs] } \ [x, ...take (n - 1) xs];
|
| { n = _, list = [x, ...xs] } \ [x, ...take (n - 1) xs];
|
||||||
|
|
||||||
# drop : Int \ List a \ List a
|
drop : Int \ List a \ List a = n list \ { n = n, list = list }
|
||||||
drop = n list \ { n = n, list = list }
|
|
||||||
| { n = _, list = [] } \ []
|
| { n = _, list = [] } \ []
|
||||||
| { n = 0, list = _ } \ list
|
| { n = 0, list = _ } \ list
|
||||||
| { n = _, list = [x, ...xs] } \ drop (n - 1) xs;
|
| { n = _, list = [x, ...xs] } \ drop (n - 1) xs;
|
||||||
|
|
||||||
# join : String \ List String \ String
|
join : String \ List String \ String = s list \ list
|
||||||
join = s list \ list
|
|
||||||
| [] \ ""
|
| [] \ ""
|
||||||
| [x] \ x
|
| [x] \ x
|
||||||
| [x, ...xs] \ (x & s & (join s xs));
|
| [x, ...xs] \ (x & s & (join s xs));
|
||||||
|
|
||||||
# repeatStr : Int \ String \ String
|
repeatStr : Int \ String \ String = n str \ n
|
||||||
repeatStr = n str \ n
|
|
||||||
| 0 \ ""
|
| 0 \ ""
|
||||||
| m \ str & (repeatStr (m - 1) str);
|
| m \ str & (repeatStr (m - 1) str);
|
||||||
|
|
||||||
# repeat : Int \ a \ List a
|
repeat : Int \ a \ List a = n a \ n
|
||||||
repeat = n a \ n
|
|
||||||
| 0 \ []
|
| 0 \ []
|
||||||
| m \ [a, ...repeat (m - 1) a];
|
| m \ [a, ...repeat (m - 1) a];
|
||||||
|
|
||||||
# zipWith : (a \ b \ c) \ List a \ List b \ List c
|
# zipWith
|
||||||
zipWith = f l1 l2 \ l1
|
zipWith : (a \ b \ c) \ List a \ List b \ List c = f l1 l2 \ l1
|
||||||
| [] \ []
|
| [] \ []
|
||||||
| [x, ...xs] \ (l2
|
| [x, ...xs] \ (l2
|
||||||
| [] \ []
|
| [] \ []
|
||||||
| [y, ...ys] \ [f x y, ...zipWith f xs ys]);
|
| [y, ...ys] \ [f x y, ...zipWith f xs ys]);
|
||||||
|
|
||||||
# contains : a \ List a \ Bool
|
contains : a \ List a \ Bool = x list \ list
|
||||||
contains = x list \ list
|
|
||||||
| [] \ False
|
| [] \ False
|
||||||
| [y, ...ys] \ (x == y
|
| [y, ...ys] \ (x == y
|
||||||
| True \ True
|
| True \ True
|
||||||
| False \ contains x ys);
|
| False \ contains x ys);
|
||||||
|
|
||||||
# find : (a \ Bool) \ List a \ Maybe a
|
find : (a \ Bool) \ List a \ Maybe a = f list \ list
|
||||||
find = f list \ list
|
|
||||||
| [] \ None
|
| [] \ None
|
||||||
| [x, ...xs] \ (f x
|
| [x, ...xs] \ (f x
|
||||||
| True \ Some x
|
| True \ Some x
|
||||||
| False \ find f xs);
|
| False \ find f xs);
|
||||||
|
|
||||||
# flatten : List (List a) \ List a
|
flatten : List (List a) \ List a = lists \ fold cat [] lists;
|
||||||
flatten = lists \ fold cat [] lists;
|
|
||||||
|
|
||||||
# and : Bool \ Bool \ Bool
|
and : Bool \ Bool \ Bool = x y \ x
|
||||||
and = x y \ x
|
|
||||||
| False \ False
|
| False \ False
|
||||||
| True \ y;
|
| True \ y;
|
||||||
|
|
||||||
# or : Bool \ Bool \ Bool
|
or : Bool \ Bool \ Bool = x y \ x
|
||||||
or = x y \ x
|
|
||||||
| True \ True
|
| True \ True
|
||||||
| False \ y;
|
| False \ y;
|
||||||
|
|
||||||
not : bool \ bool = x \ x
|
not : Bool \ Bool = x \ x
|
||||||
| True \ False
|
| True \ False
|
||||||
| False \ True;
|
| False \ True;
|
||||||
|
|
||||||
# isSome : Maybe a \ Bool
|
isSome : Maybe a \ Bool = a \ a
|
||||||
isSome = a \ a
|
|
||||||
| Some _ \ True
|
| Some _ \ True
|
||||||
| _ \ False;
|
| _ \ False;
|
||||||
|
|
||||||
# isNone : Maybe a \ Bool
|
isNone : Maybe a \ Bool = a \ a
|
||||||
isNone = a \ a
|
|
||||||
| Some _ \ False
|
| Some _ \ False
|
||||||
| _ \ True;
|
| _ \ True;
|
||||||
|
|
||||||
|
|
@ -205,23 +186,18 @@ unwrapOr : a \ Maybe a \ a = default x \ x
|
||||||
| Some val \ val
|
| Some val \ val
|
||||||
| _ \ default;
|
| _ \ default;
|
||||||
|
|
||||||
# if : Bool \ a \ a \ a
|
if : Bool \ a \ a \ a = test a b \ test
|
||||||
if = test a b \ test
|
|
||||||
| True \ a
|
| True \ a
|
||||||
| _ \ b;
|
| _ \ b;
|
||||||
|
|
||||||
# range : Int \ Int \ List Int
|
|
||||||
# [start, start+1, ..., end-1]
|
# [start, start+1, ..., end-1]
|
||||||
range = start end \ start >= end
|
range : Int \ Int \ List Int = start end \ start >= end
|
||||||
| True \ []
|
| True \ []
|
||||||
| False \ [start, ...range (start + 1) end];
|
| False \ [start, ...range (start + 1) end];
|
||||||
|
|
||||||
# sum : List Int \ Int
|
# TODO Number
|
||||||
# TODO sum floats too? wait for typeclasses?
|
sum : List Int \ Int = list \ fold add 0 list;
|
||||||
sum = list \ fold add 0 list;
|
|
||||||
|
|
||||||
# any : (a \ Bool) \ List a \ Bool
|
any : (a \ Bool) \ List a \ Bool = f list \ fold (acc x \ or acc (f x)) False list;
|
||||||
any = f list \ fold (acc x \ or acc (f x)) False list;
|
|
||||||
|
|
||||||
# all : (a \ Bool) \ List a \ Bool
|
all : (a \ Bool) \ List a \ Bool = f list \ fold (acc x \ and acc (f x)) True list;
|
||||||
all = f list \ fold (acc x \ and acc (f x)) True list;
|
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,9 @@ function unify(t1: TypeAST, t2: TypeAST, subst: Subst): string | null {
|
||||||
// Same type name
|
// Same type name
|
||||||
if (a.kind === 'type-name' && b.kind === 'type-name' && a.name === b.name) return null;
|
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
|
// Type var binds to anything
|
||||||
if (a.kind === 'type-var') {
|
if (a.kind === 'type-var') {
|
||||||
if (occursIn(a.name, b, subst)) return `Infinite type: ${a.name} occurs in ${prettyPrintType(b)}`;
|
if (occursIn(a.name, b, subst)) return `Infinite type: ${a.name} occurs in ${prettyPrintType(b)}`;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue