@@ -12,7 +12,7 @@ module Circuit.Language.Compile
1212 compileWithWire ,
1313 compileWithWires ,
1414 exprToArithCircuit ,
15- unBundle
15+ _unBundle ,
1616 )
1717where
1818
@@ -21,21 +21,21 @@ import Circuit.Arithmetic
2121import Circuit.Language.Expr
2222 ( BinOp (.. ),
2323 Expr (.. ),
24+ Hash (Hash ),
2425 UVar (.. ),
2526 UnOp (.. ),
2627 getAnnotation ,
2728 hashCons ,
28- Hash (Hash ),
2929 )
3030import Circuit.Language.TExpr qualified as TExpr
3131import Data.Field.Galois (GaloisField )
3232import Data.Map qualified as Map
33+ import Data.Maybe (fromJust )
3334import Data.Set qualified as Set
3435import Data.Vector qualified as V
36+ import Data.Vector.Sized qualified as SV
3537import Protolude hiding (Semiring )
3638import Text.PrettyPrint.Leijen.Text hiding ((<$>) )
37- import Data.Maybe (fromJust )
38- import Data.Vector.Sized qualified as SV
3939import Unsafe.Coerce (unsafeCoerce )
4040
4141-------------------------------------------------------------------------------
@@ -204,13 +204,13 @@ compileWithWire freshWire e = do
204204 <$> compileWithWires (V. singleton $ fmap TExpr. coerceGroundType freshWire) e
205205
206206compileWithWires ::
207- (Hashable f ) =>
207+ (Hashable f ) =>
208208 (GaloisField f ) =>
209209 (MonadState (BuilderState f ) m ) =>
210210 (MonadError (CircuitBuilderError f ) m ) =>
211211 V. Vector (m (TExpr. Var Wire f f )) ->
212212 TExpr. Expr Wire f ty ->
213- m (V. Vector (TExpr. Var Wire f f ) )
213+ m (V. Vector (TExpr. Var Wire f f ))
214214compileWithWires ws expr = do
215215 e <- hashCons <$> unType expr
216216 compileOut <- memoizedCompile e
@@ -331,7 +331,7 @@ _compile expr = withCompilerCache (getAnnotation expr) $ case expr of
331331 ESplit _ n input -> do
332332 -- assertSingle is justified as the input must be of type f
333333 i <- memoizedCompile input >>= assertSingleSource >>= addWire
334- outputs <- V. generateM n $ \ _ -> do
334+ outputs <- V. generateM n $ \ _ -> do
335335 w <- imm
336336 emit $ Boolean w
337337 pure w
@@ -351,14 +351,6 @@ _compile expr = withCompilerCache (getAnnotation expr) $ case expr of
351351 bs <- toList <$> memoizedCompile bits
352352 ws <- traverse addWire bs
353353 pure . V. singleton . AffineSource $ unsplit ws
354- EAtIndex _ v _ix -> do
355- v' <- memoizedCompile v
356- pure . V. singleton $ v' V. ! (fromIntegral _ix)
357- EUpdateIndex _ p b v -> do
358- v' <- memoizedCompile v
359- b' <- memoizedCompile b >>= assertSingleSource
360- let p' = fromIntegral p
361- pure $ V. imap (\ _ix w -> if _ix == p' then b' else w) v'
362354
363355memoizedCompile ::
364356 forall f m .
@@ -385,32 +377,22 @@ exprToArithCircuit expr output = do
385377 compileOut <- memoizedCompile e >>= assertSingleSource
386378 emit $ Mul (ConstGate 1 ) (addVar compileOut) output
387379
388- fieldToBool
389- :: (Hashable f , GaloisField f ) =>
390- TExpr. Expr Wire f f ->
391- ExprM f (TExpr. Expr Wire f Bool )
380+ fieldToBool ::
381+ (Hashable f , GaloisField f ) =>
382+ TExpr. Expr Wire f f ->
383+ ExprM f (TExpr. Expr Wire f Bool )
392384fieldToBool e = do
393385 eOut <- hashCons <$> unType e
394386 a <- memoizedCompile eOut >>= assertSingleSource >>= addWire
395387 emit $ Boolean a
396388 pure $ unsafeCoerce e
397389
398- unBundle ::
399- forall n f ty .
400- (KnownNat n , GaloisField f , Hashable f ) =>
401- TExpr. Expr Wire f (SV. Vector n ty ) ->
402- ExprM f (SV. Vector n (TExpr. Expr Wire f f ))
403- unBundle b = do
404- bis <- memoizedCompile . hashCons =<< unType b
405- ws <- traverse addWire bis
406- pure $ fromJust $ SV. toSized (TExpr. EVar . TExpr. VarField <$> ws)
407-
408- unType :: forall f ty m . MonadState (BuilderState f ) m => TExpr. Expr Wire f ty -> m (Expr () Wire f )
390+ unType :: forall f ty m . (MonadState (BuilderState f ) m ) => TExpr. Expr Wire f ty -> m (Expr () Wire f )
409391unType = \ case
410392 TExpr. EVal v -> pure $ case v of
411393 TExpr. ValBool b -> EVal () b
412394 TExpr. ValField f -> EVal () f
413- TExpr. EVar v -> case v of
395+ TExpr. EVar v -> case v of
414396 TExpr. VarField w -> pure $ EVar () (UVar w)
415397 TExpr. VarBool b -> do
416398 emit $ Boolean b
@@ -421,8 +403,6 @@ unType = \case
421403 TExpr. EEq l r -> EEq () <$> unType l <*> unType r
422404 TExpr. ESplit i -> ESplit () (fromIntegral $ natVal (Proxy @ (TExpr. NBits f ))) <$> unType i
423405 TExpr. EJoin i -> EJoin () <$> unType i
424- TExpr. EAtIndex v ix -> EAtIndex () <$> unType v <*> pure (fromIntegral ix)
425- TExpr. EUpdateIndex p b v -> EUpdateIndex () (fromIntegral p) <$> unType b <*> unType v
426406 TExpr. EBundle b -> EBundle () <$> traverse unType (SV. fromSized b)
427407 where
428408 untypeBinOp :: TExpr. BinOp f a -> BinOp
@@ -440,3 +420,14 @@ unType = \case
440420 TExpr. UNeg -> UNeg
441421 TExpr. UNot -> UNot
442422
423+ _unBundle ::
424+ forall n f ty .
425+ (KnownNat n ) =>
426+ (GaloisField f ) =>
427+ (Hashable f ) =>
428+ TExpr. Expr Wire f (SV. Vector n ty ) ->
429+ ExprM f (SV. Vector n (TExpr. Expr Wire f f ))
430+ _unBundle b = do
431+ bis <- memoizedCompile . hashCons =<< unType b
432+ ws <- traverse addWire bis
433+ pure $ fromJust $ SV. toSized (TExpr. EVar . TExpr. VarField <$> ws)
0 commit comments