From 41cfd04f64e10f42231079a79ea789b70e8d1b6c Mon Sep 17 00:00:00 2001 From: bch29 Date: Tue, 15 Dec 2020 11:51:07 +0000 Subject: [PATCH] feat: Add `Display` typeclass --- std/display.glu | 22 ++++++++++++++++++++++ std/float.glu | 7 ++++++- std/int.glu | 6 ++++++ std/prelude.glu | 4 ++++ std/string.glu | 4 ++++ 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 std/display.glu diff --git a/std/display.glu b/std/display.glu new file mode 100644 index 0000000000..ed44cea1e7 --- /dev/null +++ b/std/display.glu @@ -0,0 +1,22 @@ +//@NO-IMPLICIT-PRELUDE +//! Value to string conversion, intended for strings passed to users. + +/// `Display a` represents a conversion function from `a` to a pretty string. +#[implicit] +type Display a = { display : a -> String } + +/// Converts a value into a string. +/// ``` +/// let { ? } = import! std.effect +/// let { assert_eq, ? } = import! std.test +/// +/// seq assert_eq (display 123) "123" +/// seq assert_eq (display 3.14) "3.14" +/// assert_eq (display "abc") "abc" +/// ``` +let display ?d : [Display a] -> a -> String = d.display + +{ + Display, + display, +} diff --git a/std/float.glu b/std/float.glu index 35248f3c2b..762ecfa75f 100644 --- a/std/float.glu +++ b/std/float.glu @@ -1,7 +1,7 @@ //@NO-IMPLICIT-PRELUDE //! The 64-bit floating point type. -let { Semigroup, Monoid, Group, Eq, Ord, Ordering, Num, Show } = import! std.prelude +let { Semigroup, Monoid, Group, Eq, Ord, Ordering, Num, Show, Display } = import! std.prelude let additive = let semigroup : Semigroup Float = { append = \x y -> x #Float+ y } @@ -55,6 +55,10 @@ let show : Show Float = { show = (import! std.prim).show_float, } +let display : Display Float = { + display = (import! std.prim).show_float, +} + { additive, multiplicative, @@ -62,6 +66,7 @@ let show : Show Float = { ord, num, show, + display, .. import! std.float.prim } diff --git a/std/int.glu b/std/int.glu index 09affe7ac9..4ab81dcbdf 100644 --- a/std/int.glu +++ b/std/int.glu @@ -7,6 +7,7 @@ let { Group } = import! std.group let { Eq, Ord, Ordering } = import! std.cmp let { Num } = import! std.num let { Show } = import! std.show +let { Display } = import! std.display let additive = let semigroup : Semigroup Int = { @@ -59,6 +60,10 @@ let show : Show Int = { show = (import! std.prim).show_int, } +let display : Display Int = { + display = (import! std.prim).show_int, +} + { additive, multiplicative, @@ -66,6 +71,7 @@ let show : Show Int = { ord, num, show, + display, .. import! std.int.prim } diff --git a/std/prelude.glu b/std/prelude.glu index 5e584019fc..5553757b24 100644 --- a/std/prelude.glu +++ b/std/prelude.glu @@ -13,6 +13,7 @@ let { Monoid, empty } = import! std.monoid let { Group } = import! std.group let { Eq, Ord, Bool, Ordering, (==), (/=), (<), (<=), (>=), (>) } = import! std.cmp let { Show, show } = import! std.show +let { Display, display } = import! std.display let { Category, id, compose } = import! std.category let { Num, (+), (-), (*), (/), negate } = import! std.num let { Bool, not } = import! std.bool @@ -64,6 +65,9 @@ let { flat_map } = import! std.monad Show, show, + Display, + display, + Option, Bool, diff --git a/std/string.glu b/std/string.glu index d26068f4d0..6d65cbab9b 100644 --- a/std/string.glu +++ b/std/string.glu @@ -6,6 +6,7 @@ let prim = import! std.prim let { Semigroup, (<>) } = import! std.semigroup let { Monoid } = import! std.monoid let { Show } = import! std.show +let { Display } = import! std.display let { Eq, Ord, Ordering } = import! std.cmp let function = import! std.function @@ -25,10 +26,13 @@ let ord : Ord String = { eq, compare = prim.string_compare } let show : Show String = { show = \s -> "\"" ++ s ++ "\"" } +let display : Display String = { display = \s -> s } + { eq, ord, show, + display, semigroup, monoid, (++),