In the following toy example we create a new type D
for which we want to implement the typeclass Show
. Now let's say for all constructors the derived show
function would be just fine, except for one special case A
for which we want to override that default.
Is it possible to do that, or can you just have all or none of them derived?
data D = A | B | C
deriving (Show)
-- goal:
-- show A = "A is special"
-- show B = "B"
-- show C = "C"
main = print $ show A
Try it online!
@YuvalFilmus I would say the class of partially decidable language is not closed under complement, due to the halting problem when trying to construct a decider for such a language... Now try to answer your question using this information.
Computationally, when we have to partially derive a function f ( x 1, …, x n) with respect to x i, we say that we derive it “as if the rest of the variables were constants”. This derivative is then denoted by:
The partial derivative symbol is generated in LaTeX with the command \partial, so that the previous equation was produced with the code: If you have to deal with several partial derivatives along with your document, it is not practical to have to write every time fraction with the corresponding \partial symbols.
If you have to deal with several partial derivatives along with your document, it is not practical to have to write every time fraction with the corresponding \partial symbols. It would be better to define a command that you passe only the function and the variable with respect to which we want to derive.
This can be done using generics. generic-data
provides a default implementation of showsPrec
, gshowsPrec
, and you can use pattern-matching to handle non-default cases before it:
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics
import Generic.Data (gshowsPrec)
data D = A | B | C
deriving (Generic)
instance Show D where
showsPrec _ A = ("A is special" ++)
showsPrec n x = gshowsPrec n x
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With