mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-05 22:48:42 +00:00
213 lines
3.4 KiB
Text
213 lines
3.4 KiB
Text
basic (Float : #Ord #Div)
|
|
basic (Int : #Ord #IDiv)
|
|
basic (String : #Ord)
|
|
basic (Char : #Ord #Enum)
|
|
basic (Bool : #Ord)
|
|
basic Unit
|
|
|
|
//
|
|
|
|
decl not : Bool -> Bool
|
|
def not : x =
|
|
(match x with
|
|
| true -> false
|
|
| false -> true)
|
|
|
|
decl ( && ) : Bool -> Bool -> Bool
|
|
def ( && ) : x y =
|
|
match x with
|
|
| true -> (
|
|
match y with
|
|
| true -> true
|
|
| false -> false
|
|
)
|
|
| false -> false
|
|
|
|
decl ( || ) : Bool -> Bool -> Bool
|
|
def ( || ) : x y =
|
|
match x with
|
|
| true -> true
|
|
| false -> (
|
|
match y with
|
|
| true -> true
|
|
| false -> false
|
|
)
|
|
|
|
//
|
|
|
|
typeclass Move =
|
|
& var ( <- ) : Move -> Unit // TODO
|
|
|
|
typeclass Copy =
|
|
& var ( = ) : Copy -> Unit
|
|
|
|
//
|
|
|
|
typeclass (Sum : #Copy) =
|
|
& var ( += ) : Sum -> Unit
|
|
& var ( -= ) : Sum -> Unit
|
|
& var ( + ) : Sum -> Sum
|
|
& var ( - ) : Sum -> Sum
|
|
& zero : -> Sum
|
|
|
|
namespace var Sum {
|
|
def ( + ) : x = {
|
|
var ans = self
|
|
; ans += x
|
|
return ans
|
|
}
|
|
|
|
def ( - ) : x = {
|
|
var ans = self
|
|
; ans -= x
|
|
return ans
|
|
}
|
|
}
|
|
|
|
typeclass (Mult : #Sum) =
|
|
& var ( *= ) : Mult -> Unit
|
|
& var ( * ) : Mult -> Mult
|
|
|
|
namespace var Mult {
|
|
def ( * ) : x = {
|
|
var ans = self
|
|
; ans *= x
|
|
return ans
|
|
}
|
|
}
|
|
|
|
typeclass (IDiv : #Mult) =
|
|
& var div : IDiv -> Unit
|
|
& var mod : IDiv -> Unit
|
|
|
|
namespace var IDiv {
|
|
def mod : x = self -. x * self.div: x
|
|
}
|
|
|
|
typeclass (Div : #Mult) =
|
|
& var ( /= ) : Div -> Unit
|
|
& var ( / ) : Div -> Div
|
|
|
|
namespace var Div {
|
|
def ( / ) : x = {
|
|
var ans = self
|
|
; ans /= x
|
|
return ans
|
|
}
|
|
}
|
|
|
|
//
|
|
|
|
typeclass Eq =
|
|
& var ( == ) : Eq -> Bool
|
|
& var ( != ) : Eq -> Bool
|
|
|
|
namespace var Eq {
|
|
def ( != ) : x = not: (self == x)
|
|
}
|
|
|
|
//
|
|
|
|
struct Order =
|
|
| EQ
|
|
| LT
|
|
| GT
|
|
|
|
typeclass (Ord : #Eq) =
|
|
& var compare : Ord -> Order
|
|
& var ( < ) : Ord -> Bool
|
|
& var ( >= ) : Ord -> Bool
|
|
& var ( > ) : Ord -> Bool
|
|
& var ( <= ) : Ord -> Bool
|
|
|
|
decl min ('A : #Ord) : 'A -> 'A -> 'A
|
|
def min : x y = if x < y then x else y
|
|
|
|
decl max ('A : #Ord) : 'A -> 'A -> 'A
|
|
def max : x y = if x < y then y else x
|
|
|
|
namespace var Ord {
|
|
def compare : x =
|
|
if self == x then $EQ
|
|
elif self < x then $LT
|
|
else $GT
|
|
|
|
def ( >= ) : x = not: (self < x)
|
|
def ( > ) : x = x < self
|
|
def ( <= ) : x = not: (x < self)
|
|
}
|
|
|
|
//
|
|
|
|
typeclass Show =
|
|
& var show : -> String
|
|
|
|
typeclass Read =
|
|
& var read : String -> Read
|
|
|
|
typeclass DebugShow =
|
|
& debug_show : -> String
|
|
|
|
//
|
|
|
|
typeclass Default =
|
|
& default : -> Default
|
|
|
|
//
|
|
|
|
typeclass Bounded =
|
|
& min_bound : -> Bounded
|
|
& max_bound : -> Bounded
|
|
& var is_max_bound : -> Bool
|
|
& var is_min_bound : -> Bool
|
|
|
|
//
|
|
|
|
typeclass Enum =
|
|
& var succ : -> (Optional Enum)
|
|
& var pred : -> (Optional Enum)
|
|
& to_enum : Int -> Enum
|
|
& var from_enum : -> Int
|
|
|
|
//
|
|
|
|
// // bad
|
|
// typeclass Functor 'A =
|
|
// & fmap 'B ('F : (#Functor 'B)) : ('A -> 'B) -> Functor -> 'F
|
|
|
|
// typeclass (Iterator : #Eq) =
|
|
// & next : -> Unit
|
|
// & prev : -> Unit
|
|
//
|
|
// typeclass Iterable ('Iter : #Iterable) =
|
|
// & begin : -> 'Iter
|
|
// & end : -> 'Iter
|
|
|
|
//
|
|
|
|
decl ( -- ) : Int -> Int -> Int_0
|
|
def ( -- ) : begin end = {
|
|
var current = begin
|
|
return (while current < end do {
|
|
; current += 1
|
|
return current - 1
|
|
})
|
|
}
|
|
|
|
decl print : String -> Unit
|
|
decl scan : -> String
|
|
|
|
decl func : String -> Int
|
|
def func : s = {
|
|
; print: s
|
|
return 5
|
|
}
|
|
|
|
exec main {
|
|
for i in 0--9 do func: "abacaba"
|
|
; print: ({
|
|
if true then bring scan: else { ; print: "aaa" }
|
|
bring "nothing"
|
|
; print: "aaa"
|
|
})
|
|
}
|