Re: (declare (pure ...))

2024-02-10 Thread siiky via

Hi Al,

On a practical level, I would be sad if vector-ref, for example, was 
"impure", and thus compiling a vector-ref invalidated all previously- 
checked globals for the current scope. Likewise, I would prefer to 
declare a procedure using vector-ref as pure, to let csc know that it 
does not modify globals (or the filesystem etc).


The function vector-ref doesn't stop being pure (i.e. referentially 
transparent) depending how you use it. The function is always 
referentially transparent -- the expression (vector-ref some-global-vec) 
isn't. Quoting from "Functional Programming in C++" by Ivan Čukić:


> We’re going to define purity more precisely now, through a concept 
called referential transparency. Referential transparency is a 
characteristic of expressions, not just functions. Let’s say an 
expression is anything that specifies a computation and returns a 
result. We’ll say an expression is referentially transparent if the 
program wouldn’t behave any differently if we replaced the entire 
expression with just its return value. If an expression is referentially 
transparent, it has no observable side effects, and therefore all 
functions used in that expression are pure.


(p.104)

I think that part just decides whether the procedure is "pure", for 
whatever purity means. That doesn't say anything about what compiling a 
call to such a "pure" procedure means (can memoize the value or not, 
need to re-check globals or closures afterwards or not etc).


Both the Declarations and Types manual pages say clearly it means 
"referencial transparency."


And RE local state, that you asked in another email: my understanding is 
that that refers to variables belonging to the closure, that survive 
across calls. E.g.:


(define foo
  (let ((x 0))
(lambda (y)
  (set! x (add1 x))
  (* 2 y

siiky





Re: (declare (pure ...))

2024-02-09 Thread siiky via

Hi Al,


what does (declare (pure ..)) mean to csc? Is the function supposed to be

* only side-effect free, or ...

* also return the same value when called with the same arguments?


I can't remember where `(declare)` is documented, but I believe this [0] 
is also relevant.


[0] https://wiki.call-cc.org/man/5/Types#purity

Good luck!
siiky





Re: compiler types: records / collections interaction

2024-02-09 Thread siiky via

Hi Al,


What combination of

* define-record-type (srfi-9, srfi-99, chicken define-record, whatever) and

* (declare type ...)

can I use to inform the compiler that (for collections of myrec) vector- 
ref returns myrec's (likewise list car/cdr, hash-table-ref)? And that it 
needs not emit any instance-type-checking code for objects extracted 
from such collections?


For "native" Scheme types, there's at least `list-of` and `vector-of` 
[0]. The type definitions of car/cdr, vector-ref, etc are in the file 
types.db (you can find it at $PREFIX/lib/chicken/11/types.db).


For other data-structures you have to look at the specific 
implementation. For example, SRFI-69 [1] mentions types in the 
changelog. They're not mentioned in the docs, however, so you'll have to 
dig through the code to confirm this.


Separately, how can I tell the compiler that fields in these records 
have certain types? Add type declarations for the implicitly-defined 
per-field accessors and mutators?


To annotate the types of the fields of a record, take a look at 
defstruct[2]. There may be other alternatives, I only know of this one.



[0] https://wiki.call-cc.org/man/5/Types#type-syntax
[1] https://wiki.call-cc.org/eggref/5/srfi-69
[2] https://wiki.call-cc.org/eggref/5/typed-records#defstruct

Good luck!
siiky





Re: compile-time deserialization

2023-12-26 Thread siiky via
Oh I forgot to mention you need an extra compiler flag to use it 
(-extend). And I also just remembered that I wrote a post about this at 
the time. (:


https://siiky.srht.site/scheme/reader-syntax.html





Re: compile-time deserialization

2023-12-26 Thread siiky via

Hi,

You can use reader syntax[0]. Take a look at this SQL reader syntax[1,2] 
I wrote some time ago for inspiration (it should require very little 
changing).


[0] https://api.call-cc.org/5/doc/chicken/read-syntax/set-read-syntax%21
[1] 
https://git.sr.ht/~siiky/save-for-later/tree/c7297083127ac1543bf6013e27259b35d07236ee/item/sql-reader-syntax/sql-reader-syntax.scm
[2] 
https://git.sr.ht/~siiky/save-for-later/tree/c7297083127ac1543bf6013e27259b35d07236ee/item/sql-reader-syntax/example.scm


siiky