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-13 14:44:17 +03:00
|
|
|
|
2023-05-13 16:14:02 +03:00
|
|
|
//
|
2023-05-13 14:44:17 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
decl not : \bool -> \bool
|
2023-05-13 14:44:17 +03:00
|
|
|
def not : x =
|
2023-05-13 18:53:54 +03:00
|
|
|
(match x with
|
|
|
|
|
| true -> false
|
|
|
|
|
| false -> true)
|
2023-05-13 14:44:17 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
decl ( && ) : \bool -> \bool -> \bool
|
2023-05-13 14:44:17 +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-13 14:44:17 +03:00
|
|
|
def ( || ) : x y =
|
|
|
|
|
match x with
|
|
|
|
|
| true -> true
|
|
|
|
|
| false -> (
|
|
|
|
|
match y with
|
|
|
|
|
| true -> true
|
|
|
|
|
| false -> false
|
|
|
|
|
)
|
|
|
|
|
|
2023-05-21 17:00:59 +03:00
|
|
|
//
|
|
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
typeclass \char-container =
|
|
|
|
|
& var size : -> \int
|
|
|
|
|
& var at : \int -> \char
|
2023-05-22 16:03:50 +03:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
typeclass \move = // TODO
|
|
|
|
|
& var ( <- ) : \move -> \unit
|
2023-05-21 17:00:59 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
typeclass \copy =
|
|
|
|
|
& var ( = ) : \copy -> \unit
|
2023-05-21 17:00:59 +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-21 17:00:59 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
namespace var \sum {
|
2023-05-21 17:00:59 +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-21 17:00:59 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
namespace var \mult {
|
2023-05-21 17:00:59 +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-21 17:00:59 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
namespace var \idiv {
|
|
|
|
|
def mod : x = self -. x * self..div: x
|
2023-05-21 17:00:59 +03:00
|
|
|
}
|
|
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
typeclass \div[#mult] =
|
|
|
|
|
& var ( /= ) : \div -> \unit
|
|
|
|
|
& var ( / ) : \div -> \div
|
2023-05-21 17:00:59 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
namespace var \div {
|
2023-05-21 17:00:59 +03:00
|
|
|
def ( / ) : x = {
|
|
|
|
|
var ans = self
|
|
|
|
|
; ans /= x
|
|
|
|
|
return ans
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2023-05-13 14:44:17 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
typeclass \eq =
|
|
|
|
|
& var ( == ) : \eq -> \bool
|
2023-06-08 16:42:35 +03:00
|
|
|
& var ( != ) : \eq -> \bool
|
2023-05-13 14:44:17 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
namespace var \eq {
|
2023-06-08 16:42:35 +03:00
|
|
|
def ( != ) : x = not: (self == x)
|
2023-05-13 14:44:17 +03:00
|
|
|
}
|
|
|
|
|
|
2023-05-13 16:14:02 +03:00
|
|
|
//
|
2023-05-13 14:44:17 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
struct \order =
|
|
|
|
|
| $eq
|
|
|
|
|
| $lt
|
|
|
|
|
| $gt
|
2023-05-13 14:44:17 +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-13 22:40:33 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
decl min 'a[#ord] : 'a -> 'a -> 'a
|
2023-05-13 22:40:33 +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-13 22:40:33 +03:00
|
|
|
def max : x y = if x < y then y else x
|
2023-05-13 14:44:17 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
namespace var \ord {
|
2023-05-13 14:44:17 +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-13 14:44:17 +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-21 14:58:18 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
typeclass \read =
|
|
|
|
|
& read : \string -> \read
|
2023-05-22 22:02:31 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
typeclass \str[#show #read]
|
2023-05-13 14:44:17 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
// typeclass debug-show = // TODO
|
|
|
|
|
// & debugdshow : -> \string
|
2023-05-13 14:44:17 +03:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
typeclass \default =
|
|
|
|
|
& default : -> \default
|
2023-05-13 16:14:02 +03:00
|
|
|
|
|
|
|
|
//
|
2023-05-13 14:44:25 +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-13 14:44:25 +03:00
|
|
|
|
2023-05-13 16:14:02 +03:00
|
|
|
//
|
2023-05-13 14:44:17 +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-13 16:14:02 +03:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
namespace io {
|
|
|
|
|
decl print : \string -> \unit
|
|
|
|
|
decl scan : -> \string
|
2023-05-23 00:51:51 +03:00
|
|
|
}
|
2023-05-22 16:03:50 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
decl random : -> \int // TODO
|
2023-05-22 16:03:50 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
//
|
2023-05-13 16:14:02 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
// // // bad
|
|
|
|
|
// // typeclass \functor 'a =
|
|
|
|
|
// // & fmap 'b ('f[#functor['b]]) : ('a -> 'b) -> \functor -> 'f
|
|
|
|
|
//
|
|
|
|
|
// // typeclass \iterator[#eq] =
|
|
|
|
|
// // & next : -> \unit
|
|
|
|
|
// // & prev : -> \unit
|
2023-05-13 16:14:02 +03:00
|
|
|
//
|
2023-06-03 19:01:03 +03:00
|
|
|
// // typeclass \iterable 'iter[#iterable] =
|
|
|
|
|
// // & begin : -> 'iter
|
|
|
|
|
// // & end : -> 'iter
|
2023-05-13 16:14:02 +03:00
|
|
|
|
2023-05-13 14:44:17 +03:00
|
|
|
//
|
|
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
decl ( -- ) : \int -> \int -> \int`0
|
2023-05-21 14:58:18 +03:00
|
|
|
def ( -- ) : begin end = {
|
|
|
|
|
var current = begin
|
|
|
|
|
return (while current < end do {
|
2023-05-22 21:00:43 +03:00
|
|
|
; current += 1
|
2023-05-22 22:02:31 +03:00
|
|
|
bring current - 1
|
2023-05-21 14:58:18 +03:00
|
|
|
})
|
|
|
|
|
}
|
2023-05-17 18:29:39 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
// decl func : \string -> \int -> \int
|
2023-05-23 00:51:51 +03:00
|
|
|
// def func : s i = {
|
|
|
|
|
// ; print: s
|
|
|
|
|
// var x = s
|
2023-06-03 19:01:03 +03:00
|
|
|
// ; print: (i..show:)
|
2023-05-23 00:51:51 +03:00
|
|
|
// return 5
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// exec main {
|
|
|
|
|
// for i in 0--19 do func: "a" (i * 2 +. 3)
|
|
|
|
|
// ; print: ({
|
|
|
|
|
// if true then bring scan: else { ; print: "aaa" }
|
|
|
|
|
// bring "nothing"
|
|
|
|
|
// ; print: "aaa"
|
|
|
|
|
// })
|
|
|
|
|
// }
|
|
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
decl scan-int : -> \int
|
|
|
|
|
def scan-int = \int..read: (\io..scan:)
|
2023-05-23 00:51:51 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
decl print-int : \int -> \unit
|
|
|
|
|
def print-int : x = \io..print: (x..show:)
|
2023-05-23 00:51:51 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
decl scan-anything 'a[#read] : -> 'a
|
|
|
|
|
def scan-anything = 'a..read: (\io..scan:)
|
2023-05-23 00:51:51 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
decl print-anything 'a[#show] : 'a -> \unit
|
|
|
|
|
def print-anything : x = \io..print: (x..show:)
|
2023-05-23 00:51:51 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
// decl sorted 'a[#ord #copy]: 'a`0 -> \int -> 'a`0
|
2023-05-23 11:54:15 +03:00
|
|
|
// def sorted : a sz = {
|
2023-06-03 19:01:03 +03:00
|
|
|
// var a-copy = a
|
2023-05-23 11:54:15 +03:00
|
|
|
// if sz == 2 then {
|
2023-06-03 19:01:03 +03:00
|
|
|
// if a-copy`0 > a-copy`1 then {
|
|
|
|
|
// var x = a-copy`0
|
|
|
|
|
// a-copy`0 = a-copy`1
|
|
|
|
|
// a-copy`1 = x
|
2023-05-23 11:54:15 +03:00
|
|
|
// }
|
2023-06-03 19:01:03 +03:00
|
|
|
// return a-copy
|
2023-05-23 11:54:15 +03:00
|
|
|
// }
|
|
|
|
|
//
|
2023-06-03 19:01:03 +03:00
|
|
|
// var center = sz..div: 2
|
2023-05-23 11:54:15 +03:00
|
|
|
//
|
2023-06-03 19:01:03 +03:00
|
|
|
// var a-left = for i in 0--center do a`i
|
|
|
|
|
// var a-right = for i in center-sz do a`i
|
2023-05-23 11:54:15 +03:00
|
|
|
//
|
2023-06-03 19:01:03 +03:00
|
|
|
// return a-copy
|
2023-05-23 11:54:15 +03:00
|
|
|
// }
|
2023-05-23 00:51:51 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
// struct \array 'a = & 'a // 'a`0
|
2023-05-23 20:05:48 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
// namespace \array {
|
|
|
|
|
// decl of : 'a`0 -> \array['a]
|
|
|
|
|
// def of : x = $array['a] & data = x
|
2023-05-23 20:05:48 +03:00
|
|
|
// }
|
2023-05-23 11:54:15 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
struct \three-tuple = & \string & \string & \string
|
2023-05-23 11:54:15 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
decl scan-three-t : -> \three-tuple
|
|
|
|
|
def scan-three-t = $three-tuple & \io..scan: & \io..scan: & \io..scan:
|
2023-05-23 11:54:15 +03:00
|
|
|
|
2023-06-03 19:01:03 +03:00
|
|
|
decl scan-three : -> (& \string & \string & \string)
|
|
|
|
|
def scan-three = & \io..scan: & \io..scan: & \io..scan:
|
2023-05-23 11:54:15 +03:00
|
|
|
|
2023-05-13 13:11:12 +03:00
|
|
|
exec main {
|
2023-06-03 19:01:03 +03:00
|
|
|
var n = scan-anything:[int]
|
|
|
|
|
var x = (for _ in 0--n do scan-int:) // $array[int] & 0
|
|
|
|
|
; print-anything:[int] n
|
|
|
|
|
|
|
|
|
|
var & a & b & c = scan-three-t:
|
|
|
|
|
; \io..print: b
|
|
|
|
|
var & d & e & f = scan-three:
|
|
|
|
|
; \io..print: e
|
2023-05-13 13:11:12 +03:00
|
|
|
}
|