On Sat, Apr 14, 2012 at 10:15:52AM +0400, Serge D. Mechveliani wrote:
> 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)"
But, again, this approach does not help. Because
show(xs : List Product(INT1, INT1))
cannot derive Show for Product(INT1, INT1).
Bill Page wrote for the example of Sized:
> It is common to see in the Axiom library for example something like:
>
> )abbrev package SIZED Sized
> Sized(A:OrderedRing): with
> size: A -> Integer
> size: List A -> Integer
> size: DirectProduct(2,A) -> Integer
> == add
> size(a:A):Integer == abs(a)
> size(a:List A):Integer == reduce(+, map(size$SizedInt(A),a))
> size(a:DirectProduct(2,A)):Integer ==
> size(first a)$SizedInt(A) + size(second a)$SizedInt(A)
>
> Note: This code is just of illustration. I haven't actually tried to
> compile it.
But I do not see how this approach will work for the example with Show.
> You should study the code for interpret and especially the package
> InputFormFunctions1
I look into
InputForm():
Join(SExpressionCategory(String,Symbol,Integer,DoubleFloat,OutputForm),
ConvertibleTo SExpression) with
...
and see
...
interpret x ==
v := interpret(x)$Lisp
mkObj(unwrap(objVal(v)$Lisp)$Lisp, objMode(v)$Lisp)$Lisp
It only refers to the Lisp library, everything is hidden.
Further,
.. package ..
InputFormFunctions1(R:Type):with
packageCall : Symbol -> InputForm
++ packageCall(f) returns the input form corresponding to f$R.
has the definitions like
packageCall name ==
convert([convert("$elt"::Symbol), Rname,
convert name]$List(InputForm))@InputForm
I wonder, how can this help my example with Show.
I am stuck: do not see how to program this in Spad without defining
the copies for the constructors: INT1, Product1, List1 ...
I shall be grateful to anyone who demonstrates how to program in a resonable
way in Spad this example with the category Show and constructors
Integer, Product, List, Fraction, UP.
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.