unparse, error

0 views
Skip to first unread message

Serge D. Mechveliani

unread,
Feb 22, 2012, 3:49:49 PM2/22/12
to fricas...@googlegroups.com
Please, can you explain the following two features?

`unparse' in Spad
*****************
The program

-------------------------------------------- t.input
INT ==> Integer
show(xs : List INT) : String ==
oF := xs :: OutputForm
unparse(oF :: InputForm)
f(xs : List INT) : INT ==
empty? xs => 0
n := first xs
n < 2 => error concat["f ", show xs, " : first(arg) < 2."]
n + 1
--------------------------------------------

works in the interpreter. And its Spad analogue

-------------------------------------------------- t.spad
INT ==> Integer
)abbrev package FOO Foo
Foo() : with
show : List INT -> String
f : List INT -> INT
==
add
show(xs : List INT) : String ==
oF := xs :: OutputForm
unparse(oF :: InputForm)
f(xs : List INT) : INT ==
empty? xs => 0
n := first xs
n < 2 => error concat["f ", show xs, " : first(arg) < 2."]
n + 1
----------------------------------------------------------

does not compile. How to make it work, please?

Error messages
**************
I am looking into src/algebra/*.spad* in FriCAS-1.1.5 for the
examples of forming a message under applying `error'.
And the examples occur like this:

f(l : List Foo) : List Foo ==
empty? l => l
first(l) = 0 => error "first(l) = 0"

As I think, the error report will be like this:

Error in the library code:
first(l) = 0

It does not show in what function and of what package the break has
happened, and what are the responsible argument values.

What I am missing, please?

In Haskell, I write like this

f xs = let msg = showString "\nModule Foo, f " . shows xs .
showString "\n:\n"
in
if head xs == 0 then error $ msg "head xs == 0.\n"
else
...
Here the function msg :: String -> String is used in all the error
calls in the body of f, it presents a common prefix for these
messages. For example, f [0,3] breaks with the message

--------------------
Error ...
Module Foo, f [0,3]
:
head xs == 0.
--------------------

Respecively, my code of
... => error concat["f ", show xs, " : first(arg) < 2."]

is an attempt to follow this style in Spad
(but this `show' does not work).

What are your comments, please?
Thanks,

------
Sergei
mec...@botik.ru


Waldek Hebisch

unread,
Feb 23, 2012, 2:48:34 PM2/23/12
to fricas...@googlegroups.com
Serge D. Mechveliani wrote:
>
> Please, can you explain the following two features?
>
> `unparse' in Spad
> *****************
> The program
>
> -------------------------------------------------- t.spad
> INT ==> Integer
> )abbrev package FOO Foo
> Foo() : with
> show : List INT -> String
> f : List INT -> INT
> ==
> add
> show(xs : List INT) : String ==
> oF := xs :: OutputForm
> unparse(oF :: InputForm)
> f(xs : List INT) : INT ==
> empty? xs => 0
> n := first xs
> n < 2 => error concat["f ", show xs, " : first(arg) < 2."]
> n + 1
> ----------------------------------------------------------
>
> does not compile. How to make it work, please?
>

show(xs : List INT) : String ==
unparse(convert(xs)@InputForm)

>
>
> Error messages
> **************
> I am looking into src/algebra/*.spad* in FriCAS-1.1.5 for the
> examples of forming a message under applying `error'.
> And the examples occur like this:
>
> f(l : List Foo) : List Foo ==
> empty? l => l
> first(l) = 0 => error "first(l) = 0"
>
> As I think, the error report will be like this:
>
> Error in the library code:
> first(l) = 0
>
> It does not show in what function and of what package the break has
> happened, and what are the responsible argument values.

Right, some functions print more information, but most
are as this.

--
Waldek Hebisch
heb...@math.uni.wroc.pl

Serge D. Mechveliani

unread,
Feb 24, 2012, 1:20:21 PM2/24/12
to fricas...@googlegroups.com
On Thu, Feb 23, 2012 at 08:48:34PM +0100, Waldek Hebisch wrote:
>
> [..]

>
> > -------------------------------------------------- t.spad
> > INT ==> Integer
> > )abbrev package FOO Foo
> > Foo() : with
> > show : List INT -> String
> > f : List INT -> INT
> > ==
> > add
> > show(xs : List INT) : String ==
> > oF := xs :: OutputForm
> > unparse(oF :: InputForm)
> > f(xs : List INT) : INT ==
> > empty? xs => 0
> > n := first xs
> > n < 2 => error concat["f ", show xs, " : first(arg) < 2."]
> > n + 1
> > ----------------------------------------------------------
> >
> > does not compile. How to make it work, please?
> >
>
> show(xs : List INT) : String ==
> unparse(convert(xs)@InputForm)


Yes. Thank you.

1. What is `@', please?

2. I also tried show(xs : List String) ...
in

)abbrev package FOO Foo
Foo() : with f : List String -> List String ==
add
show(xs : List String) : String == unparse(convert(xs)@InputForm)

f(xs : List String) : List String ==
empty? xs => [] :: List String
lx := first xs
lx = "]" => error concat["f ", show xs, " : first(arg) = ]"]
[lx, lx]

And -> f ["]", "cd"]
reports
Function: convert : % -> InputForm is missing from domain: String
Internal Error
The function convert with signature (InputForm)$ is missing from
domain String

It is desirable to use an universal show, may be

show(x : SomeAppropriateCategory) : String ==
unparse(convert(xs)@InputForm) ?
What might it be?

Thanks,

------
Sergei
mec...@botik.ru

Waldek Hebisch

unread,
Feb 24, 2012, 3:37:36 PM2/24/12
to fricas...@googlegroups.com
Serge D. Mechveliani wrote:
>
> > show(xs : List INT) : String ==
> > unparse(convert(xs)@InputForm)
>
>
> Yes. Thank you.
>
> 1. What is `@', please?

'@' request evaluation with specific result type. Main use is
to resolve overloading on function return type: frequently
there are several 'convert' function which differ only in
their return type. 'convert(xs)@InputForm' request 'convert'
producing value of type 'InputForm'. It is differnet from
'::' -- '@' reports error if compiler can not select
overloaded function in a way giving requested result type.
'::' calls the 'coerce' function which may do some computation
to convert value form one type to another.

>
> 2. I also tried show(xs : List String) ...
> in
>
> )abbrev package FOO Foo
> Foo() : with f : List String -> List String ==
> add
> show(xs : List String) : String == unparse(convert(xs)@InputForm)
>
> f(xs : List String) : List String ==
> empty? xs => [] :: List String
> lx := first xs
> lx = "]" => error concat["f ", show xs, " : first(arg) = ]"]
> [lx, lx]
>
> And -> f ["]", "cd"]
> reports
> Function: convert : % -> InputForm is missing from domain: String
> Internal Error
> The function convert with signature (InputForm)$ is missing from
> domain String
>
> It is desirable to use an universal show, may be
>
> show(x : SomeAppropriateCategory) : String ==
> unparse(convert(xs)@InputForm) ?
> What might it be?

In your case the message says that 'convert' function in String is
supposed to exist, but actually is unimplemented. For String the
solution is to use newest version (I added implementation on
January 9).

In general you have to problems:
- Axiom/FriCAS is an open-ended system, new domains get added
and they may be of arbitrary type, so there is no warranty
that domain will satify any specific category.
- even if domain satisfy some category, specific function
exported by the category may be unimplemented

Concerning debugging messages: the official way to create
output is first coerce what you have in hand to OutputForm
and then use print. Unfortunately, while you can print
OutputForm, there is no provision to get a string. This
is because OutputForm was designed for rich 2-dimensional
output and making decent formatter requires not so trivial
work. Actually, there are ways of getting string version
from OutputForm: TexFormat allows you to get TeX version
as a string and similarlt MathMLFormat. However, I am
not sure how useful it is for debugging.

BTW: FriCAS contains tracing facility, for example:

)trace String )math

will print arguments and results of all calls to
exported functions in String.

BTW2: When I wanted to provide more information in
error message I used construct like:

bad?(val) =>
print(val::OutputForm)$OutputForm
error "foo_routine: got bad val"

--
Waldek Hebisch
heb...@math.uni.wroc.pl

Reply all
Reply to author
Forward
0 new messages