//	BoolLevel.txt


bool scaneRowOfIsBool(row){
	for(i=0;i<row->size();++i)if(typeof((*row)[i])!="bool")return 0;
	return 1;
}


var getBasisPredecatsTypes(){
	Q={"void","bool","int","float","double","digit","string","char","vector","set","map","program","graph","function"};
	R={};
	for(i=0;i<Q.size();++i)R.push(PROGRAM("typeof(x)=="+Q[i].export()));
	Q={"int","float","double","digit"};
	R.push(PROGRAM(Q.export()+".find(typeof(x))>=0"));
	R.push(PROGRAM("((set)v).size()<2"));
	R.push(PROGRAM("((set)v).size()==1"));
	return R;
}



bool BoolLevel(sys){
	if(!scaneRowOfIsBool(&sys->outputs))return 0;
	R=getBasisPredecatsTypes();
	if(sys->namesinput.size()==1){
		SovpaloP={};
		for(i=0;i<R.size();++i){
			P=R[i];
			names=P.getNames(1);
			P.replace(names[0],"U");
			P2=PROGRAM("!#");
			P2.setSub({0},P);
			Result={};
			Result2={};
			for(t=0;t<sys->inputs.size();++t){
				U=sys->inputs[t];
				Result.push(eval(P));
				Result2.push(eval(P2));
				}
			if(Result!=sys->outputs && Result2!=sys->outputs)continue;
			if(Result2==sys->outputs)P=P2;
			SovpaloP.push(P);
			}
		if(SovpaloP.size()){
			Powers={};
			for(i=0;i<SovpaloP.size();++i)Powers.push(SovpaloP[i].getPower());
			P=SovpaloP[Powers.find(Powers.min())];
			P.replace("U",sys->namesinput[0]);
			P2=PROGRAM("return #");
			P2.setSub({0},P);
			Xfunct={
				name:(*sys)["имя программы"],
				program:P2,
				inputnames:sys->namesinput,
				outputnames:sys->namesoutput[0]
				};
			sys->functions.push(Xfunct);
			return 1;
			}
		}
	//trace(sys->outputs.export());

	return 0;
}

