From 127fd888e87745f4bab0e2c4e7fba2ba5c0c6e91 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Sat, 25 Apr 2015 08:08:15 -0500 Subject: [PATCH] NTuple{N,T} -> Tuple{Vararg{T,N}} This is at least enough to get the build working --- base/base.jl | 2 ++ base/datafmt.jl | 1 + base/inference.jl | 4 ++-- base/multidimensional.jl | 1 + base/sparse.jl | 1 + src/alloc.c | 2 ++ src/builtins.c | 2 +- src/dump.c | 4 ++-- src/gf.c | 14 ++++++-------- src/jltypes.c | 41 ++++++++++++++++++++++++++++++++++++++-- src/julia.h | 15 +++++++++++++-- 11 files changed, 70 insertions(+), 17 deletions(-) diff --git a/base/base.jl b/base/base.jl index e276d68b05351..7035fe7579593 100644 --- a/base/base.jl +++ b/base/base.jl @@ -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{}) = () diff --git a/base/datafmt.jl b/base/datafmt.jl index 1a0db96a24805..13201fe8c9947 100644 --- a/base/datafmt.jl +++ b/base/datafmt.jl @@ -4,6 +4,7 @@ module DataFmt importall Base import Base: _default_delims, tryparse_internal +import Base: NTuple export countlines, readdlm, readcsv, writedlm, writecsv diff --git a/base/inference.jl b/base/inference.jl index 08f4348be48c5..ab380557fbc47 100644 --- a/base/inference.jl +++ b/base/inference.jl @@ -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 diff --git a/base/multidimensional.jl b/base/multidimensional.jl index ce0af0a76199a..1bde236fd7fb4 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -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 diff --git a/base/sparse.jl b/base/sparse.jl index 1c391d58f6840..7a735c4826d3b 100644 --- a/base/sparse.jl +++ b/base/sparse.jl @@ -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, diff --git a/src/alloc.c b/src/alloc.c index c381fb6bed204..f0b0fd446748e 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -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); @@ -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; diff --git a/src/builtins.c b/src/builtins.c index 1c736129170fe..a20253197db8a 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -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); diff --git a/src/dump.c b/src/dump.c index 1b68a5714add5..7892099174f2d 100644 --- a/src/dump.c +++ b/src/dump.c @@ -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, @@ -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, diff --git a/src/gf.c b/src/gf.c index 196451cc6fff7..35a5282d958e5 100644 --- a/src/gf.c +++ b/src/gf.c @@ -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++) { @@ -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; @@ -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); @@ -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; @@ -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; @@ -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; diff --git a/src/jltypes.c b/src/jltypes.c index ad4103492df1e..a4fe7cf927455 100644 --- a/src/jltypes.c +++ b/src/jltypes.c @@ -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; @@ -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 @@ -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); @@ -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); @@ -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)) { @@ -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; @@ -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); @@ -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)) { @@ -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), @@ -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} @@ -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; } @@ -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; @@ -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); } @@ -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] @@ -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; } @@ -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; @@ -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; @@ -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, @@ -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] @@ -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; } @@ -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; diff --git a/src/julia.h b/src/julia.h index b33082a1ee54e..53228f4dc2004 100644 --- a/src/julia.h +++ b/src/julia.h @@ -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; @@ -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) { @@ -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);