Skip to content

Commit

Permalink
feat: Add Display typeclass
Browse files Browse the repository at this point in the history
  • Loading branch information
bch29 committed Dec 15, 2020
1 parent e02e664 commit 41cfd04
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
22 changes: 22 additions & 0 deletions std/display.glu
Original file line number Diff line number Diff line change
@@ -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,
}
7 changes: 6 additions & 1 deletion std/float.glu
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down Expand Up @@ -55,13 +55,18 @@ let show : Show Float = {
show = (import! std.prim).show_float,
}

let display : Display Float = {
display = (import! std.prim).show_float,
}

{
additive,
multiplicative,
eq,
ord,
num,
show,
display,
..
import! std.float.prim
}
6 changes: 6 additions & 0 deletions std/int.glu
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -59,13 +60,18 @@ let show : Show Int = {
show = (import! std.prim).show_int,
}

let display : Display Int = {
display = (import! std.prim).show_int,
}

{
additive,
multiplicative,
eq,
ord,
num,
show,
display,
..
import! std.int.prim
}
4 changes: 4 additions & 0 deletions std/prelude.glu
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -64,6 +65,9 @@ let { flat_map } = import! std.monad
Show,
show,

Display,
display,

Option,
Bool,

Expand Down
4 changes: 4 additions & 0 deletions std/string.glu
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
(++),
Expand Down

0 comments on commit 41cfd04

Please sign in to comment.