An efficient cost-based canonical form for Boolean matching