No Activity tracked this Week
- 🔭 Working with the Julia programming language.
Full-featured traits in Julia. Without full features how dare I say this?
License: MIT License
I am trying to define this trait but it gives me errors.
@trait StringConvertible{T} begin
(hasmethod) :: (convert, Tuple{String, T}) => true
end
ERROR: error in method definition: function Base.hasmethod must be explicitly imported to be extended
Stacktrace:
[1] top-level scope at none:0
[2] top-level scope at none:1
Trying the following changes the error:
@trait StringConvertible{T} begin
(Base.hasmethod) :: (convert, Tuple{String, T}) => true
end
ERROR: LoadError: MethodError: no method matching extract_method_interface(::Expr, ::Array{Expr,1}, ::Bool, ::Array{Any,1})
Closest candidates are:
extract_method_interface(::Symbol, ::AbstractArray, ::Any, ::AbstractArray) at C:\Users\yahyaaba\.julia\packages\CanonicalTraits\IA6yQ\src\Utils.jl:87
Stacktrace:
[1] ##CanonicalTraits 146#400#51 at .\none:0 [inlined]
[2] ##CanonicalTraits 144#398#50 at .\none:0 [inlined]
[3] ##CanonicalTraits 137#391#49 at .\none:0 [inlined]
[4] ##CanonicalTraits 135#389#48 at .\none:0 [inlined]
[5] extract_method(::Expr) at C:\Users\yahyaaba\.julia\packages\CanonicalTraits\IA6yQ\src\Utils.jl:99
[6] iterate at .\generator.jl:47 [inlined]
[7] collect_to!(::Array{LineNumberNode,1}, ::Base.Generator{SubArray{Any,1,Array{Any,1},Tuple{UnitRange{Int64}},true},typeof(CanonicalTraits.extract_method)}, ::Int64, ::Tuple{Base.OneTo{Int64},Int64}) at .\array.jl:711
[8] collect_to_with_first!(::Array{LineNumberNode,1}, ::LineNumberNode, ::Base.Generator{SubArray{Any,1,Array{Any,1},Tuple{UnitRange{Int64}},true},typeof(CanonicalTraits.extract_method)}, ::Tuple{Base.OneTo{Int64},Int64}) at .\array.jl:689
[9] _collect(::SubArray{Any,1,Array{Any,1},Tuple{UnitRange{Int64}},true}, ::Base.Generator{SubArray{Any,1,Array{Any,1},Tuple{UnitRange{Int64}},true},typeof(CanonicalTraits.extract_method)}, ::Base.EltypeUnknown,
::Base.HasShape{1}) at .\array.jl:683
[10] collect_similar at .\array.jl:607 [inlined]
[11] map at .\abstractarray.jl:2072 [inlined]
[12] ##CanonicalTraits 406#660#110 at C:\Users\yahyaaba\.julia\packages\CanonicalTraits\IA6yQ\src\Typeclasses.jl:101 [inlined]
[13] (::CanonicalTraits.var"##CanonicalTraits 406#660#110"{Symbol,SubArray{Any,1,Array{Any,1},Tuple{UnitRange{Int64}},true},LineNumberNode,Array{Pair{Symbol,Expr},1},Array{Expr,1}})(::Array{Any,1}) at .\none:0
[14] ##CanonicalTraits 404#658#109 at .\none:0 [inlined]
[15] ##CanonicalTraits 416#670#108 at C:\Users\yahyaaba\.julia\packages\CanonicalTraits\IA6yQ\src\Typeclasses.jl:96 [inlined]
[16] (::CanonicalTraits.var"##CanonicalTraits 414#668#107"{LineNumberNode,Expr,Array{Pair{Symbol,Expr},1},Array{Expr,1}})(::Expr) at .\none:0 [17] trait(::LineNumberNode, ::Any, ::Any) at C:\Users\yahyaaba\.julia\packages\CanonicalTraits\IA6yQ\src\Typeclasses.jl:96
[18] @trait(::LineNumberNode, ::Module, ::Any, ::Any) at C:\Users\yahyaaba\.julia\packages\CanonicalTraits\IA6yQ\src\Typeclasses.jl:151
in expression starting at none:1
https://github.com/thautwarm/CanonicalTraits.jl/blame/master/docs/src/index.md#L93
wrong:
- when `V` is `Vector{T}`, `V` is `T`.
- when `V` is `NTuple{5, T}`, `V` is `T`
right:
- when `V` is `Vector{T}`, `F` is `T`.
- when `V` is `NTuple{5, T}`, `F` is `T`
caused by JuliaLang/julia#33240
causing JuliaStaging/GeneralizedGenerated.jl#17
@trait Typeable{T} begin
to_type :: T => Type{<:TypeLevel{T}}
to_type(x::T) = TVal{T, x}
from_type :: Type{<:TypeLevel{T}} => T
from_type(t::Type{<:TypeLevel{T}}) = interpret(t)
show_repr :: [IO, Type{<:TypeLevel{T}}] => Nothing
show_repr(io, t) = begin
print(io, from_type(t))
end
end
Hi Taine,
In one of your example, you have
@trait Addable{L, R} begin
(+) :: [L, R] => Any
(+) = Base.:+
end
I'd like to do something like this, but avoid the Any
. I was thinking something like
@trait Addable{L, R, X} where {X = inferAddable(L,R)} begin
(+) :: [L, R] => X
(+) = Base.:+
end
But then I'm having trouble getting the @implement
to work out. Is this currently possible?
@trait Add1{T} begin
add1 :: [T] => T
end
@trait Add1{T} >: Addn{T} begin
addn :: [Int, T] => T
addn(n, x) = let s = x; for i in 1:n; s = add1(x) end; s; end
end
@implement Add1{T} >: Add1{Vector{T}} where T begin
add1(xs) = add1.(xs)
end
So I think what @AriMKatz was getting at in #7 (comment) was that they want a way to implement a trait based on behaviour instead of on datatype.
That is, suppose we have two traits TraitA
and TraitB
@trait TraitA{T} begin
...
end
@trait TraitB{T} begin
...
end
Ari wants to be able to write
@trait TraitC{T} begin
f :: [T] => T
end
@implement TraitC{T} where {hastrait(T, TraitA)} begin
f(x::T) = ...
end
@implement TraitC{T} where {hastrait(T, TraitB)} begin
f(x::T) = ...
end
So the implementation of TraitC{T}
will depend on if T
has the trait TraitA
or if it has TraitB
. Did I get that right Ari?
I think this is a sensible thing to try and support, but it might be tricky to implement.
trait
is used in the "non-canonical trait" sense in many places throughout Julia code and docs.
For that reason only, using a macro name that differs from "@trait" may be helpful to the wider use and easier general understanding of this package, when released. I thought @ctrait
(for canonical trait) might work well.
Currently, this creates a trait InnerProd
which is a subtrait of VecSpace
:
@trait VecSpace{F, V} >: InnerProd{F, V} where
{F = V2F(V)} begin
dot :: [V, V] => F
end
I feel this is kinda unclear, and awkward to read. Could we also support the reverse syntax? e.g.
@trait InnerProd{F, V} <: VecSpace{F, V} where
{F = V2F(V)} begin
dot :: [V, V] => F
end
I find this much more legible and 'julian'.
There exists some dependency issue here.
ERROR: Unsatisfiable requirements detected for package GeneralizedGenerated [6b9d7cbe]:
GeneralizedGenerated [6b9d7cbe] log:
├─possible versions are: [0.1.0-0.1.4, 0.2.0-0.2.1] or uninstalled
├─restricted to versions 0.2.0 by an explicit requirement, leaving only versions 0.2.0
└─restricted by compatibility requirements with CanonicalTraits [a603d957] to versions: 0.1.0-0.1.4 or uninstalled — no versions left
└─CanonicalTraits [a603d957] log:
├─possible versions are: 0.2.1 or uninstalled
└─CanonicalTraits [a603d957] is fixed to version 0.2.1
I stumbled upon this by trying:
develop CanonicalTraits
with GeneralizedGenerated in my global environment.
Edit: I realize this must be a registry issue right?
This issue is used to trigger TagBot; feel free to unsubscribe.
If you haven't already, you should update your TagBot.yml
to include issue comment triggers.
Please see this post on Discourse for instructions and more details.
If you'd like for me to do this for you, comment TagBot fix
on this issue.
I'll open a PR within a few hours, please be patient!
Hi Taine,
I'd like to better understand the limitations of this approach, and any workarounds.
In the docs you say,
Due to the limitations of dynamic language, the type parameters occurred in trait signature should occur in the argument of each trait methods.
I had some bugs in an early attempt to use this, so your point on this is very helpful. But then in the README example you have,
@trait VecSpace{F, V} >: InnerProd{F, V} where
{F = V2F(V)} begin
dot :: [V, V] => F
end
Doesn't dot
violate this constraint? Is it the functional dependency that makes it still work? Do you have any general suggestions for using this package, especially wrt any interactions with abstract types?
Hi Taine,
I was just talking to @McCoyBecker about traits. There are a few different approaches in Julia, and it can be tough to choose among them. He also mentioned being a fan of the approach Rust takes for this.
How would you compare these with CanonicalTraits
?
using CanonicalTraits
@trait OPlus{T} begin
(⊕) :: [T, T] => T
end
@trait OAdd{T} begin
(⊕) :: [T, T] => T
end
struct NumType
value::Int
end
@implement! OAdd{NumType} begin
⊕(x,y) = NumType(x.value + y.value)
end
@implement! OPlus{NumType} begin
⊕(x,y) = NumType(100(x.value + y.value))
end
NumType(2) ⊕ NumType(3)
This gives the answer NumType(5)
but I think it could be ambiguous. Rust has disambiguation syntax:
When we call fly on an instance of Human, the compiler defaults to calling the method that is directly implemented on the type
...
To call the fly methods from either the Pilot trait or the Wizard trait, we need to use more explicit syntax to specify which fly method we mean.
CanonicalTraits could throw an ambiguity error by wrapping each implementation in a module and using
it.
x, y = NumType(2), NumType(3)
OAdd(typeof(x)).:⊕(x,y) # NumType(5).
OPlus(typeof(x)).:⊕(x,y) # NumType(500)
It's a little verbose but an @as
macro could make it shorter.
Links
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.