On Tue, Nov 07, 2023 at 01:28:33PM +0100, Ralf Hemmecke wrote:
> On 11/7/23 02:29, Waldek Hebisch wrote:
> > > Yes, exactly that is the point. qnew claims to return an object of type %,
> > > but, in fact, it doesn't.
> >
> > Well, result of 'qnew' is uninitialized, but this does not differ
> > much from (note no initialization)
> >
> > a : T
>
> Maybe technically (i.e. how it is implemented in the compiler) it gives
> similar things, but conceptually, I would say that "a:T" is different.
> It is a declaration and thus just a hint to the compiler/interpreter that
> when this identifier is used it is to be considered as something of type T.
Well, we differ in our opinions what is conceptual. And we differ in
conceputal model of what "a : T" is doing: declaration instructs compiler
to allocate space for one thing (value of the variable) and promises
that we will store there only things of type T. 'qnew' allocate
space for specified number of things and type of array give similar
promise. They differ somewhat in ways you use them: "a : T"
associates name 'a' with allocated storage, in case or arrays
location are denoted by indexing expressions like 't(1,1)'. So
the way how you specify used storage cell is different, but semantics
of essentially the same. In FriCAS access to array element may
look like a function call, but logically is the same as access to
simple variable. So I would say that at Spad level implementaion
differs quite a lot but conceptually they are almost the same
(at lover level again there is some convergence of implementations).
> However, I would expect the compiler to yell at me when I write code like
>
> =======================
> )abbrev package FOO Foo
> Foo: E == I where
> E ==> with
> foo: () -> Integer
> I ==> add
> foo(): Integer ==
> a: Integer
> b: Integer := a
> =======================
>
> Unfortunately, it doesn't. :-(
Compiler emits warnings, it can not be (much) more because problem
of reliably and staticaly detecting reads from uninitialized variables
is unsolvable.
> > Would you say that a does not have type T?
>
> It has type T, but after a declaration a:T, there is not yet reservation of
> memory on my computer,
No, declaration reserves memory for variable. Resonable model is
that memory is reserved when declaration is "executed", that is
allocation happens at runtime when control flow reaches block
in which variable is declared (so actually "before" declaration,
but the difference is not important as you can use variable only
after declaration).
> so in fact, I do not yet have a variable, just a hint
> to the compiler that the identifier "a" has type T when it comes to type
> checking.
>
> > > > We could add the followowing to description of 'qnew':
> > > >
> > > > Use only when you know what you are doing.
> > >
> > > That is one thing, but does not give enough information. How should a user
> > > know what he is doing? Where would he get this information from?
> >
> > One of fundamental commandments of programming is:
> >
> > "Thou shall not attempt to read uninitialized variables"
>
> Of course. But putting a big warning sign is much better than just
> mystically saying "Use only when you know what you are doing." and not
> telling where to get information about it. It is like saying "Do not use
> this function, because we are not telling you what it does."
>
> I do not quite get your rejection of more informative documentation.
Well, when we got our first Unix machine in math in Wroclaw (in 1993)
it came with manual pages. There were hundreds of programs, each
having a manual "page", frequently having size of several printed
pages. Typical user reacion was "the system is badly documented,
documentation is so big that it is impossible to find anything".
And there is question what is "informative": I do not think
that _very_ basic "safety" warnings are informative.
> Put whatever you want into the ++ docstring.
> I just wanted to say that I am more on the side of having documentation that
> does not require uses to look at the code in order to understand what a
> function does.
Well, 'qnew' docstring clearly says that allocated array is
uninitialized. Rest is general knowledge (and code will not
help if somebody lacks that knowledge).
> > This is general programming knowledge, to get such knowledge
> > Dijkstra "Discipline of programming" may be good start.
>
> Are you saying that people how haven't read this, should not use FriCAS?
> I fear that then many mathematicians are excluded from or shy away from
> FriCAS.
Well, we are talking about 'qnew' here. Begining users, or even more
advanced ones who do not want to think about prgramming concepts should
avoid dangerous functions, like 'qnew' or destructive operations.
> > But there is more to this: people may think that someting like
> >
> > a := qnew(4, 4)$Matrix(Float)
> > b := a(1,1) - a(1, 1)
>
> Why? a(1,1) is clearly the same as elt(a,1,1), i.e. use of a in a function.
> Forbidden by my ++ suggestion.
Well, you look at implementaion and you see what looks like a function.
But conceptually it is not a function. With your proposal users having
right conceptual model may think that the above is allowed, while some
legal uses are forbiden (like using a function to intialize an array).
> > Do you want to add a warning in FriCAS book in section describing
> > declarations that one should initialize variables before use?
>
> Yes, why not. We all know that there is a difference between a FriCAS
> (programming) variable and a variable (i.e. something that evaluates to
> itself). In traditional CAS that's not totally easy to understand for a
> first time user.
Well, FriCAS book (and more so docstrings) is a reference documentation.
One merit of such documentation is shortness, that is skipping
information that user should already know. To say that truth,
I expect users to "know" that thay should not read vatiable before
initialization. "Know" in sense that users heard that at some
moment and forgot/ignored that information. I expect that short
warning in our documentaion would be ignored like previous
information.
Another is "findability": information should be in places were users
are expected to search for it. User unaware of dangers of using
uninitialized variables is unlikely to seach for such information
is section called "declarations". It makes sense to have a separate
section or chapter with title like "Programming model and safety"
which explains this and related things in more depth. Then we
could put short reference to this in part about declarations.
Docstring of 'qnew' is simply wrong place for such information.
--
Waldek Hebisch