[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] Question about procedure type annotations
From: |
megane |
Subject: |
Re: [Chicken-users] Question about procedure type annotations |
Date: |
Sat, 27 Oct 2012 19:35:33 +0300 |
User-agent: |
mu4e 0.9.9-dev4; emacs 24.1.1 |
Felix writes:
> From: megane <address@hidden>
> Subject: [Chicken-users] Question about procedure type annotations
> Date: Wed, 24 Oct 2012 18:54:09 +0300
>
>> Hi!
>>
>> Below is an example that fails.
>>
>> Is this expected behavior? It feels a bit redundant to use 'assume' for
>> the parameter 'a' as the type for the procedure has already been
>> declared.
>>
>> (: foo (fixnum -> undefined))
>> (define (foo a)
>> (cond-expand
>> (compiling
>> (compiler-typecase a
>> (fixnum #t))
>> (else #t)))
>> (print a))
>>
>> ;; Error: in toplevel procedure `foo':
>> ;; (test1.scm:2) no clause applies in `compiler-typecase' for expression
>> of type `*':
>> ;; fixnum
>
> Nothing prevents me from doing
>
> (foo "123")
>
> The procedure can not expect to be always called with correctly typed
> arguments, from any possible call-site. The flow-analysis is
> intraprocedural and goes not beyond procedure boundaries. You can use
> the "-strict-types" option to declare that "foo" will indeed be called
> with correctly typed arguments - your example should work with that
> option, but calling it with other argument types will result in
> undefined behaviour at run-time.
Ah, of course.
>
> "assume" overrides whatever type-information is available from
> flow-analysis (as does "the").
>
> One direction would be to generate type-checks at entry to a
> procedure with declared argument types, and I'm currently
> working on something related. This would have a run-time cost,
> but the arguments would than be known to be correct. I'm
> not sure yet what is best.
That's really interesting! Would this be in the core or some kind of an
extension?
Am I correct that this would not cause much run-time overhead in simple
cases, as the types of the arguments would be checked anyway at some
point if they are used?
For example in: (define (foo a b) (+ a b)) the `+' would normally check
the types. But if you check the types (e.g. float?) first and then
assume them to be of correct type the `+' would be specialized to a call
that does no type checks.
You could currently do some kind of a procedure defining macro that
defines two versions, one that just does the type-checks and calls the
other. The second version contains the actual body and the arguments are
`assumed' to be of correct type. And, if the types are known at the
call-site it would be specialized to call the second one directly.
This would give a bit more fine grained solution than `-strict-types',
but you would then have two versions of each procedure.