Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
comfusy-examples [2009/11/25 19:39] philippe.suter |
comfusy-examples [2010/01/17 11:38] philippe.suter |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== COMFUSY: COMplete FUnctional SYnthesis - Examples ====== | ====== COMFUSY: COMplete FUnctional SYnthesis - Examples ====== | ||
- | |||
- | * ''FastExponentiation'' | ||
- | |||
- | ++++click to reveal the code| | ||
- | <code scala> | ||
- | import synthesis.Definitions._ | ||
- | |||
- | object FastExponentiation { | ||
- | def pow(base: Int, p: Int) = { | ||
- | def fp(m : Int, b : Int, i : Int) : Int = i match { | ||
- | case 0 => m | ||
- | case 2 * j => fp(m, b*b, j) | ||
- | case 2 * j + 1 => fp(m*b, b*b, j) | ||
- | } | ||
- | fp(1, base, p) | ||
- | } | ||
- | |||
- | def main(args : Array[String]) : Unit = { | ||
- | println("Base ?") | ||
- | val bse = Console.readInt | ||
- | println("Exponent ?") | ||
- | val exp = Console.readInt | ||
- | println(bse + "^" + exp + " = " + pow(bse,exp)) | ||
- | } | ||
- | } | ||
- | </code> | ||
- | ++++ | ||
- | |||
- | * ''PrimeHeuristic'' | ||
- | |||
- | ++++click to reveal the code| | ||
- | <code scala> | ||
- | import synthesis.Definitions._ | ||
- | |||
- | object PrimeHeuristic { | ||
- | def maybePrime(n: Int): Boolean = n match { | ||
- | case 2 * k => false | ||
- | case 3 * k => false | ||
- | case 6 * k - 1 => true | ||
- | case 6 * k + 1 => true | ||
- | } | ||
- | |||
- | def main(args : Array[String]) : Unit = { | ||
- | println("Number !") | ||
- | val x = Console.readInt | ||
- | |||
- | println("Any chance that " + x + " is prime ? " + maybePrime(x)) | ||
- | } | ||
- | } | ||
- | </code> | ||
- | ++++ | ||
* ''ScaleWeights'' | * ''ScaleWeights'' | ||
Line 171: | Line 120: | ||
println(setB) | println(setB) | ||
} | } | ||
+ | } | ||
+ | </code> | ||
+ | ++++ | ||
+ | |||
+ | * ''FastExponentiation'' | ||
+ | |||
+ | ++++click to reveal the code| | ||
+ | <code scala> | ||
+ | import synthesis.Definitions._ | ||
+ | |||
+ | object FastExponentiation { | ||
+ | def pow(base: Int, p: Int) = { | ||
+ | def fp(m : Int, b : Int, i : Int) : Int = i match { | ||
+ | case 0 => m | ||
+ | case 2 * j => fp(m, b*b, j) | ||
+ | case 2 * j + 1 => fp(m*b, b*b, j) | ||
+ | } | ||
+ | fp(1, base, p) | ||
+ | } | ||
+ | |||
+ | def main(args : Array[String]) : Unit = { | ||
+ | println("Base ?") | ||
+ | val bse = Console.readInt | ||
+ | println("Exponent ?") | ||
+ | val exp = Console.readInt | ||
+ | println(bse + "^" + exp + " = " + pow(bse,exp)) | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | ++++ | ||
+ | |||
+ | * ''PrimeHeuristic'' | ||
+ | |||
+ | ++++click to reveal the code| | ||
+ | <code scala> | ||
+ | import synthesis.Definitions._ | ||
+ | |||
+ | object PrimeHeuristic { | ||
+ | def maybePrime(n: Int): Boolean = n match { | ||
+ | case 2 * k => false | ||
+ | case 3 * k => false | ||
+ | case 6 * k - 1 => true | ||
+ | case 6 * k + 1 => true | ||
+ | } | ||
+ | |||
+ | def main(args : Array[String]) : Unit = { | ||
+ | println("Number !") | ||
+ | val x = Console.readInt | ||
+ | |||
+ | println("Any chance that " + x + " is prime ? " + maybePrime(x)) | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | ++++ | ||
+ | |||
+ | * ''NewYearSong'' (similar code with fewer variables was used to compute the distribution of names in [[http://www.youtube.com/watch?v=E2aPFdu0FNA|this song]]) | ||
+ | |||
+ | ++++click to reveal the code| | ||
+ | |||
+ | <code scala> | ||
+ | import synthesis.Definitions._ | ||
+ | object NewYearSong { | ||
+ | def main(args : Array[String]) : Unit = { | ||
+ | println("eLines (6) ?") | ||
+ | val eLines: Int = Console.readInt | ||
+ | println("iLines (2) ?") | ||
+ | val iLines: Int = Console.readInt | ||
+ | println("iSyls (5) ?") | ||
+ | val iSyls: Int = Console.readInt | ||
+ | println("nSyls (952) ?") | ||
+ | val nSyls: Int = Console.readInt | ||
+ | |||
+ | val (line7, line8, nsLines, nLines, tLines, _) = | ||
+ | choose((line7: Int, line8: Int, nsLines: Int, nLines: Int, tLines: Int, tLinesFact: Int) => ( | ||
+ | tLines == eLines + nLines | ||
+ | && nLines == iLines + nsLines | ||
+ | && nLines == line7 + line8 | ||
+ | && nSyls + iSyls == 7 * line7 + 8 * line8 | ||
+ | && tLines == 4 * tLinesFact // expresses (4 | tLines) | ||
+ | && line8 >= 0 | ||
+ | && line7 >= 0 | ||
+ | && tLines >= 0 | ||
+ | )) | ||
+ | |||
+ | println("line7 : " + line7) | ||
+ | println("line8 : " + line8) | ||
+ | println("nsLines : " + nsLines) | ||
+ | println("nLines : " + nLines) | ||
+ | println("tLines : " + tLines) | ||
+ | } | ||
} | } | ||
</code> | </code> | ||
++++ | ++++ |