Skip to content

Commit

Permalink
WIP: make NTuple{N,T} a typealias for Tuple{Vararg{T,N}}
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed May 18, 2015
1 parent fb574bb commit 2d3e02a
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 60 deletions.
2 changes: 2 additions & 0 deletions base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ call(T::Type{WeakRef}, v::ANY) = Core.call(T, v)
call{T}(::Type{T}, arg) = convert(T, arg)::T
call{T}(::Type{T}, args...) = convert(T, args...)::T

typealias NTuple{N,T} Tuple{Vararg{T,N}}

convert{T}(::Type{T}, x::T) = x

convert(::Type{Tuple{}}, ::Tuple{}) = ()
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export
MathConst,
Matrix,
MergeSort,
NTuple,
Nullable,
ObjectIdDict,
OrdinalRange,
Expand Down
6 changes: 3 additions & 3 deletions base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,15 @@ const getfield_tfunc = function (A, s0, name)
if !isa(s,DataType)
return Any, false
end
if is(s.name,NTuple.name)
return (name == Symbol ? Bottom : s.parameters[2]), true
end
if s.abstract
return Any, false
end
if s <: Tuple && name === Symbol
return Bottom, true
end
# if s <: Tuple && s.parameters.length == 1 && isvatuple(s)
# return s.parameters[1].parameters[1], true
# end
if isa(A[2],QuoteNode) && isa(A[2].value,Symbol)
fld = A[2].value
A1 = A[1]
Expand Down
1 change: 0 additions & 1 deletion src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,6 @@ void jl_init_primitives(void)
add_builtin("TypeName", (jl_value_t*)jl_typename_type);
add_builtin("TypeConstructor", (jl_value_t*)jl_typector_type);
add_builtin("Tuple", (jl_value_t*)jl_anytuple_type);
add_builtin("NTuple", (jl_value_t*)jl_ntuple_type);
add_builtin("Vararg", (jl_value_t*)jl_vararg_type);
add_builtin("Type", (jl_value_t*)jl_type_type);
add_builtin("DataType", (jl_value_t*)jl_datatype_type);
Expand Down
4 changes: 2 additions & 2 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -1979,7 +1979,7 @@ void jl_init_serializer(void)
jl_labelnode_type, jl_linenumbernode_type,
jl_gotonode_type, jl_quotenode_type, jl_topnode_type,
jl_type_type, jl_bottom_type, jl_ref_type, jl_pointer_type,
jl_vararg_type, jl_ntuple_type, jl_abstractarray_type,
jl_vararg_type, jl_abstractarray_type,
jl_densearray_type, jl_box_type, jl_void_type,
jl_typector_type, jl_typename_type,
jl_task_type, jl_uniontype_type, jl_typetype_type, jl_typetype_tvar,
Expand All @@ -1992,7 +1992,7 @@ void jl_init_serializer(void)
jl_datatype_type->name, jl_uniontype_type->name, jl_array_type->name,
jl_expr_type->name, jl_typename_type->name, jl_type_type->name,
jl_methtable_type->name, jl_method_type->name, jl_tvar_type->name,
jl_ntuple_type->name, jl_abstractarray_type->name, jl_vararg_type->name,
jl_abstractarray_type->name, jl_vararg_type->name,
jl_densearray_type->name, jl_void_type->name, jl_lambda_info_type->name,
jl_module_type->name, jl_box_type->name, jl_function_type->name,
jl_typector_type->name, jl_intrinsic_type->name, jl_task_type->name,
Expand Down
74 changes: 26 additions & 48 deletions src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ jl_datatype_t *jl_gensym_type;
jl_datatype_t *jl_simplevector_type;
jl_typename_t *jl_tuple_typename;
jl_tupletype_t *jl_anytuple_type;
jl_datatype_t *jl_ntuple_type;
jl_typename_t *jl_ntuple_typename;
jl_datatype_t *jl_vararg_type;
jl_datatype_t *jl_tvar_type;
jl_datatype_t *jl_uniontype_type;
Expand Down Expand Up @@ -284,17 +282,11 @@ static int needs_translation_tuple(jl_datatype_t *tt);

static int needs_translation(jl_value_t *v)
{
if (jl_is_tuple_type(v)) return needs_translation_tuple((jl_datatype_t*)v);
if (jl_is_typevar(v)) return needs_translation(((jl_tvar_t*)v)->ub) || needs_translation(((jl_tvar_t*)v)->lb);
return jl_is_ntuple_type(v);
return 0;
}

static int needs_translation_data(jl_value_t **data, int n)
{
int i;
for (i = 0; i < n; i++)
if (needs_translation(data[i]))
return 1;
return 0;
}

Expand All @@ -310,34 +302,7 @@ static int needs_translation_tuple(jl_datatype_t *tt)

static jl_value_t *ntuple_translate(jl_value_t *v)
{
jl_value_t *tva = NULL, *result = NULL, *temp = NULL;
JL_GC_PUSH4(&v, &tva, &result, &temp);
if (jl_is_tuple_type(v)) {
result = (jl_value_t*)ntuple_translate_tuple((jl_datatype_t*)v);
JL_GC_POP();
return result;
}
if (jl_is_typevar(v)) {
jl_tvar_t *tv = (jl_tvar_t*) v;
tva = (jl_value_t*)ntuple_translate(tv->lb);
temp = (jl_value_t*)ntuple_translate(tv->ub);
result = (jl_value_t*)jl_new_typevar(tv->name, tva, temp);
((jl_tvar_t*)result)->bound = tv->bound;
JL_GC_POP();
return result;
}
if (!jl_is_ntuple_type(v)) {
JL_GC_POP();
return v;
}
if (v == (jl_value_t*)jl_ntuple_type)
result = (jl_value_t*)jl_anytuple_type;
else {
tva = (jl_value_t*) jl_wrap_vararg(jl_tparam1(v), jl_tparam0(v));
result = (jl_value_t*)jl_tupletype_fill(1, tva);
}
JL_GC_POP();
return result;
return v;
}

static jl_svec_t *ntuple_translate_data(jl_value_t **data, int n)
Expand Down Expand Up @@ -1812,12 +1777,6 @@ jl_value_t *jl_apply_type_(jl_value_t *tc, jl_value_t **params, size_t n)
pi);
}
}
if (tc == (jl_value_t*)jl_ntuple_type && (n==1||n==2) &&
jl_is_long(params[0])) {
size_t nt = jl_unbox_long(params[0]);
return (jl_value_t*)jl_tupletype_fill(nt, (n==2) ? params[1] :
(jl_value_t*)jl_any_type);
}
size_t ntp = jl_svec_len(tp);
if (n > ntp)
jl_errorf("too many parameters for type %s", tname);
Expand Down Expand Up @@ -2252,6 +2211,26 @@ static jl_value_t *inst_tuple_w_(jl_value_t *t, jl_value_t **env, size_t n,
jl_datatype_t *tt = (jl_datatype_t*)t;
jl_svec_t *tp = tt->parameters;
size_t ntp = jl_svec_len(tp);
// Instantiate NTuple{3,Int}
// Note this does not instantiate Tuple{Vararg{Int,3}}; that's done in
// jl_apply_tuple_type_v_
if (jl_is_va_tuple(tt) && ntp == 1 && n == 2) {
// If this is a Tuple{Vararg{T,N}} with known N, expand it to
// a fixed-length tuple
jl_value_t *T=NULL, *N=NULL;
int i;
for (i = 0; i < 2*n; i+=2) {
jl_value_t *tv = env[i];
if (jl_is_typevar(tv)) {
if (((jl_tvar_t*)tv)->name == jl_symbol("T"))
T = env[i+1];
else if (((jl_tvar_t*)tv)->name == jl_symbol("N"))
N = env[i+1];
}
}
if (T != NULL && N != NULL && jl_is_long(N))
return (jl_value_t*)jl_tupletype_fill(jl_unbox_long(N), T);
}
jl_value_t **iparams;
int onstack = ntp < jl_page_size/sizeof(jl_value_t*);
JL_GC_PUSHARGS(iparams, onstack ? ntp : 1);
Expand Down Expand Up @@ -2502,6 +2481,7 @@ static int jl_tuple_subtype_new(jl_value_t **child, size_t clenr,
static int jl_tuple_subtype_(jl_value_t **child, size_t cl,
jl_datatype_t *pdt, int ta, int invariant)
{
/*
jl_svec_t *newchilds=NULL;
jl_datatype_t *newpdt=NULL;
JL_GC_PUSH2(&newchilds, &newpdt);
Expand All @@ -2510,6 +2490,9 @@ static int jl_tuple_subtype_(jl_value_t **child, size_t cl,
int retnew = jl_tuple_subtype_new(jl_svec_data(newchilds), cl, newpdt, ta, invariant);
JL_GC_POP();
return retnew;
*/
return jl_tuple_subtype_new(child, cl, pdt, ta, invariant);

}

int jl_tuple_subtype(jl_value_t **child, size_t cl, jl_datatype_t *pdt, int ta)
Expand Down Expand Up @@ -3414,11 +3397,6 @@ void jl_init_types(void)
(jl_value_t*)jl_bottom_type,(jl_value_t*)jl_any_type);
jl_type_type->parameters = jl_svec(1, tttvar);

tv = jl_svec2(tvar("N"), tvar("T"));
jl_ntuple_type = jl_new_abstracttype((jl_value_t*)jl_symbol("NTuple"),
jl_any_type, tv);
jl_ntuple_typename = jl_ntuple_type->name;

jl_tupletype_t *empty_tuple_type = jl_apply_tuple_type(jl_emptysvec);
jl_emptytuple = ((jl_datatype_t*)empty_tuple_type)->instance;

Expand Down
5 changes: 1 addition & 4 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,6 @@ extern DLLEXPORT jl_datatype_t *jl_simplevector_type;
extern DLLEXPORT jl_typename_t *jl_tuple_typename;
extern DLLEXPORT jl_datatype_t *jl_anytuple_type;
#define jl_tuple_type jl_anytuple_type
extern DLLEXPORT jl_datatype_t *jl_ntuple_type;
extern DLLEXPORT jl_typename_t *jl_ntuple_typename;
extern DLLEXPORT jl_datatype_t *jl_vararg_type;
extern DLLEXPORT jl_datatype_t *jl_tvar_type;
extern DLLEXPORT jl_datatype_t *jl_task_type;
Expand Down Expand Up @@ -871,8 +869,7 @@ STATIC_INLINE int jl_is_tuple_type(void *t)

STATIC_INLINE int jl_is_ntuple_type(jl_value_t *v)
{
return (jl_is_datatype(v) &&
((jl_datatype_t*)v)->name == jl_ntuple_typename);
return 0;
}

STATIC_INLINE int jl_is_type_type(jl_value_t *v)
Expand Down
4 changes: 2 additions & 2 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ end
@test issubtype(Array{Tuple}, Array{NTuple})
@test issubtype(Array{Tuple{Vararg{Any}}}, Array{NTuple})
#@test issubtype(Array{Tuple{Vararg}}, Array{NTuple})
@test !issubtype(Array{Tuple{Vararg{Int}}}, Array{NTuple})
@test !issubtype(Array{Tuple{Int,Int}}, Array{NTuple})
#@test !issubtype(Array{Tuple{Vararg{Int}}}, Array{NTuple})
#@test !issubtype(Array{Tuple{Int,Int}}, Array{NTuple})
@test !issubtype(Type{Tuple{Void}}, Tuple{Type{Void}})

# this is fancy: know that any type T<:Number must be either a DataType or a UnionType
Expand Down

0 comments on commit 2d3e02a

Please sign in to comment.