Skip to content

Commit aec37ef

Browse files
committed
Rewrite CompM combinators in Prelude.sawcore to use new tuple types.
1 parent 0b43f8f commit aec37ef

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

saw-core/prelude/Prelude.sawcore

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,15 +2085,24 @@ composeM : (a b c: sort 0) -> (a -> CompM b) -> (b -> CompM c) -> a -> CompM c;
20852085
composeM a b c f g x = bindM b c (f x) g;
20862086

20872087
-- Tuple a type onto the input and output types of a monadic function
2088-
tupleCompMFunBoth : (a b c: sort 0) -> (a -> CompM b) -> #(c, a) -> CompM #(c, b);
2089-
tupleCompMFunBoth a b c f =
2090-
\ (x : #(c, a)) ->
2091-
bindM b #(c, b) (f x.1) (\ (y:b) -> returnM #(c, b) (x.0, y));
2092-
2093-
-- Tuple a valu onto the output of a monadic function
2094-
tupleCompMFunOut : (a b c: sort 0) -> c -> (a -> CompM b) -> (a -> CompM #(c, b));
2095-
tupleCompMFunOut a b c x f =
2096-
\ (y:a) -> bindM b #(c, b) (f y) (\ (z:b) -> returnM #(c, b) (x, z));
2088+
tupleCompMFunBoth :
2089+
(a b : TypeList) ->
2090+
(c : sort 0) ->
2091+
(Tuple a -> CompM (Tuple b)) ->
2092+
Tuple (TypeCons c a) -> CompM (Tuple (TypeCons c b));
2093+
tupleCompMFunBoth a b c f x =
2094+
bindM (Tuple b) (Tuple (TypeCons c b)) (f (tailTuple c a x))
2095+
(\ (y : Tuple b) -> returnM (Tuple (TypeCons c b)) (consTuple c b (headTuple c a x) y));
2096+
2097+
-- Tuple a value onto the output of a monadic function
2098+
tupleCompMFunOut :
2099+
(a : sort 0) ->
2100+
(b : TypeList) ->
2101+
(c : sort 0) ->
2102+
c -> (a -> CompM (Tuple b)) -> (a -> CompM (Tuple (TypeCons c b)));
2103+
tupleCompMFunOut a b c x f y =
2104+
bindM (Tuple b) (Tuple (TypeCons c b)) (f y)
2105+
(\ (z : Tuple b) -> returnM (Tuple (TypeCons c b)) (consTuple c b x z));
20972106

20982107
-- Map a monadic function across a vector
20992108
mapM : (a :sort 0) -> (b : isort 0) -> (a -> CompM b) -> (n : Nat) -> Vec n a -> CompM (Vec n b);
@@ -2258,16 +2267,21 @@ lrtPi lrts b =
22582267
(\ (lrt:LetRecType) (_:LetRecTypes) (rest:sort 0) -> lrtToType lrt -> rest)
22592268
lrts;
22602269

2261-
-- Build the product type (lrtToType lrt1, ..., lrtToType lrtn) from the
2270+
-- Build the type list [lrtToType lrt1, ..., lrtToType lrtn] from the
22622271
-- LetRecTypes list [lrt1, ..., lrtn]
2263-
lrtTupleType : LetRecTypes -> sort 0;
2264-
lrtTupleType lrts =
2272+
lrtTypeList : LetRecTypes -> TypeList;
2273+
lrtTypeList lrts =
22652274
LetRecTypes#rec
2266-
(\ (lrts:LetRecTypes) -> sort 0)
2267-
#()
2268-
(\ (lrt:LetRecType) (_:LetRecTypes) (rest:sort 0) -> #(lrtToType lrt, rest))
2275+
(\ (lrts:LetRecTypes) -> TypeList)
2276+
TypeNil
2277+
(\ (lrt:LetRecType) (_:LetRecTypes) (rest:TypeList) -> TypeCons (lrtToType lrt) rest)
22692278
lrts;
22702279

2280+
-- Build the product type (lrtToType lrt1, ..., lrtToType lrtn) from the
2281+
-- LetRecTypes list [lrt1, ..., lrtn]
2282+
lrtTupleType : LetRecTypes -> sort 0;
2283+
lrtTupleType lrts = Tuple (lrtTypeList lrts);
2284+
22712285
-- NOTE: the following are needed to define letRecM instead of making it a
22722286
-- primitive, which we are keeping commented here in case that is needed
22732287
{-
@@ -2349,7 +2363,7 @@ letRecM1 : (a b c : sort 0) -> ((a -> CompM b) -> (a -> CompM b)) ->
23492363
letRecM1 a b c fn body =
23502364
letRecM
23512365
(LRT_Cons (LRT_Fun a (\ (_:a) -> LRT_Ret b)) LRT_Nil) c
2352-
(\ (f:a -> CompM b) -> (fn f, ()))
2366+
(\ (f:a -> CompM b) -> consTuple (a -> CompM b) TypeNil (fn f) ())
23532367
(\ (f:a -> CompM b) -> body f);
23542368

23552369
-- A single-argument fixed-point function
@@ -2359,7 +2373,7 @@ fixM : (a:sort 0) -> (b:a -> sort 0) ->
23592373
fixM a b f x =
23602374
letRecM (LRT_Cons (LRT_Fun a (\ (y:a) -> LRT_Ret (b y))) LRT_Nil)
23612375
(b x)
2362-
(\ (g: (y:a) -> CompM (b y)) -> (f g, ()))
2376+
(\ (g: (y:a) -> CompM (b y)) -> consTuple ((y:a) -> CompM (b y)) TypeNil (f g) ())
23632377
(\ (g: (y:a) -> CompM (b y)) -> g x);
23642378

23652379

@@ -2454,7 +2468,10 @@ multiFixM : (lrts:LetRecTypes) -> lrtPi lrts (lrtTupleType lrts) ->
24542468
multiArgFixM : (lrt:LetRecType) -> (lrtToType lrt -> lrtToType lrt) ->
24552469
lrtToType lrt;
24562470
multiArgFixM lrt F =
2457-
(multiFixM (LRT_Cons lrt LRT_Nil) (\ (f:lrtToType lrt) -> (F f, ()))).0;
2471+
(multiFixM
2472+
(LRT_Cons lrt LRT_Nil)
2473+
(\ (f:lrtToType lrt) -> consTuple (lrtToType lrt) TypeNil (F f) ())
2474+
).0;
24582475

24592476

24602477
-- Test computations
@@ -2516,7 +2533,7 @@ test_fun6 x =
25162533
(Vec 64 Bool)
25172534
(\ (f1:(Vec 64 Bool -> CompM (Vec 64 Bool)))
25182535
(f2:(Vec 64 Bool -> CompM (Vec 64 Bool))) ->
2519-
(f2, (f1, ())))
2536+
(f2, f1))
25202537
(\ (f1:(Vec 64 Bool -> CompM (Vec 64 Bool)))
25212538
(f2:(Vec 64 Bool -> CompM (Vec 64 Bool))) ->
25222539
f1 x);

0 commit comments

Comments
 (0)