11use super :: * ;
22
33#[ derive( Clone , Copy , Debug ) ]
4- pub enum Variable {
5- Public ( u32 ) ,
6- Private ( u32 ) ,
4+ pub enum Input {
5+ Variable ( Variable ) ,
6+ Public ( usize ) ,
7+ Private ( usize ) ,
8+ }
9+
10+ #[ derive( Clone , Copy , Debug ) ]
11+ pub struct Variable {
12+ pub label : usize ,
713}
814
915#[ derive( Clone , Copy , Debug ) ]
@@ -12,64 +18,65 @@ pub enum Expression<ExpL, ExpR> {
1218 Mul ( ExpL , ExpR ) ,
1319}
1420
15- impl Add for Variable {
16- type Output = Expression < Variable , Variable > ;
21+ impl Add for Input {
22+ type Output = Expression < Input , Input > ;
1723
1824 fn add ( self , rhs : Self ) -> Self :: Output { Expression :: Add ( self , rhs) }
1925}
2026
21- impl < ExpL , ExpR > Add < Expression < ExpL , ExpR > > for Variable {
22- type Output = Expression < Variable , Expression < ExpL , ExpR > > ;
27+ impl < ExpL , ExpR > Add < Expression < ExpL , ExpR > > for Input {
28+ type Output = Expression < Input , Expression < ExpL , ExpR > > ;
2329
2430 fn add ( self , rhs : Expression < ExpL , ExpR > ) -> Self :: Output { Expression :: Add ( self , rhs) }
2531}
2632
27- impl < ExpL , ExpR > Add < Variable > for Expression < ExpL , ExpR > {
28- type Output = Expression < Expression < ExpL , ExpR > , Variable > ;
33+ impl < ExpL , ExpR > Add < Input > for Expression < ExpL , ExpR > {
34+ type Output = Expression < Expression < ExpL , ExpR > , Input > ;
2935
30- fn add ( self , rhs : Variable ) -> Self :: Output { Expression :: Add ( self , rhs) }
36+ fn add ( self , rhs : Input ) -> Self :: Output { Expression :: Add ( self , rhs) }
3137}
3238
33- impl < EXP1 , EXP2 , EXP3 , EXP4 > Add < Expression < EXP3 , EXP4 > > for Expression < EXP1 , EXP2 > {
34- type Output = Expression < Expression < EXP1 , EXP2 > , Expression < EXP3 , EXP4 > > ;
39+ impl < ExpL1 , ExpR1 , ExpL2 , ExpR2 > Add < Expression < ExpL2 , ExpR2 > > for Expression < ExpL1 , ExpR1 > {
40+ type Output = Expression < Expression < ExpL1 , ExpR1 > , Expression < ExpL2 , ExpR2 > > ;
3541
36- fn add ( self , rhs : Expression < EXP3 , EXP4 > ) -> Self :: Output { Expression :: Add ( self , rhs) }
42+ fn add ( self , rhs : Expression < ExpL2 , ExpR2 > ) -> Self :: Output { Expression :: Add ( self , rhs) }
3743}
3844
39- impl Mul for Variable {
40- type Output = Expression < Variable , Variable > ;
45+ impl Mul for Input {
46+ type Output = Expression < Input , Input > ;
4147
4248 fn mul ( self , rhs : Self ) -> Self :: Output { Expression :: Mul ( self , rhs) }
4349}
4450
45- impl < ExpL , ExpR > Mul < Expression < ExpL , ExpR > > for Variable {
46- type Output = Expression < Variable , Expression < ExpL , ExpR > > ;
51+ impl < ExpL , ExpR > Mul < Expression < ExpL , ExpR > > for Input {
52+ type Output = Expression < Input , Expression < ExpL , ExpR > > ;
4753
4854 fn mul ( self , rhs : Expression < ExpL , ExpR > ) -> Self :: Output { Expression :: Mul ( self , rhs) }
4955}
5056
51- impl < ExpL , ExpR > Mul < Variable > for Expression < ExpL , ExpR > {
52- type Output = Expression < Expression < ExpL , ExpR > , Variable > ;
57+ impl < ExpL , ExpR > Mul < Input > for Expression < ExpL , ExpR > {
58+ type Output = Expression < Expression < ExpL , ExpR > , Input > ;
5359
54- fn mul ( self , rhs : Variable ) -> Self :: Output { Expression :: Mul ( self , rhs) }
60+ fn mul ( self , rhs : Input ) -> Self :: Output { Expression :: Mul ( self , rhs) }
5561}
5662
57- impl < EXP1 , EXP2 , EXP3 , EXP4 > Mul < Expression < EXP3 , EXP4 > > for Expression < EXP1 , EXP2 > {
58- type Output = Expression < Expression < EXP1 , EXP2 > , Expression < EXP3 , EXP4 > > ;
63+ impl < ExpL1 , ExpR1 , ExpL2 , ExpR2 > Mul < Expression < ExpL2 , ExpR2 > > for Expression < ExpL1 , ExpR1 > {
64+ type Output = Expression < Expression < ExpL1 , ExpR1 > , Expression < ExpL2 , ExpR2 > > ;
5965
60- fn mul ( self , rhs : Expression < EXP3 , EXP4 > ) -> Self :: Output { Expression :: Mul ( self , rhs) }
66+ fn mul ( self , rhs : Expression < ExpL2 , ExpR2 > ) -> Self :: Output { Expression :: Mul ( self , rhs) }
6167}
6268
63- impl Display for Variable {
69+ impl Display for Input {
6470 fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
6571 match self {
66- Variable :: Public ( val) => write ! ( f, "{}" , val) ,
67- Variable :: Private ( val) => write ! ( f, "{}" , val) ,
72+ Input :: Variable ( val) => write ! ( f, "x_{}" , val. label) ,
73+ Input :: Public ( val) => write ! ( f, "{}" , val) ,
74+ Input :: Private ( val) => write ! ( f, "{}" , val) ,
6875 }
6976 }
7077}
7178
72- impl < EXPL : Display , EXPR : Display > Display for Expression < EXPL , EXPR > {
79+ impl < ExpL : Display , ExpR : Display > Display for Expression < ExpL , ExpR > {
7380 fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
7481 match self {
7582 Expression :: Add ( left, right) => write ! ( f, "({} + {})" , left, right) ,
@@ -85,8 +92,9 @@ mod tests {
8592 #[ test]
8693 fn writing_a_program ( ) {
8794 // Create two variables
88- let a = Variable :: Public ( 7 ) ;
89- let b = Variable :: Private ( 3 ) ;
95+ let a = Input :: Public ( 7 ) ;
96+ let b = Input :: Private ( 3 ) ;
97+ let x = Input :: Variable ( Variable { label : 0 } ) ;
9098
9199 // Create basic expressions with these variables
92100 let add_ab = a + b;
@@ -108,5 +116,9 @@ mod tests {
108116 // Check that we can add two expressions together
109117 println ! ( "{}" , add_ab + mul_ab) ;
110118 assert_eq ! ( format!( "{}" , add_ab + mul_ab) , "((7 + 3) + (7 * 3))" ) ;
119+
120+ // Check that we can multiply an expression by a variable
121+ println ! ( "{}" , mul_ab * x) ;
122+ assert_eq ! ( format!( "{}" , mul_ab * x) , "((7 * 3) * x_0)" ) ;
111123 }
112124}
0 commit comments