Skip to content

Commit

Permalink
NTuple{N,T} -> Tuple{Vararg{T,N}}
Browse files Browse the repository at this point in the history
This is at least enough to get the build working
  • Loading branch information
timholy committed Apr 26, 2015
1 parent 1f2d034 commit 127fd88
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 17 deletions.
2 changes: 2 additions & 0 deletions base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ call(T::Type{WeakRef}, v::ANY) = Core.call(T, v)

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/datafmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module DataFmt

importall Base
import Base: _default_delims, tryparse_internal
import Base: NTuple

export countlines, readdlm, readcsv, writedlm, writecsv

Expand Down
4 changes: 2 additions & 2 deletions base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ const getfield_tfunc = function (A, s0, name)
if !isa(s,DataType)
return Any
end
if is(s.name,NTuple.name)
return s.parameters[2]
if s <: Tuple && s.types.length == 1 && isvatuple(s)
return s.types[1].parameters[1]
end
if s.abstract
return Any
Expand Down
1 change: 1 addition & 0 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module IteratorsMD
import Base: eltype, length, start, done, next, last, getindex, setindex!, linearindexing, min, max, eachindex
import Base: simd_outer_range, simd_inner_length, simd_index, @generated
import Base: @nref, @ncall, @nif, @nexprs, LinearFast, LinearSlow, to_index
import Base: NTuple

export CartesianIndex, CartesianRange

Expand Down
1 change: 1 addition & 0 deletions base/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ importall Base.LinAlg
import Base.promote_eltype
import Base.@get!
import Base.Broadcast.eltype_plus, Base.Broadcast.broadcast_shape
import Base.NTuple

export AbstractSparseArray, AbstractSparseMatrix, AbstractSparseVector, SparseMatrixCSC,
blkdiag, dense, droptol!, dropzeros!, etree, issparse, nnz, nonzeros, nzrange,
Expand Down
2 changes: 2 additions & 0 deletions src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ typedef struct {
// Note that this function updates len
static jl_value_t *jl_new_bits_internal(jl_value_t *dt, void *data, size_t *len)
{
/*
if (jl_is_ntuple_type(dt)) {
jl_value_t *lenvar = jl_tparam0(dt);
jl_value_t *elty = jl_tparam1(dt);
Expand All @@ -119,6 +120,7 @@ static jl_value_t *jl_new_bits_internal(jl_value_t *dt, void *data, size_t *len)
memcpy(jl_data_ptr(v), data, nb);
return v;
}
*/

assert(jl_is_datatype(dt));
jl_datatype_t *bt = (jl_datatype_t*)dt;
Expand Down
2 changes: 1 addition & 1 deletion src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ 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("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 @@ -1897,7 +1897,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_ntuple_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 @@ -1910,7 +1910,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_ntuple_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
14 changes: 6 additions & 8 deletions src/gf.c
Original file line number Diff line number Diff line change
Expand Up @@ -743,8 +743,7 @@ static jl_function_t *cache_method(jl_methtable_t *mt, jl_tupletype_t *type,
// supertype of any other method signatures. so far we are conservative
// and the types we find should be bigger.
if (!isstaged && jl_nparams(type) > mt->max_args
&& jl_is_va_tuple(decl)
&& !jl_is_vararg_fixedlen(jl_tparam(decl, jl_svec_len(decl->parameters)-1))) {
&& jl_is_va_tuple_varlen(decl)) {
size_t nspec = mt->max_args + 2;
limited = jl_alloc_svec(nspec);
for(i=0; i < nspec-1; i++) {
Expand Down Expand Up @@ -1062,7 +1061,6 @@ static jl_function_t *jl_mt_assoc_by_type(jl_methtable_t *mt, jl_datatype_t *tt,
}
m = m->next;
}

if (ti == (jl_value_t*)jl_bottom_type) {
if (m != (void*)jl_nothing) {
func = m->func;
Expand Down Expand Up @@ -1178,8 +1176,8 @@ static void check_ambiguous(jl_methlist_t *ml, jl_tupletype_t *type,
size_t sl = jl_nparams(sig);
// we know !jl_args_morespecific(type, sig)
if ((tl==sl ||
(tl==sl+1 && jl_is_va_tuple(type)) ||
(tl+1==sl && jl_is_va_tuple(sig))) &&
(tl==sl+1 && jl_is_va_tuple_varlen(type)) ||
(tl+1==sl && jl_is_va_tuple_varlen(sig))) &&
!jl_args_morespecific((jl_value_t*)sig, (jl_value_t*)type)) {
jl_value_t *isect = jl_type_intersection((jl_value_t*)type,
(jl_value_t*)sig);
Expand Down Expand Up @@ -1263,7 +1261,7 @@ jl_methlist_t *jl_method_list_insert(jl_methlist_t **pml, jl_tupletype_t *type,
gc_wb(l, l->sig);
l->tvars = tvars;
gc_wb(l, l->tvars);
l->va = jl_is_va_tuple(type);
l->va = jl_is_va_tuple_varlen(type);
l->isstaged = isstaged;
l->invokes = (struct _jl_methtable_t *)jl_nothing;
l->func = method;
Expand Down Expand Up @@ -1292,7 +1290,7 @@ jl_methlist_t *jl_method_list_insert(jl_methlist_t **pml, jl_tupletype_t *type,
jl_set_typeof(newrec, jl_method_type);
newrec->sig = type;
newrec->tvars = tvars;
newrec->va = jl_is_va_tuple(type);
newrec->va = jl_is_va_tuple_varlen(type);
newrec->isstaged = isstaged;
newrec->func = method;
newrec->invokes = (struct _jl_methtable_t*)jl_nothing;
Expand Down Expand Up @@ -1389,7 +1387,7 @@ jl_methlist_t *jl_method_table_insert(jl_methtable_t *mt, jl_tupletype_t *type,
}
// update max_args
size_t na = jl_nparams(type);
if (jl_is_va_tuple(type))
if (jl_is_va_tuple_varlen(type))
na--;
if (na > mt->max_args)
mt->max_args = na;
Expand Down
41 changes: 39 additions & 2 deletions src/jltypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ 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_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 @@ -503,6 +503,8 @@ static jl_value_t *intersect_tag(jl_datatype_t *a, jl_datatype_t *b,
JL_GC_PUSH1(&p);
jl_value_t *ti;
size_t i;
if (0) {}
/*
if (a->name == jl_ntuple_typename) {
assert(jl_svec_len(p) == 2);
// NOTE: tuples are covariant, so NTuple element type is too
Expand All @@ -516,6 +518,7 @@ static jl_value_t *intersect_tag(jl_datatype_t *a, jl_datatype_t *b,
}
jl_svecset(p, 1, ti);
}
*/
else {
for(i=0; i < jl_svec_len(p); i++) {
jl_value_t *ap = jl_svecref(a->parameters,i);
Expand Down Expand Up @@ -851,6 +854,7 @@ static jl_value_t *jl_type_intersect(jl_value_t *a, jl_value_t *b,
long alen = (long)jl_nparams(a);
jl_value_t *temp=NULL;
JL_GC_PUSH2(&b, &temp);
/*
if (jl_is_ntuple_type(b)) {
has_ntuple_intersect_tuple = 1;
jl_value_t *lenvar = jl_tparam0(b);
Expand Down Expand Up @@ -912,6 +916,7 @@ static jl_value_t *jl_type_intersect(jl_value_t *a, jl_value_t *b,
}
}
}
*/
if (jl_is_type_type(b)) {
jl_value_t *btp0v = jl_tparam0(b);
if (jl_is_typevar(btp0v)) {
Expand All @@ -932,11 +937,13 @@ static jl_value_t *jl_type_intersect(jl_value_t *a, jl_value_t *b,
if (jl_is_tuple_type(b)) {
return jl_type_intersect(b, a, penv,eqc,var);
}
/*
if (jl_is_ntuple_type(a) && jl_is_type_type(b)) {
jl_value_t *temp = a;
a = b;
b = temp;
}
*/
// tag
if (!jl_is_datatype(a) || !jl_is_datatype(b))
return (jl_value_t*)jl_bottom_type;
Expand Down Expand Up @@ -1689,12 +1696,14 @@ 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 @@ -2409,6 +2418,7 @@ static int jl_subtype_le(jl_value_t *a, jl_value_t *b, int ta, int invariant)

if (jl_is_tuple_type(a)) {
if (jl_is_datatype(b)) {
/*
if (((jl_datatype_t*)b)->name == jl_ntuple_typename) {
jl_value_t *tp = jl_tparam1(b);
if (tuple_all_subtype((jl_datatype_t*)a, tp, 0, invariant)) {
Expand All @@ -2420,6 +2430,7 @@ static int jl_subtype_le(jl_value_t *a, jl_value_t *b, int ta, int invariant)
}
return 0;
}
*/
}
if (jl_is_tuple_type(b)) {
return jl_tuple_subtype_(jl_svec_data(((jl_datatype_t*)a)->parameters), jl_nparams(a),
Expand All @@ -2431,6 +2442,7 @@ static int jl_subtype_le(jl_value_t *a, jl_value_t *b, int ta, int invariant)
if (!invariant && (jl_datatype_t*)b == jl_any_type) return 1;

if (jl_is_tuple_type(b)) {
/*
if (jl_is_datatype(a) &&
((jl_datatype_t*)a)->name == jl_ntuple_typename) {
// only ((T>:S)...,) can be a supertype of NTuple{N,S}
Expand All @@ -2439,6 +2451,7 @@ static int jl_subtype_le(jl_value_t *a, jl_value_t *b, int ta, int invariant)
return jl_subtype_le(ntp, jl_tparam0(jl_tparam0(b)), 0, invariant);
}
}
*/
return 0;
}

Expand All @@ -2449,10 +2462,12 @@ static int jl_subtype_le(jl_value_t *a, jl_value_t *b, int ta, int invariant)
int super=0;
while (tta != (jl_datatype_t*)jl_any_type) {
if (tta->name == ttb->name) {
/*
if (tta->name == jl_ntuple_typename) {
// NTuple must be covariant
return jl_subtype_le(jl_tparam(tta,1), jl_tparam(ttb,1), 0, invariant);
}
*/
if (super && ttb->name == jl_type_type->name && jl_is_typevar(jl_tparam0(b))) {
if (jl_subtype_le(a, jl_tparam0(b), 0, 1))
return 1;
Expand Down Expand Up @@ -2613,10 +2628,12 @@ static int jl_type_morespecific_(jl_value_t *a, jl_value_t *b, int invariant)
}
size_t i;
if (jl_is_tuple_type(a)) {
/*
if (jl_is_datatype(b) &&
((jl_datatype_t*)b)->name == jl_ntuple_typename) {
return tuple_all_morespecific((jl_datatype_t*)a, jl_tparam(b,1), invariant);
}
*/
if (jl_is_tuple_type(b)) {
return jl_tuple_morespecific_((jl_datatype_t*)a, (jl_datatype_t*)b, invariant);
}
Expand Down Expand Up @@ -2677,6 +2694,7 @@ static int jl_type_morespecific_(jl_value_t *a, jl_value_t *b, int invariant)
if (!invariant && (jl_datatype_t*)b == jl_any_type) return 1;

if (jl_is_tuple_type(b)) {
/*
if (jl_is_datatype(a) &&
((jl_datatype_t*)a)->name == jl_ntuple_typename) {
// only ((T>:S)...,) can be a supertype of NTuple[N,S]
Expand All @@ -2685,6 +2703,7 @@ static int jl_type_morespecific_(jl_value_t *a, jl_value_t *b, int invariant)
return jl_type_morespecific_(ntp, jl_tparam0(jl_tparam0(b)), invariant);
}
}
*/
return 0;
}

Expand All @@ -2699,10 +2718,12 @@ static int jl_type_morespecific_(jl_value_t *a, jl_value_t *b, int invariant)
if (tta->name != jl_type_type->name)
return 1;
}
/*
if (tta->name == jl_ntuple_typename) {
// NTuple must be covariant
return jl_type_morespecific_(jl_tparam(tta,1), jl_tparam(ttb,1), invariant);
}
*/
if (super && ttb->name == jl_type_type->name && jl_is_typevar(jl_tparam0(b))) {
if (jl_type_morespecific_(a, jl_tparam0(b), 1))
return 1;
Expand Down Expand Up @@ -2948,7 +2969,18 @@ static jl_value_t *type_match_(jl_value_t *child, jl_value_t *parent,
return jl_false;
}

if (jl_is_tuple_type(child) && jl_nparams(child) == 1 &&
jl_is_vararg_type(jl_tparam(child,0)) &&
jl_is_tuple_type(parent) && jl_nparams(parent) == 1 &&
jl_is_vararg_type(jl_tparam(parent,0))) {
// We're matching Tuple{Vararg{S,M}} against Tuple{Vararg{T,N}}
// Use the DataType rules rather than the tuple rules
child = jl_tparam(child, 0);
parent = jl_tparam(parent,0);
}

if (jl_is_tuple_type(child)) {
/*
if (jl_is_datatype(parent) &&
((jl_datatype_t*)parent)->name == jl_ntuple_typename) {
jl_svec_t *tp = ((jl_datatype_t*)parent)->parameters;
Expand Down Expand Up @@ -2976,6 +3008,7 @@ static jl_value_t *type_match_(jl_value_t *child, jl_value_t *parent,
JL_GC_POP();
return tmp;
}
*/

if (jl_is_tuple_type(parent)) {
return tuple_match((jl_datatype_t*)child, (jl_datatype_t*)parent, env,
Expand All @@ -2984,6 +3017,7 @@ static jl_value_t *type_match_(jl_value_t *child, jl_value_t *parent,
return jl_false;
}
if (jl_is_tuple_type(parent)) {
/*
if (jl_is_datatype(child) &&
((jl_datatype_t*)child)->name == jl_ntuple_typename) {
// only ((T>:S)...,) can be a supertype of NTuple[N,S]
Expand All @@ -2992,6 +3026,7 @@ static jl_value_t *type_match_(jl_value_t *child, jl_value_t *parent,
return type_match_(ntp, jl_tparam0(jl_tparam0(parent)), env, morespecific, invariant);
}
}
*/
return jl_false;
}

Expand Down Expand Up @@ -3223,10 +3258,12 @@ 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
15 changes: 13 additions & 2 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,8 @@ 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_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 @@ -869,11 +869,13 @@ STATIC_INLINE int jl_is_va_tuple(jl_datatype_t *t)
return (l>0 && jl_is_vararg_type(jl_tparam(t,l-1)));
}

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

STATIC_INLINE int jl_is_type_type(jl_value_t *v)
{
Expand Down Expand Up @@ -1008,6 +1010,15 @@ STATIC_INLINE int jl_is_vararg_fixedlen(jl_value_t *v)
return jl_is_long(lenv);
}

STATIC_INLINE int jl_is_va_tuple_varlen(jl_datatype_t *t)
{
size_t l = jl_svec_len(t->parameters);
if (l == 0)
return 0;
jl_value_t *last = jl_tparam(t,l-1);
return jl_is_vararg_type(last) && !jl_is_vararg_fixedlen(last);
}

// structs
DLLEXPORT int jl_field_index(jl_datatype_t *t, jl_sym_t *fld, int err);
DLLEXPORT jl_value_t *jl_get_nth_field(jl_value_t *v, size_t i);
Expand Down

0 comments on commit 127fd88

Please sign in to comment.