haskell - Data constructors without breaking the open/closed principle -
haskell - Data constructors without breaking the open/closed principle -
i have info constructor this
class fooclass foo :: -> b class barclass bar :: -> b info foobar = foo :: fooclass => -> io () | bar :: barclass => -> io () so can utilize pattern matching:
foobar :: foobar -> -> io () foobar (foo f) x = f (foo x) foobar (bar f) x = f (bar x) however, breaks open/closed principle. i'd able extend foobar additional methods based on other classes.
how implement in haskell?
as others have pointed out, code flawed in ways obscure question. it's unsafe seek think hard how oo principles translate fp. have place, because much of oo embedded in fp naturally, it's much improve larn fp straight first , observe laws later special cases.
in particular, can talk how greater refinement of types form of extension. instance, comparing types like
(num a) => -> io () (num a, show a) => -> io () we can talk how sec function takes in set of types natural subtype of inputs first function. in particular, set of possible types can input sec function refinement of inputs first. users of these functions, there fewer valid ways utilize sec function. implementers of these functions, there more valid ways implement sec function. in fact, know following
all values valid inputs sec function valid inputs first all functions correctly typed first signature correctly typed second.this duality between giving , taking explored in study of game semantics. thought of "open extension" plays out trivially in can decide inquire more refined type, it's uninteresting since that's obvious in how refined types used.
so adts (data declarations) directly? open/closed? mu—adts aren't objects, rule not apply directly.
haskell constructor open-closed-principle
Comments
Post a Comment