========================================================
Primitive Types
========================================================

object				system.Object		null
void				system.Void			{}
uint8				system.UInt8		0
uint16				system.UInt16		0
uint32	(uint)		system.UInt32		0
uint64				system.UInt64		0
int8				system.Int8			0
int16				system.Int16		0
int32	(int)		system.Int32		0
int64				system.Int64		0
float				system.Single		0.0
double				system.Double		0.0
bool				system.Boolean		false
char				system.Char			'\0'
string				system.String		""

========================================================
Custom Types
========================================================

data PointI
{
	x : int;
	y : int;
}
// default value : {x : default(int), y : default(int)}

data<T> Point
{
	x : T;
	y : T;
}

// {x : 10, y : 20} : Point<int>

data Derived : Base1, Base2
{
}

========================================================
Composite Types
========================================================

data<T, U> Pair
{
	first : T;
	second : U;
}


(T, U, V)			Pair<T, Pair<U, Pair<V, void>>>		{default(T), default(U), default(V)}
T?														null		// T??->T?, T^?->T?, object?->object | null
T^														default(T)	// T?^->T?, T^^->T^, cannot be interface. T should be DataType<T>
T()					Array<T>
T(U)				Map<U, T>
(T, U)=>V			Delegate<(T, U), V>

typeof(Expr)

========================================================
Interfaces
========================================================

interface<T> Disposable
{
	func Dispose();
}

interface<T> Observer
{
	func OnNext(value : T);
	func OnError(error : Exception);
	func OnComplete();
}

interface<T> Observable
{
	func Subscribe(observer : Observer<T>) : Disposable
}

// default value for an interface does not exists
// except the concept DefaultValue is implemented

instance<T> DefaultValue<Enumerable<T>>
{
	func Default() = Enumerable<T>
	{
		func CreateEnumerator() = Enumerator<T>
		{
			get Current : T { throw InvalidOperationException{}; }
			func MoveNext() : bool = false;
		};
	};
}

interface<TArguments, TReturn> Delegate
	where TupleType<TArgs>
{
	get operator()(arguments : TArguments) : TReturn;
}

x=>x+1
(x,y)=>x+y
func (x : int, y : int) : int = x + y
func (x : int, y : int) : int { return x + y; }

// equivalent to the lambda expression above
Delegate<(int, int), int>
{
	get operator()({x, y} : (int, int)) : int
	{
		return x + y;
	}
}

========================================================
Container Interfaces
========================================================

interface<T> Enumerator
{
	get Current : T;
	func MoveNext() : bool;
}

interface<T> Enumerable
{
	func CreateEnumerator() : Enumerator<T>;
}

interface<T> Sequence : Enumerable<T>
{
	get Length : T;
	func SliceFrom(start : int) : Sequence<T>
}

interface<T, U> ReadableMapping : Delegate<(T), U>
{
	get Keys : Enumerable<T>;
	get Values : Enumerable<U>;
	get Pairs : Enumerable<(T, U)>;
}

interface<T> Array : ReadableMapping<int, T>, Sequence<T>
{
	set operator()(index : (int)) : T
	func Slice(start : int, length : int) : Array<T>;
}

interface<T> Set : Sequence<T>
{
	func Contains(value : T) : bool;
	func Add(value : T) : bool;
	func Remove(value : T) : bool;
}

interface<T> List : Array<T>, Collection<T>
{
	func IndexOf(value : T) : int;
	func Insert(index : int, value : T) : bool;
	func RemoveAt(index : int) : bool;
}

interface<T, U> Map : ReadableMapping<T, U>
{
	set operator()(index : (T)) : U
	func ContainsKey(value : T) : bool;
	func Add(key : T, value : U) : int;
	func Remove(key : T) : bool;
}

func<T>				Array(length : int) : Array<T>;
func<T>				ArrayOf(values : Enumerable<T>) : Array<T>;
func<T>				ListOf(values : Enumerable<T>) : List<T>;
func<T>				SortedListOf(values : Enumerable<T>) : List<T>;
func<T>				SortedSetOf(values : Enumerable<T>) : Set<T>;
func<T>				HashSetOf(values : Enumerable<T>) : Set<T>;
func<TKey, TValue>	SortedMapOf(values : Enumerable<(TKey, TValue)>) : Map<TKey, TValue>;
func<TKey, TValue>	HashMapOf(values : Enumerable<(TKey, TValue)>) : Map<TKey, TValue>;

Array(10) : int()
ArrayOf({1, 2, 3, 4, 5})
HashMapOf({{1,1}, {2,4}, {3,9}, {4,16}, {5,25}})
// {1, 2, 3} creates (int, int, int)
// (int, int, int) implicitly converts to Sequence<int>

========================================================
Concepts
========================================================

concept<T> Addable
{
	get Zero : T;
	func operator+ (x : T) : T;
	func operator- (x : T) : T;
	func operator+ (x : T, y : T) : T;
	func operator- (x : T, y : T) : T;
}

concept<T, TDiff> Differentiable
{
	get Zero : TDiff;
	func operator+ (x : T, y : TDiff) : T;
	func operator+ (x : TDiff, y : T) : T;
	func operator- (x : T, y : T) : TDiff;
}

========================================================
Predefined Concepts
========================================================

DefaultValue<T>
SizeOf<T>
Tuplize<T>
Hashable<T>

NotNullType<T>
DataType<T>
InterfaceType<T>
TupleType<T>
InheritFrom<TDerived, TBase>

Convertable<TFrom, TTo>
ImplicitConvertable<TFrom, TTo>

concept Convertable<TFrom, TTo>
	where NotNull<TFrom>, NotNull<TTo>
{
	func TryConvert(value : TFrom) : TTo?;
	func Convert(value : TFrom) : TTo
	{
		if(result = TryConvert(value))
		{
			return result;
		}
		else
		{
			throw InvalidConvertionException{};
		}
	}
}

// all necessary or inherited constraints will be inferred and filled
// e.g. if T^ exists in the declaration body, then Data<T>
// e.g. if Texpr as U exists then Convertable<T, U>
instance<TFrom, TTo> Convertable<TFrom? TTo>
	where Convertable<TFrom, TTo>
{
	func TryConvert(value : TFrom?) : TTo?
	{
		if(expr = value)
		{
			return TryConvert(expr);
		}
		else
		{
			return null;
		}
	}
}

========================================================
Patterns
========================================================

Constant
Identifier		// can only be part of other patterns
X : Type
X^				// extract and convert to T^
X?				// extract and convert to T?
{X, Y, Z}
{X, Y, Z} ++ W
{x:X, y:Y}

========================================================
Expressions
========================================================

Operator:
	++ -- ()
	+ - not
	* / %
	+ -
	shl shr
	== != < <= > >=
	and
	xor
	or

<constructors>
Expr : Type

default(T)					// DefaultValue<T>.Default()
Expr as Type (or Type?)		// Convertable<typeof(Expr), Type>.Convert()/TryConvert()

========================================================
Statements
========================================================

<Pattern> | <LeftValue> | var Name = Expr
return [Expr];
break;
continue;
throw [Expr];

if(Expr) statement [else statement]
if(value = Expr) statement [else statement] // typeof(Expr) = typeof(value)?
for(<Pattern> in Expr) statement
while(Expr) statement
try statement {catch (<pattern>) statement} [finally statement]
switch (Expr) { {case <pattern> : statement} [default : statement] }
using(<pattern> = Expr) statement
lock(Expr) statement

========================================================
Functions
========================================================

func <generic-types> Name <instantiation-types> ( [case]<pattern>, .. ) <generic-requirements> (= Expr | statement )

func<T> WriteStream(this : Stream, value : T); // throw NotImplementedException{}

func WriteStream<int>(this : Stream, value : int);
func WriteStream<string>(this : Stream, value : string);
func<T, U> WriteStream<Pair<T, U>>(this : Stream, value : Pair<T, U>);
func WriteStream<string>(case this : StringStream, value : string);

get/set<T> operator()(this : Enumerable<T>, index : (int));

namespace Name {}

========================================================
Continuations
========================================================

func<T, U> CreateCps(start : (T)=>void) : U;

---------------------------------------------------------

data<T> CpsEnumerator
{
	current			: T;
	available		: bool;
	start			: (CpsEnumerator<T>^)=>void;
	continuation?	: ()=>void;
}

// func<T> CreateCps<CpsEnumerator^, Enumerable<T>>(..):Enumerable<T> is inferred
func<T> CreateCps(start : (CpsEnumerator<T>^)=>void )
{
	return Enumerable<T>
	{
		func CreateEnumerator()
		{
			state? = CpsEnumerator{ start : start };
			return Enumerable<T>
			{
				get Current : T { return state.current; }
				func MoveNext()
				{
					state.available=false;
					if(state.continuation==null)
					{
						state.startContinuation(state);
					}
					else
					{
						state.continuation();
					}
					return state.available;
				}
			};
		}
	};
}

func<T> yield(state : CpsEnumerator<T>^, value : T, continuation : ()=>void)
{
	state.current=value;
	state.available=true;
	state.continuation=continuation;
}

func<T> exit(state : CpsEnumerator<T>^)
{
}

---------------------------------------------------------

func<T> Where(this : Enumerable<T>, predicate : (T)=>bool)
{
	for(s in this)
		if(predicate(s))
			yield! s;
	// return cannot be followed with an expression because exit function does not accept one
}

---------------------------------------------------------

func<T> CreateCps(start : (Promise<T>^)=>void) : Task<T>
{
	return CreateFuturePromiseTask<T>(start);
}

func<T> await(state : Promise<T>^, value : Task<T>, continuation : (T)=>void)
{
	source.ContinueWith(task=>continuation(task.Result);
}

func<T> exit(state : Promose<T>^, value : T)
{
	state.SetResult(T);
}

---------------------------------------------------------

func Find(src)
{
    var urls=(await! DownloadAsync(src)).Split("\r\n");
	for(url in urls)
	{
		var content=await! DownloadAsync(url);
		if(content.Length>100)
		{
			return content;
		}
	}
	return null; // call exit
}