LARA

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
comfusy-examples [2009/11/25 19:39]
philippe.suter
comfusy-examples [2012/05/12 13:11] (current)
losa
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''​
  
-++++click to reveal the code| 
 <code scala> <code scala>
 import synthesis.Definitions._ import synthesis.Definitions._
Line 89: Line 37:
 } }
 </​code>​ </​code>​
-++++ 
  
   * ''​SecondsToTime''​   * ''​SecondsToTime''​
  
-++++click to reveal the code| 
 <code scala> <code scala>
 import synthesis.Definitions._ import synthesis.Definitions._
Line 114: Line 60:
 } }
 </​code>​ </​code>​
-++++ 
  
   * ''​SetConstraints''​   * ''​SetConstraints''​
  
-++++click to reveal the code| 
 <code scala> <code scala>
 import synthesis.Definitions._ import synthesis.Definitions._
Line 142: Line 86:
 } }
 </​code>​ </​code>​
-++++ 
  
   * ''​SplitSetsInABalancedWay''​   * ''​SplitSetsInABalancedWay''​
  
-++++click to reveal the code| 
 <code scala> <code scala>
 import synthesis.Definitions._ import synthesis.Definitions._
Line 173: Line 115:
 } }
 </​code>​ </​code>​
-+++++ 
 +  * ''​FastExponentiation''​ 
 + 
 +<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''​ 
 + 
 +<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]]) 
 + 
 +<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>​