lang_2023/tests/stdlib.lang

190 lines
3 KiB
Text
Raw Normal View History

2023-06-03 19:01:03 +03:00
basic \float[#ord #div #str]
basic \int[#ord #idiv #str]
basic \string[#ord #str #char-container #copy]
basic \char[#ord #str #copy]
basic \bool[#ord #str #copy]
basic \unit[#str #copy]
2023-05-23 11:54:15 +03:00
//
2023-06-03 19:01:03 +03:00
decl not : \bool -> \bool
2023-05-23 11:54:15 +03:00
def not : x =
(match x with
| true -> false
| false -> true)
2023-06-03 19:01:03 +03:00
decl ( && ) : \bool -> \bool -> \bool
2023-05-23 11:54:15 +03:00
def ( && ) : x y =
match x with
| true -> (
match y with
| true -> true
| false -> false
)
| false -> false
2023-06-03 19:01:03 +03:00
decl ( || ) : \bool -> \bool -> \bool
2023-05-23 11:54:15 +03:00
def ( || ) : x y =
match x with
| true -> true
| false -> (
match y with
| true -> true
| false -> false
)
//
2023-06-03 19:01:03 +03:00
typeclass \char-container =
& var size : -> \int
& var at : \int -> \char
2023-05-23 11:54:15 +03:00
//
2023-06-03 19:01:03 +03:00
typeclass \move = // TODO
& var ( <- ) : \move -> \unit
2023-05-23 11:54:15 +03:00
2023-06-03 19:01:03 +03:00
typeclass \copy =
& var ( = ) : \copy -> \unit
2023-05-23 11:54:15 +03:00
//
2023-06-03 19:01:03 +03:00
typeclass \sum[#copy] =
& var ( += ) : \sum -> \unit
& var ( -= ) : \sum -> \unit
& var ( + ) : \sum -> \sum
& var ( - ) : \sum -> \sum
& zero : -> \sum
2023-05-23 11:54:15 +03:00
2023-06-03 19:01:03 +03:00
namespace var \sum {
2023-05-23 11:54:15 +03:00
def ( + ) : x = {
var ans = self
; ans += x
return ans
}
def ( - ) : x = {
var ans = self
; ans -= x
return ans
}
}
2023-06-03 19:01:03 +03:00
typeclass \mult[#sum] =
& var ( *= ) : \mult -> \unit
& var ( * ) : \mult -> \mult
2023-05-23 11:54:15 +03:00
2023-06-03 19:01:03 +03:00
namespace var \mult {
2023-05-23 11:54:15 +03:00
def ( * ) : x = {
var ans = self
; ans *= x
return ans
}
}
2023-06-03 19:01:03 +03:00
typeclass \idiv[#mult] =
& var div : \idiv -> \idiv
& var mod : \idiv -> \idiv
2023-05-23 11:54:15 +03:00
2023-06-03 19:01:03 +03:00
namespace var \idiv {
def mod : x = self -. x * self..div: x
2023-05-23 11:54:15 +03:00
}
2023-06-03 19:01:03 +03:00
typeclass \div[#mult] =
& var ( /= ) : \div -> \unit
& var ( / ) : \div -> \div
2023-05-23 11:54:15 +03:00
2023-06-03 19:01:03 +03:00
namespace var \div {
2023-05-23 11:54:15 +03:00
def ( / ) : x = {
var ans = self
; ans /= x
return ans
}
}
//
2023-06-03 19:01:03 +03:00
typeclass \eq =
& var ( == ) : \eq -> \bool
& var ( != ) : \eq -> \bool
2023-05-23 11:54:15 +03:00
2023-06-03 19:01:03 +03:00
namespace var \eq {
2023-05-23 11:54:15 +03:00
def ( != ) : x = not: (self == x)
}
//
2023-06-03 19:01:03 +03:00
struct \order =
| $eq
| $lt
| $gt
2023-05-23 11:54:15 +03:00
2023-06-03 19:01:03 +03:00
typeclass \ord[#eq] =
& var compare : \ord -> \order
& var ( < ) : \ord -> \bool
& var ( >= ) : \ord -> \bool
& var ( > ) : \ord -> \bool
& var ( <= ) : \ord -> \bool
2023-05-23 11:54:15 +03:00
2023-06-03 19:01:03 +03:00
decl min 'a[#ord] : 'a -> 'a -> 'a
2023-05-23 11:54:15 +03:00
def min : x y = if x < y then x else y
2023-06-03 19:01:03 +03:00
decl max 'a[#ord] : 'a -> 'a -> 'a
2023-05-23 11:54:15 +03:00
def max : x y = if x < y then y else x
2023-06-03 19:01:03 +03:00
namespace var \ord {
2023-05-23 11:54:15 +03:00
def compare : x =
2023-06-03 19:01:03 +03:00
if self == x then $eq
elif self < x then $lt
else $gt
2023-05-23 11:54:15 +03:00
def ( >= ) : x = not: (self < x)
def ( > ) : x = x < self
def ( <= ) : x = not: (x < self)
}
//
2023-06-03 19:01:03 +03:00
typeclass \show =
& var show : -> \string
2023-05-23 11:54:15 +03:00
2023-06-03 19:01:03 +03:00
typeclass \read =
& read : \string -> \read
2023-05-23 11:54:15 +03:00
2023-06-03 19:01:03 +03:00
typeclass \str[#show #read]
2023-05-23 11:54:15 +03:00
2023-06-03 19:01:03 +03:00
// typeclass debug-show = // TODO
// & debugdshow : -> \string
2023-05-23 11:54:15 +03:00
//
2023-06-03 19:01:03 +03:00
typeclass \default =
& default : -> \default
2023-05-23 11:54:15 +03:00
//
2023-06-03 19:01:03 +03:00
typeclass \bounded =
& min-bound : -> \bounded
& max-bound : -> \bounded
& var is-max-bound : -> \bool
& var is-min-bound : -> \bool
2023-05-23 11:54:15 +03:00
//
2023-06-03 19:01:03 +03:00
typeclass \enum =
& var succ : -> \optional[enum]
& var pred : -> \optional[enum]
& to-enum : \int -> \enum
& var from-enum : -> \int
2023-05-23 11:54:15 +03:00
//
2023-06-03 19:01:03 +03:00
namespace io {
decl print : \string -> \unit
decl scan : -> \string
2023-05-23 11:54:15 +03:00
}
2023-06-03 19:01:03 +03:00
decl random : -> \int // TODO
2023-05-23 11:54:15 +03:00
//