2017-09-06 11:05:19 +03:00
|
|
|
(use IO)
|
|
|
|
(use Int)
|
|
|
|
(use Array)
|
2018-06-05 11:05:33 +03:00
|
|
|
(Project.no-echo)
|
2017-06-26 12:15:03 +03:00
|
|
|
|
2020-06-02 06:39:19 +03:00
|
|
|
(definterface fmap (λ [(Ref (λ [a] b)) (f a)] (f b)))
|
2017-11-23 01:24:38 +03:00
|
|
|
|
2017-06-26 12:15:03 +03:00
|
|
|
(defmodule ArrayExtension
|
2017-10-17 17:54:00 +03:00
|
|
|
(defn fmap [f a] (Array.endo-map f a))
|
2020-05-10 20:32:22 +03:00
|
|
|
(implements fmap ArrayExtension.fmap)
|
2017-06-26 12:15:03 +03:00
|
|
|
)
|
|
|
|
|
2021-11-30 12:35:22 +03:00
|
|
|
(deftype (MyBox a) [x a])
|
2017-06-26 12:15:03 +03:00
|
|
|
|
2021-11-30 12:35:22 +03:00
|
|
|
(defmodule MyBox
|
|
|
|
(defn fmap [f box] (let [new-x (~f @(MyBox.x &box))]
|
|
|
|
(MyBox.set-x box new-x)))
|
|
|
|
(implements fmap MyBox.fmap))
|
2017-06-26 12:15:03 +03:00
|
|
|
|
2021-11-30 12:35:22 +03:00
|
|
|
(use MyBox)
|
2017-09-06 11:05:19 +03:00
|
|
|
(use ArrayExtension)
|
2017-06-26 12:15:03 +03:00
|
|
|
|
2020-06-02 06:39:19 +03:00
|
|
|
;; TODO: This function currently concretizes to the type of the first (f *) it
|
|
|
|
;; receives. Is there a way for us to ensure it remains generic?
|
|
|
|
;; N.B. the only reason it worked previously was because it was ill-typed as
|
|
|
|
;; (a -> a) which erroneously served as a universal type.
|
|
|
|
;(sig higherOrder (Fn [(f a)] (f b)))
|
|
|
|
;(defn higherOrder [x] (fmap &Int.inc x))
|
2017-06-26 12:15:03 +03:00
|
|
|
|
|
|
|
(defn main []
|
|
|
|
(do
|
2021-11-30 12:35:22 +03:00
|
|
|
(println &(str @(MyBox.x &(fmap &Int.inc (MyBox.init 100)))))
|
|
|
|
(println &(str @(MyBox.x &(MyBox.fmap &inc (MyBox.init 100)))))
|
2018-11-14 16:09:43 +03:00
|
|
|
(println &(str &(ArrayExtension.fmap &inc [10 20 30 40 50])))
|
|
|
|
(println &(str &(fmap &Int.inc [10 20 30 40 50])))
|
|
|
|
(println &(Array.str &(fmap &Int.inc [10 20 30 40 50])))
|
|
|
|
(println &(Array.str &(ArrayExtension.fmap &Int.inc [10 20 30 40 50])))
|
2020-06-02 06:39:19 +03:00
|
|
|
;(println &(str &(higherOrder (Box.init 999))))
|
|
|
|
;(println &(str &(higherOrder [9 99 999 9999])))
|
2017-06-26 12:15:03 +03:00
|
|
|
))
|