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] // namespace io { decl print : \string -> \unit decl scan : -> \string } decl random : -> \int // TODO decl error : \string -> \unit decl some 'a : 'a -> 'a? decl none 'a : 'a -> 'a? // decl not : \bool -> \bool def not : x = (match x with | true -> false | false -> true) // typeclass \char-container = & var size : -> \int & var at : \int -> \char // typeclass \move = // TODO & var ( <- ) : \move -> \unit 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 -> \idiv & var mod : \idiv -> \idiv 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 = & read : \string -> \read typeclass \str[#show #read] // typeclass debug-show = // TODO // & debugdshow : -> \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`_ def ( -- ) : begin end = { var current = begin return (while current < end do { ; current += 1 bring current - 1 }) } // decl func : \string -> \int -> \int // def func : s i = { // ; print: s // var x = s // ; print: (i..show:) // 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" // }) // } decl scan-int : -> \int def scan-int = \int..read: (\io..scan:) decl print-int : \int -> \unit def print-int : x = \io..print: (x..show:) decl print-int-with-comment : ::i \int -> \string? -> \unit def print-int-with-comment : i maybe-comment = { ; \io..print: (i..show:) var comment? = maybe-comment in \io..print: comment } decl scan-anything 'a[#read] : -> 'a def scan-anything = 'a..read: (\io..scan:) decl print-anything 'a[#show] : 'a -> \unit def print-anything : x = \io..print: (x..show:) // decl sorted 'a[#ord #copy]: 'a`0 -> \int -> 'a`0 // def sorted : a sz = { // var a-copy = a // if sz == 2 then { // if a-copy`0 > a-copy`1 then { // var x = a-copy`0 // a-copy`0 = a-copy`1 // a-copy`1 = x // } // return a-copy // } // // var center = sz..div: 2 // // var a-left = for i in 0--center do a`i // var a-right = for i in center-sz do a`i // // return a-copy // } struct \array 'a = & 'a`_ namespace \array { decl of : 'a`_ -> \array['a] def of : x = $array['a] & x } struct \three-tuple = & \string & \string & \string decl scan-three-t : -> \three-tuple def scan-three-t = $three-tuple & \io..scan: & \io..scan: & \io..scan: decl scan-three : -> (& \string & \string & \string) def scan-three = & \io..scan: & \io..scan: & \io..scan: exec main { // var n = \int..read: (\io..scan:) // // if n <= 0 then error: "n can't be less then 1" // // // var x = (for _ in 0--n do scan-int:) // var x = \array[int]..of: (for _ in 0--n do scan-int:) // // // var k? = if n < 2 then n * 2 +. 3 in // , print-anything:[string] "n < 2" // , print-anything:[int] k // // ; print-anything:[int] n ; print-int-with-comment: ::i 123 (some: "comment") // var & a & b & c = scan-three-t: // ; \io..print: b // var & d & e & f = scan-three: // ; \io..print: e }