On Fri, Apr 13, 2012 at 11:05:42PM +0400, Serge D. Mechveliani wrote:
> [..]
> 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.
>
>
> [..]
>
> 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).
I start to think now that this is by
(1) defining a Show category,
(2) defining an extended copy Integer1 for Integer, (**)
(3) defining a packge for each of the constructors
Integer1, Product, List, Fraction, UP,
(4) using further Integer1 instead of Integer.
Like this:
----------------------------------------------------------------------
INT ==> Integer
)abbrev category SHOW Show
Show() : Category == SetCategory with show : % -> String
)abbrev domain INT1 Integer1
Integer1() : Export == Implementation where
Export == Join(IntegerNumberSystem, ConvertibleTo String, OpenMath,_
Canonical, canonicalsClosed, Show)
Implementation == Integer add
Rep := INT
show(n : %) : String == convert(n) :: String
)abbrev package PAIR1 Pair1
Pair1(S : Show, T : Show) : Export == Implementation where
Pair ==> Product(S, T)
Export == with
show : Pair -> String
Implementation == add
show(p : Pair) : String ==
s := first p
t := second p
concat["(", show s, ",", show t, ")"]
...
----------------------------------------------------------------------
> show (construct(2,3) $ Product(INT1, INT1))
"(2,3)"
Is this what Waldek meant?
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.