[julia-users] Computed type for parametrized method definition

2015-03-10 Thread Benjamin Piwowarski
Hi,

I would like to use a computed type for a parametrized method definition, 
in order to catch as early as possible errors in types. I tried this

abstract typeA

immutable typeA1 <: typeA end
immutable typeA2 <: typeA end

type typeB1 end
type typeB2 end

function transformType(T)
if T == typeA1
   return typeB1
elseif T == typeA2
   return typeB2
end
error("Cannot handle type $T")
end

function compute{T<:typeA}(a::transformType(T), b::transformType(T))
return (a, b)
end

but it failed with the following error message

ERROR: Cannot handle type T<:typeA
 in error at error.jl:21
 in transformType at /Users/bpiwowar/Desktop/test.jl:15
 in include at 
/opt/homebrew-cask/Caskroom/julia/0.3.5/Julia-0.3.5.app/Contents/Resources/julia/lib/julia/sys.dylib
 in include_from_node1 at loading.jl:128
 in process_options at 
/opt/homebrew-cask/Caskroom/julia/0.3.5/Julia-0.3.5.app/Contents/Resources/julia/lib/julia/sys.dylib
 in _start at 
/opt/homebrew-cask/Caskroom/julia/0.3.5/Julia-0.3.5.app/Contents/Resources/julia/lib/julia/sys.dylib
while loading /Users/bpiwowar/Desktop/test.jl, in expression starting on 
line 18

The type transformType(T)is hence computed when the method is defined, but 
not when the method is called. Is there any way to prevent this (to defer 
evaluation at compilation time?). 

The only other way (I can think of) to get this result would be to use 
macros and enumerate the different possible types, but if somebody can 
think of a better solution, thanks!



Re: [julia-users] Computed type for parametrized method definition

2015-03-10 Thread Mauro
> abstract typeA
>
> immutable typeA1 <: typeA end
> immutable typeA2 <: typeA end
>
> type typeB1 end
> type typeB2 end

More Julian would be to write:

transformType(T::Type{typeA1}) = typeB1
transformType(T::Type{typeA2}) = typeB2
transformType(T::Type) = error("Cannot handle type $T")

> function transformType(T)
> if T == typeA1
>return typeB1
> elseif T == typeA2
>return typeB2
> end
> error("Cannot handle type $T")
> end
>
> function compute{T<:typeA}(a::transformType(T), b::transformType(T))
> return (a, b)
> end
>
> but it failed with the following error message
>
> ERROR: Cannot handle type T<:typeA
>  in error at error.jl:21
>  in transformType at /Users/bpiwowar/Desktop/test.jl:15
>  in include at 
> /opt/homebrew-cask/Caskroom/julia/0.3.5/Julia-0.3.5.app/Contents/Resources/julia/lib/julia/sys.dylib
>  in include_from_node1 at loading.jl:128
>  in process_options at 
> /opt/homebrew-cask/Caskroom/julia/0.3.5/Julia-0.3.5.app/Contents/Resources/julia/lib/julia/sys.dylib
>  in _start at 
> /opt/homebrew-cask/Caskroom/julia/0.3.5/Julia-0.3.5.app/Contents/Resources/julia/lib/julia/sys.dylib
> while loading /Users/bpiwowar/Desktop/test.jl, in expression starting on 
> line 18
>
> The type transformType(T)is hence computed when the method is defined, but 
> not when the method is called. Is there any way to prevent this (to defer 
> evaluation at compilation time?). 

Have a look at stagedfunction (v0.4).

> The only other way (I can think of) to get this result would be to use 
> macros and enumerate the different possible types, but if somebody can 
> think of a better solution, thanks!

why not write:
type typeB1 <: typeB end
type typeB2 <: typeB end
compute(a::typeB, b::typeB) = return (a, b)


Re: [julia-users] Computed type for parametrized method definition

2015-03-10 Thread Mauro
>> Have a look at stagedfunction (v0.4). 
>>
> I tried with the nightly build, but without success (using stagedfunction 
> instead of function) - however, it looks like it is the solution, maybe I 
> will just have to wait until this is implemented (and the syntax set).

You will need to adapt the code to work with stagedfunctions.  They work
in stages: with the types of the arguments the code for the actual
function is produced.  Sadly there is no documentation, I think.  See:
https://github.com/JuliaLang/julia/issues/7311

>> > The only other way (I can think of) to get this result would be to use 
>> > macros and enumerate the different possible types, but if somebody can 
>> > think of a better solution, thanks! 
>>
>> why not write: 
>> type typeB1 <: typeB end 
>> type typeB2 <: typeB end 
>> compute(a::typeB, b::typeB) = return (a, b) 
>>
> Because I would like to ensure that a and b are of the same type 

In that case use:

compute{T<:typeB}(a::T, b::T) = return (a, b)

T will be set to the concrete type of a and there will be a no-method
error if isa(b,T) is not true.


Re: [julia-users] Computed type for parametrized method definition

2015-03-12 Thread Benjamin Piwowarski


Le mardi 10 mars 2015 14:21:00 UTC+1, Mauro a écrit :
>
> > abstract typeA 
> > 
> > immutable typeA1 <: typeA end 
> > immutable typeA2 <: typeA end 
> > 
> > type typeB1 end 
> > type typeB2 end 
>
> More Julian would be to write: 
>
> transformType(T::Type{typeA1}) = typeB1 
> transformType(T::Type{typeA2}) = typeB2 
> transformType(T::Type) = error("Cannot handle type $T") 
>
ok, thanks
 

>
> > function transformType(T) 
> > if T == typeA1 
> >return typeB1 
> > elseif T == typeA2 
> >return typeB2 
> > end 
> > error("Cannot handle type $T") 
> > end 
> > 
> > function compute{T<:typeA}(a::transformType(T), b::transformType(T)) 
> > return (a, b) 
> > end 
> > 
> > but it failed with the following error message 
> > 
> > ERROR: Cannot handle type T<:typeA 
> >  in error at error.jl:21 
> >  in transformType at /Users/bpiwowar/Desktop/test.jl:15 
> >  in include at 
> > 
> /opt/homebrew-cask/Caskroom/julia/0.3.5/Julia-0.3.5.app/Contents/Resources/julia/lib/julia/sys.dylib
>  
>
> >  in include_from_node1 at loading.jl:128 
> >  in process_options at 
> > 
> /opt/homebrew-cask/Caskroom/julia/0.3.5/Julia-0.3.5.app/Contents/Resources/julia/lib/julia/sys.dylib
>  
>
> >  in _start at 
> > 
> /opt/homebrew-cask/Caskroom/julia/0.3.5/Julia-0.3.5.app/Contents/Resources/julia/lib/julia/sys.dylib
>  
>
> > while loading /Users/bpiwowar/Desktop/test.jl, in expression starting on 
> > line 18 
> > 
> > The type transformType(T)is hence computed when the method is defined, 
> but 
> > not when the method is called. Is there any way to prevent this (to 
> defer 
> > evaluation at compilation time?). 
>
> Have a look at stagedfunction (v0.4). 
>
I tried with the nightly build, but without success (using stagedfunction 
instead of function) - however, it looks like it is the solution, maybe I 
will just have to wait until this is implemented (and the syntax set).
 

>
> > The only other way (I can think of) to get this result would be to use 
> > macros and enumerate the different possible types, but if somebody can 
> > think of a better solution, thanks! 
>
> why not write: 
> type typeB1 <: typeB end 
> type typeB2 <: typeB end 
> compute(a::typeB, b::typeB) = return (a, b) 
>
Because I would like to ensure that a and b are of the same type 


Re: [julia-users] Computed type for parametrized method definition

2015-04-09 Thread Benjamin Piwowarski


Le mardi 10 mars 2015 17:56:40 UTC+1, Mauro a écrit :
>
> >> Have a look at stagedfunction (v0.4). 
> >> 
> > I tried with the nightly build, but without success (using 
> stagedfunction 
> > instead of function) - however, it looks like it is the solution, maybe 
> I 
> > will just have to wait until this is implemented (and the syntax set). 
>
> You will need to adapt the code to work with stagedfunctions.  They work 
> in stages: with the types of the arguments the code for the actual 
> function is produced.  Sadly there is no documentation, I think.  See: 
> https://github.com/JuliaLang/julia/issues/7311 
> 
>  
>
> >> > The only other way (I can think of) to get this result would be to 
> use 
> >> > macros and enumerate the different possible types, but if somebody 
> can 
> >> > think of a better solution, thanks! 
> >> 
> >> why not write: 
> >> type typeB1 <: typeB end 
> >> type typeB2 <: typeB end 
> >> compute(a::typeB, b::typeB) = return (a, b) 
> >> 
> > Because I would like to ensure that a and b are of the same type 
>
> In that case use: 
>
> compute{T<:typeB}(a::T, b::T) = return (a, b) 
>
> T will be set to the concrete type of a and there will be a no-method 
> error if isa(b,T) is not true. 
>

This is my fault - I did not the code generic enough. In my case, I would 
like to use different type transform functions, e.g.

function compute{T<:typeA}(a::transformTypeA(T), b::transformTypeB(T))
return (a, b)
end

I am not sure though staged function are the way to go - I tried to follow 
some examples, and I would have to make tests on the types (which is not 
the purpose - I just want to do early error checking by ensuring types are 
compatible) before returning several version of the function depending on 
the types.

Thanks for all your answers,
benjamin


Re: [julia-users] Computed type for parametrized method definition

2015-04-09 Thread Mauro

On Thu, 2015-04-09 at 17:57, Benjamin Piwowarski  wrote:
> Le mardi 10 mars 2015 17:56:40 UTC+1, Mauro a écrit :
>>
>> >> Have a look at stagedfunction (v0.4). 
>> >> 
>> > I tried with the nightly build, but without success (using 
>> stagedfunction 
>> > instead of function) - however, it looks like it is the solution, maybe 
>> I 
>> > will just have to wait until this is implemented (and the syntax set). 
>>
>> You will need to adapt the code to work with stagedfunctions.  They work 
>> in stages: with the types of the arguments the code for the actual 
>> function is produced.  Sadly there is no documentation, I think.  See: 
>> https://github.com/JuliaLang/julia/issues/7311 
>> 
>>  
>>
>> >> > The only other way (I can think of) to get this result would be to 
>> use 
>> >> > macros and enumerate the different possible types, but if somebody 
>> can 
>> >> > think of a better solution, thanks! 
>> >> 
>> >> why not write: 
>> >> type typeB1 <: typeB end 
>> >> type typeB2 <: typeB end 
>> >> compute(a::typeB, b::typeB) = return (a, b) 
>> >> 
>> > Because I would like to ensure that a and b are of the same type 
>>
>> In that case use: 
>>
>> compute{T<:typeB}(a::T, b::T) = return (a, b) 
>>
>> T will be set to the concrete type of a and there will be a no-method 
>> error if isa(b,T) is not true. 
>>
>
> This is my fault - I did not the code generic enough. In my case, I would 
> like to use different type transform functions, e.g.
>
> function compute{T<:typeA}(a::transformTypeA(T), b::transformTypeB(T))
> return (a, b)
> end
>
> I am not sure though staged function are the way to go - I tried to follow 
> some examples, and I would have to make tests on the types (which is not 
> the purpose - I just want to do early error checking by ensuring types are 
> compatible) before returning several version of the function depending on 
> the types.

If you want to compute on the types, as you do with transformTypeA(T)
then stagedfunctions are what you need.  There is now some
documentation, not merged yet, but here:

https://github.com/tlycken/julia/blob/doc-stagedfunctions/doc/manual/metaprogramming.rst#staged-functions

Maybe something like:

function compute{T,S}(a::T, b::S)
if transformTypeA(T)!=transformTypeB(S)
error("incompatible types")
end
:(return (a, b))
end