www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - RFC: std.concepts

reply Shammah Chancellor <anonymous coward.com> writes:
It was request that I create a NG thread about a module I was hoping to 
merge with phobos. (std.concepts)  Please take a look.

Thanks in advance.

-S

Link to PR:

https://github.com/D-Programming-Language/phobos/pull/2627

Docs:


bool isConcept(T, C, bool diagnostics = false, string errorPrefix = "")();
Returns true if T supports the concept C. Note, templated member 
functions are not supported currently.

Concepts should be defined as in the following example:
----
class CInputRange(E) : Concept
{
	abstract void popFront();
	 property abstract E front();
	bool empty;

	//Optional axioms function.  This will be checked if it compiles, and 
that it returns true if it does.
	static bool axioms(T)()
	{
		return true;
	}
}

class CInfinite() : Concept
{
	static bool axioms(T)() {
		enum empty = T.empty;
		return !empty;
	}
}

class CInfiniteInputRange(E) : CInputRange!E
{
	mixin CInfinite;
}
---

template conceptDiagnostic(R, string F, int L, C...)
Prints error messages for why template instantiation failed.

Examples:
---
bool DoStuff(R)(R infRange) if ( isConcept!(R, CInfiniteInputRange!string))
{
	return true;
}

bool DoStuff(R)(R infRange) if ( isConcept!(R, COutputRange!string))
{
	return true;
}

//Example of using conceptDiagnostic
bool DoStuff(R, string F = __FILE__, size_t L = __LINE__ )(R infRange)
{
	mixin conceptDiagnostic!(R, F, L, COutputRange!string, 
CInfiniteInputRange!string);
	return false;
}
---

class Concept;
Base class for Concepts. All Concepts should derive from this, or 
another concept.
Oct 19 2014
next sibling parent reply "Hpkl" <Hpkl nowhere.gr> writes:
On Sunday, 19 October 2014 at 21:10:12 UTC, Shammah Chancellor 
wrote:
 It was request that I create a NG thread about a module I was 
 hoping to merge with phobos. (std.concepts)  Please take a look.

 Thanks in advance.
So basically it allows, at compile time, to check that an agregate contains all the "primitive" methods defined in a concept, and eventually it diagnoses accurately which axiom is missing ? right ? e.g the the assertion in http://dpaste.dzfl.pl/08f740ac5e48 would be correct ?
Oct 19 2014
parent reply Shammah Chancellor <email domain.com> writes:
On 2014-10-19 22:14:29 +0000, Hpkl said:

 On Sunday, 19 October 2014 at 21:10:12 UTC, Shammah Chancellor wrote:
 It was request that I create a NG thread about a module I was hoping to 
 merge with phobos. (std.concepts)  Please take a look.
 
 Thanks in advance.
 
So basically it allows, at compile time, to check that an agregate contains all the "primitive" methods defined in a concept, and eventually it diagnoses accurately which axiom is missing ? right ? e.g the the assertion in http://dpaste.dzfl.pl/08f740ac5e48 would be correct ?
It can't give messages about the particular axioms that fail without spliting them up into individual axioms -- and that's definitely something to consider. However, axioms are usually fairly rare in concepts AFAIK. You can instead write the following, and it will tell you what's missing. class CLutFunc1: Concept { void prepare(in uint sampling); float get(in float x); } -S.
Oct 19 2014
parent "Hpkl" <Hpkl nowhere.gr> writes:
On Sunday, 19 October 2014 at 22:39:06 UTC, Shammah Chancellor 
wrote:
 On 2014-10-19 22:14:29 +0000, Hpkl said:

 On Sunday, 19 October 2014 at 21:10:12 UTC, Shammah Chancellor 
 wrote:
 It was request that I create a NG thread about a module I was 
 hoping to merge with phobos. (std.concepts)  Please take a 
 look.
 
 Thanks in advance.
 
So basically it allows, at compile time, to check that an agregate contains all the "primitive" methods defined in a concept, and eventually it diagnoses accurately which axiom is missing ? right ? e.g the the assertion in http://dpaste.dzfl.pl/08f740ac5e48 would be correct ?
It can't give messages about the particular axioms that fail without spliting them up into individual axioms -- and that's definitely something to consider. However, axioms are usually fairly rare in concepts AFAIK. You can instead write the following, and it will tell you what's missing. class CLutFunc1: Concept { void prepare(in uint sampling); float get(in float x); } -S.
Sorry, I'm not an english native speaker. When I've used the word "axiom" I meant "trait". It seems that what's called an "axiom" is made of "traits". Sorry about this semanthic confusion. Actually I get what you call a concept, I've just used the wrong word. My bad.
Oct 20 2014
prev sibling next sibling parent "Chris Williams" <yoreanon-chrisw yahoo.co.jp> writes:
On Sunday, 19 October 2014 at 21:10:12 UTC, Shammah Chancellor 
wrote:
 It was request that I create a NG thread about a module I was 
 hoping to merge with phobos. (std.concepts)  Please take a look.
It seems like it might have some good use cases, but I'm not sure that it warrants a new module. I would probably recommend putting it into std.traits, as a more advanced version of the isX range of functionality.
Oct 20 2014
prev sibling next sibling parent Leandro Motta Barros via Digitalmars-d <digitalmars-d puremagic.com> writes:
Looks very nice! I think I could make use of it. (Though I use D only
recreationally :-) )

LMB


On Sun, Oct 19, 2014 at 7:10 PM, Shammah Chancellor via Digitalmars-d <
digitalmars-d puremagic.com> wrote:

 It was request that I create a NG thread about a module I was hoping to
 merge with phobos. (std.concepts)  Please take a look.

 Thanks in advance.

 -S

 Link to PR:

 https://github.com/D-Programming-Language/phobos/pull/2627

 Docs:


 bool isConcept(T, C, bool diagnostics = false, string errorPrefix = "")();
 Returns true if T supports the concept C. Note, templated member functions
 are not supported currently.

 Concepts should be defined as in the following example:
 ----
 class CInputRange(E) : Concept
 {
         abstract void popFront();
          property abstract E front();
         bool empty;

         //Optional axioms function.  This will be checked if it compiles,
 and that it returns true if it does.
         static bool axioms(T)()
         {
                 return true;
         }
 }

 class CInfinite() : Concept
 {
         static bool axioms(T)() {
                 enum empty = T.empty;
                 return !empty;
         }
 }

 class CInfiniteInputRange(E) : CInputRange!E
 {
         mixin CInfinite;
 }
 ---

 template conceptDiagnostic(R, string F, int L, C...)
 Prints error messages for why template instantiation failed.

 Examples:
 ---
 bool DoStuff(R)(R infRange) if ( isConcept!(R, CInfiniteInputRange!string))
 {
         return true;
 }

 bool DoStuff(R)(R infRange) if ( isConcept!(R, COutputRange!string))
 {
         return true;
 }

 //Example of using conceptDiagnostic
 bool DoStuff(R, string F = __FILE__, size_t L = __LINE__ )(R infRange)
 {
         mixin conceptDiagnostic!(R, F, L, COutputRange!string,
 CInfiniteInputRange!string);
         return false;
 }
 ---

 class Concept;
 Base class for Concepts. All Concepts should derive from this, or another
 concept.
Oct 20 2014
prev sibling parent "Dicebot" <public dicebot.lv> writes:
It is important to leave possibility for compiler to verify that 
restricted types are used only in compliance with a concept. I am 
thinking about moving some basic primitives to druntime and 
designing it in a bit more light-weight way - for example, I 
don't like usage of Concept base class here.

Overall this is a very promising direction to develop but getting 
it to the point where it can be included into Phobos and 
advertised can be much more tricky. For now it can be useful as a 
dub package to experiment with.

I hope to provide more detailed feedback after investigating 
implementation closer.
Oct 24 2014