I am sorry, may I ask, again, a question about categories and standard
constructors?
This time it is more concrete.
Concerning my request on adding categories to standard constructors
Waldek wrote
> Why do you need Integer (etc.) to have ParseCategory? You can have:
>
> ParseCategory(T : SetCategory) : Category == SetCategory with
> parseElem : String -> Product(T, String)
> [..]
>
> and then implement packages of that category.
1. I know what is "a domain of that category",
but what does this mean: "a package of that category" ?
2. Here is a concrete and small contrived example/question presented by
an Haskell program.
------------------------------------------------------------------------
class Show1 a where show1 :: a -> String
--
-- An user class for printing a data to String.
-- `Show1' is taken to avoid clash with `Show' of the standard library.
-- class <-> `category' of Spad,
-- `::' <-> `:' of Spad, `=' <-> `==', `:=', ':' <-> cons.
-- `a', `b' ... <-> category or a package parameter in Spad.
-- Integer, (,), [], Ratio are constructors from the standard library,
-- (a, b) <-> Product(a, b), [a] <-> List a, Ratio <-> Fraction.
instance Show1 Integer where show1 = show -- use the library function
instance (Show1 a, Show1 b) => Show1 (a, b) -- print pair
where
show1 (x, y) = concat["(", show1 x, "," show1 y, ")"]
instance Show1 a => Show1 (Ratio a) -- print fraction
where
show1 (n % d) = concat["(", show1 n, "/", show1 d, ")"]
-- contrived code
instance Show1 a => Show1 [a] -- print list
where
show1 [] = "[]"
show1 (x : xs) = concat [ "[", show1 x, showL xs, "]" ]
where
showL [] = ""
showL (x: xs) = concat ["," show1 x, showL xs]
data UPol a = UPol String [(a, Integer)]
--
-- An user data for an univariate polynomial over `a':
-- UPol <variable> <monomial list>,
-- a monomial is a pair (coefficient, exponent).
instance Show1 a => Show1 (UPol a)
where
show1 (UPol v mons) = concat [ "(UPol", v, show1 mons, ")" ]
--
-- mons is a list of pairs, and for this list the instance of
-- show1 is already defined.
------------------------------------------------------------------------
This is a contrived code, is has not been tried to compile.
Example of usage: the program
let f = UPol "x" [(1/1, 4), ((-1)/2, 3), (1/1, 0)]
:: UPol (Ratio Integer)
-- represents x^4 - (1/2)x^3 + 1
in show1 f
yields "(UPol x [(1%1,4),((-1)%2,3),(1%1,0)])"
This approach uses defining user class instances for the standard
domain constructors (,), [], Ratio.
And Haskell has not packages; probably, its polymorphic functions
(+ adding user instances to standard constructors) are sufficient.
Now, what is a reasonable code for this whole example with show1
(for Product, List, Fraction, UP) in Spad ?
ShowCategory(T : SetCategory) : Category == SetCategory with
show1 : T -> String
(`a' <-> T).
What packages to define to make show1 work as in the above example
in Haskell ?
Thanks.
------
Sergei
[email protected]
--
You received this message because you are subscribed to the Google Groups
"FriCAS - computer algebra system" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/fricas-devel?hl=en.