@@ -7,7 +7,7 @@ use std::sync::Arc;
77use either:: Either ;
88use simplicity:: jet:: Elements ;
99use simplicity:: node:: { CoreConstructible as _, JetConstructible as _} ;
10- use simplicity:: { Cmr , FailEntropy } ;
10+ use simplicity:: { types , Cmr , FailEntropy } ;
1111
1212use self :: builtins:: array_fold;
1313use crate :: array:: { BTreeSlice , Partition } ;
@@ -26,7 +26,7 @@ use crate::value::StructuralValue;
2626use crate :: witness:: Arguments ;
2727use crate :: Value ;
2828
29- type ProgNode = Arc < named:: ConstructNode < Elements > > ;
29+ type ProgNode < ' brand > = Arc < named:: ConstructNode < ' brand , Elements > > ;
3030
3131/// Each SimplicityHL expression expects an _input value_.
3232/// A SimplicityHL expression is translated into a Simplicity expression
@@ -66,15 +66,12 @@ struct Scope<'brand> {
6666 /// Later assignments occur higher in the tree than earlier assignments.
6767 /// ```
6868 variables : Vec < Vec < Pattern > > ,
69- ctx : simplicity:: types:: Context ,
69+ ctx : simplicity:: types:: Context < ' brand > ,
7070 /// Tracker of function calls.
7171 call_tracker : Arc < CallTracker > ,
7272 /// Values for parameters inside the SimplicityHL program.
7373 arguments : Arguments ,
7474 include_debug_symbols : bool ,
75- // In the next commits, this will be dropped since the 'brand
76- // tag will be moved into 'ctx'.
77- phantom : std:: marker:: PhantomData < fn ( & ' brand ( ) ) -> & ' brand ( ) > ,
7875}
7976
8077impl < ' brand > Scope < ' brand > {
@@ -87,17 +84,17 @@ impl<'brand> Scope<'brand> {
8784 /// The supplied `arguments` are consistent with the program's parameters.
8885 /// Call [`Arguments::is_consistent`] before calling this method!
8986 pub fn new (
87+ ctx : simplicity:: types:: Context < ' brand > ,
9088 call_tracker : Arc < CallTracker > ,
9189 arguments : Arguments ,
9290 include_debug_symbols : bool ,
9391 ) -> Self {
9492 Self {
9593 variables : vec ! [ vec![ Pattern :: Ignore ] ] ,
96- ctx : simplicity :: types :: Context :: new ( ) ,
94+ ctx,
9795 call_tracker,
9896 arguments,
9997 include_debug_symbols,
100- phantom : core:: marker:: PhantomData ,
10198 }
10299 }
103100
@@ -109,7 +106,6 @@ impl<'brand> Scope<'brand> {
109106 call_tracker : Arc :: clone ( & self . call_tracker ) ,
110107 arguments : self . arguments . clone ( ) ,
111108 include_debug_symbols : self . include_debug_symbols ,
112- phantom : core:: marker:: PhantomData ,
113109 }
114110 }
115111
@@ -188,12 +184,12 @@ impl<'brand> Scope<'brand> {
188184 /// ```
189185 ///
190186 /// The expression `drop (IOH & OH)` returns the seeked value.
191- pub fn get ( & self , target : & BasePattern ) -> Option < PairBuilder < ProgNode > > {
187+ pub fn get ( & self , target : & BasePattern ) -> Option < PairBuilder < ProgNode < ' brand > > > {
192188 BasePattern :: from ( & self . get_input_pattern ( ) ) . translate ( & self . ctx , target)
193189 }
194190
195191 /// Access the Simplicity type inference context.
196- pub fn ctx ( & self ) -> & simplicity:: types:: Context {
192+ pub fn ctx ( & self ) -> & simplicity:: types:: Context < ' brand > {
197193 & self . ctx
198194 }
199195
@@ -205,10 +201,10 @@ impl<'brand> Scope<'brand> {
205201 /// for debug symbols will simply ignore it. The semantics of the program remain unchanged.
206202 pub fn with_debug_symbol < S : AsRef < Span > > (
207203 & mut self ,
208- args : PairBuilder < ProgNode > ,
209- body : & ProgNode ,
204+ args : PairBuilder < ProgNode < ' brand > > ,
205+ body : & ProgNode < ' brand > ,
210206 span : & S ,
211- ) -> Result < PairBuilder < ProgNode > , RichError > {
207+ ) -> Result < PairBuilder < ProgNode < ' brand > > , RichError > {
212208 match self . call_tracker . get_cmr ( span. as_ref ( ) ) {
213209 Some ( cmr) if self . include_debug_symbols => {
214210 let false_and_args = ProgNode :: bit ( self . ctx ( ) , false ) . pair ( args) ;
@@ -231,7 +227,7 @@ fn compile_blk<'brand>(
231227 scope : & mut Scope < ' brand > ,
232228 index : usize ,
233229 last_expr : Option < & Expression > ,
234- ) -> Result < PairBuilder < ProgNode > , RichError > {
230+ ) -> Result < PairBuilder < ProgNode < ' brand > > , RichError > {
235231 if index >= stmts. len ( ) {
236232 return match last_expr {
237233 Some ( expr) => expr. compile ( scope) ,
@@ -268,25 +264,28 @@ impl Program {
268264 arguments : Arguments ,
269265 include_debug_symbols : bool ,
270266 ) -> Result < Arc < named:: CommitNode < Elements > > , RichError > {
271- let mut scope = Scope :: new (
272- Arc :: clone ( self . call_tracker ( ) ) ,
273- arguments,
274- include_debug_symbols,
275- ) ;
276-
277- let main = self . main ( ) ;
278- let construct = main. compile ( & mut scope) . map ( PairBuilder :: build) ?;
279- // SimplicityHL types should be correct by construction. If not, assign the
280- // whole main function as the span for them, which is as sensible as anything.
281- named:: finalize_types ( & construct) . with_span ( main)
267+ types:: Context :: with_context ( |ctx| {
268+ let mut scope = Scope :: new (
269+ ctx,
270+ Arc :: clone ( self . call_tracker ( ) ) ,
271+ arguments,
272+ include_debug_symbols,
273+ ) ;
274+
275+ let main = self . main ( ) ;
276+ let construct = main. compile ( & mut scope) . map ( PairBuilder :: build) ?;
277+ // SimplicityHL types should be correct by construction. If not, assign the
278+ // whole main function as the span for them, which is as sensible as anything.
279+ named:: finalize_types ( & construct) . with_span ( main)
280+ } )
282281 }
283282}
284283
285284impl Expression {
286285 fn compile < ' brand > (
287286 & self ,
288287 scope : & mut Scope < ' brand > ,
289- ) -> Result < PairBuilder < ProgNode > , RichError > {
288+ ) -> Result < PairBuilder < ProgNode < ' brand > > , RichError > {
290289 match self . inner ( ) {
291290 ExpressionInner :: Block ( stmts, expr) => {
292291 scope. push_scope ( ) ;
@@ -303,7 +302,7 @@ impl SingleExpression {
303302 fn compile < ' brand > (
304303 & self ,
305304 scope : & mut Scope < ' brand > ,
306- ) -> Result < PairBuilder < ProgNode > , RichError > {
305+ ) -> Result < PairBuilder < ProgNode < ' brand > > , RichError > {
307306 let expr = match self . inner ( ) {
308307 SingleExpressionInner :: Constant ( value) => {
309308 let value = StructuralValue :: from ( value) ;
@@ -374,7 +373,7 @@ impl Call {
374373 fn compile < ' brand > (
375374 & self ,
376375 scope : & mut Scope < ' brand > ,
377- ) -> Result < PairBuilder < ProgNode > , RichError > {
376+ ) -> Result < PairBuilder < ProgNode < ' brand > > , RichError > {
378377 let args_ast = SingleExpression :: tuple ( self . args ( ) . clone ( ) , * self . as_ref ( ) ) ;
379378 let args = args_ast. compile ( scope) ?;
380379
@@ -467,8 +466,13 @@ impl Call {
467466/// The fold `(fold f)_n : E^(<2^n) × A → A`
468467/// takes the list of type `E^(<2^n)` and an initial accumulator of type `A`,
469468/// and it produces the final accumulator of type `A`.
470- fn list_fold ( bound : NonZeroPow2Usize , f : & ProgNode ) -> Result < ProgNode , simplicity:: types:: Error > {
471- fn next_f_array ( f_array : & ProgNode ) -> Result < ProgNode , simplicity:: types:: Error > {
469+ fn list_fold < ' brand > (
470+ bound : NonZeroPow2Usize ,
471+ f : & ProgNode < ' brand > ,
472+ ) -> Result < ProgNode < ' brand > , simplicity:: types:: Error > {
473+ fn next_f_array < ' brand > (
474+ f_array : & ProgNode < ' brand > ,
475+ ) -> Result < ProgNode < ' brand > , simplicity:: types:: Error > {
472476 /* f_(n + 1) : E^(2^(n + 1)) × A → A
473477 * f_(n + 1) := OIH ▵ (OOH ▵ IH; f_n); f_n
474478 */
@@ -478,10 +482,10 @@ fn list_fold(bound: NonZeroPow2Usize, f: &ProgNode) -> Result<ProgNode, simplici
478482 let half2_acc = ProgNode :: o ( ) . i ( ) . h ( ctx) . pair ( updated_acc) ;
479483 half2_acc. comp ( f_array) . map ( PairBuilder :: build)
480484 }
481- fn next_f_fold (
482- f_array : & ProgNode ,
483- f_fold : & ProgNode ,
484- ) -> Result < ProgNode , simplicity:: types:: Error > {
485+ fn next_f_fold < ' brand > (
486+ f_array : & ProgNode < ' brand > ,
487+ f_fold : & ProgNode < ' brand > ,
488+ ) -> Result < ProgNode < ' brand > , simplicity:: types:: Error > {
485489 /* (fold f)_(n + 1) : E<2^(n + 1) × A → A
486490 * (fold f)_(n + 1) := OOH ▵ (OIH ▵ IH);
487491 * case (drop (fold f)_n)
@@ -539,16 +543,18 @@ fn list_fold(bound: NonZeroPow2Usize, f: &ProgNode) -> Result<ProgNode, simplici
539543/// In this case, the loop continues without returning anything.
540544/// The loop returns the final iterator after the final iteration
541545/// if `f` never returned a successful output.
542- fn for_while (
546+ fn for_while < ' brand > (
543547 bit_width : Pow2Usize ,
544- f : PairBuilder < ProgNode > ,
545- ) -> Result < PairBuilder < ProgNode > , simplicity:: types:: Error > {
548+ f : PairBuilder < ProgNode < ' brand > > ,
549+ ) -> Result < PairBuilder < ProgNode < ' brand > > , simplicity:: types:: Error > {
546550 /* for_while_0 f : E × A → A
547551 * for_while_0 f := (OH ▵ (IH ▵ false); f) ▵ IH;
548552 * case (injl OH)
549553 * (OH ▵ (IH ▵ true); f)
550554 */
551- fn for_while_0 ( f : & ProgNode ) -> Result < PairBuilder < ProgNode > , simplicity:: types:: Error > {
555+ fn for_while_0 < ' brand > (
556+ f : & ProgNode < ' brand > ,
557+ ) -> Result < PairBuilder < ProgNode < ' brand > > , simplicity:: types:: Error > {
552558 let ctx = f. inference_context ( ) ;
553559 let f_output = ProgNode :: o ( )
554560 . h ( ctx)
@@ -571,7 +577,9 @@ fn for_while(
571577 * where
572578 * f : A × (C × 2^(2^(n + 1))) → B + A
573579 */
574- fn adapt_f ( f : & ProgNode ) -> Result < PairBuilder < ProgNode > , simplicity:: types:: Error > {
580+ fn adapt_f < ' brand > (
581+ f : & ProgNode < ' brand > ,
582+ ) -> Result < PairBuilder < ProgNode < ' brand > > , simplicity:: types:: Error > {
575583 let ctx = f. inference_context ( ) ;
576584 let f_input = ProgNode :: o ( ) . h ( ctx) . pair (
577585 ProgNode :: i ( )
@@ -643,7 +651,7 @@ impl Match {
643651 fn compile < ' brand > (
644652 & self ,
645653 scope : & mut Scope < ' brand > ,
646- ) -> Result < PairBuilder < ProgNode > , RichError > {
654+ ) -> Result < PairBuilder < ProgNode < ' brand > > , RichError > {
647655 scope. push_scope ( ) ;
648656 scope. insert (
649657 self . left ( )
0 commit comments