flan/test/programs/dyn-fold-position.flan

34 lines
1.4 KiB
Plaintext

;;;; A dyn operand anywhere in a fold makes the fold dyn from there on: the
;;;; typed operands before it are folded typed, and the dyn runtime takes the
;;;; rest. (+ 1 2 d) is (+ 3 d), whichever position the dyn is in.
(defn as-i32 [x i32] i32 x)
(defn as-f64 [x f64] f64 x)
(defn main [] i32
(let [d (the dyn \a)
f (the dyn 2.5)
n (the dyn 4)
c \a]
;; + and - over a dyn char: first, middle, last.
(println (+ d 1 2) (+ 1 d 2) (+ 1 2 d))
(println (- d 1 2) (- 10 n 1) (- 10 1 n))
;; A dyn float past an integer pair promotes, as (* 6 f) does.
(println (* f 2 3) (* 2 f 3) (* 2 3 f))
(println (/ f 2 5) (/ 9 f 2) (/ 9 2 f))
(println (+ 1 2 3 f) (- 10 1 2 f))
;; A char fold with a dyn past the pair.
(println (+ c 1 n) (- c 1 n) (+ c 1 2 n))
;; The bitwise folds.
(println (bit-or n 1 2) (bit-or 1 n 2) (bit-or 1 2 n))
(println (bit-and n 7 6) (bit-and 7 n 6) (bit-and 7 6 n))
(println (bit-xor n 1 2) (bit-xor 1 n 2) (bit-xor 1 2 n))
;; Comparison chains: a float past an integer pair is compared as one.
(println (< f 3 4) (< 1 f 3) (< 1 2 f) (< 1 3 f))
(println (<= 2 2 f) (> 3 2 f) (>= 3 3 f) (>= 3 3 n))
(println (= 4 4 n) (= 4 n 4) (= n 4 4) (= 4 4 f))
(println (!= 1 2 n) (!= 1 4 n) (!= 2 f 3))
;; At a typed want the dyn answer is opened at the end.
(println (as-i32 (+ 1 2 n)) (as-f64 (* 2 3 f))))
0)