Skip to content

Commit 71a9b65

Browse files
pratapsingh1729zhengyao-lin
authored andcommitted
add Some? and None?
1 parent da2cbf4 commit 71a9b65

File tree

7 files changed

+53
-3
lines changed

7 files changed

+53
-3
lines changed

extraction/src/execlib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,19 @@ pub open spec fn view_first_byte_pair_opt(x: Option<(u8, SecretBuf<'_>)>) -> Seq
327327
}
328328
}
329329

330+
331+
pub fn owl_is_some<T>(x: Option<T>) -> (res: bool)
332+
ensures res <==> x.is_some()
333+
{
334+
x.is_some()
335+
}
336+
337+
pub fn owl_is_none<T>(x: Option<T>) -> (res: bool)
338+
ensures res <==> x.is_none()
339+
{
340+
x.is_none()
341+
}
342+
330343
}
331344

332345

extraction/src/speclib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,17 @@ pub open spec fn ghost_unit() -> Ghost<()>
249249
Ghost(())
250250
}
251251

252+
pub open spec fn is_some<T>(x: Option<T>) -> bool
253+
{
254+
vstd::std_specs::option::is_some(&x)
255+
}
256+
257+
pub open spec fn is_none<T>(x: Option<T>) -> bool
258+
{
259+
vstd::std_specs::option::is_none(&x)
260+
}
261+
262+
252263
// pub open spec fn owl_ghost_unit() -> Ghost<()>
253264
// {
254265
// Ghost(())

src/Extraction/Concretify.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ concretifyApp (PRes (PDot PTop f)) params args = do
305305
-- This is probably fine, since some other branch should constrain the type
306306
-- debugPrint "WARNING: Can't infer type of None, using dummy type"
307307
return $ mkAppNoLets f $ FOption FDummy
308+
("Some?", [x]) -> return $ mkAppNoLets "is_some" $ FBool
309+
("None?", [x]) -> return $ mkAppNoLets "is_none" $ FBool
308310
("andb", [x, y]) -> return $ mkAppNoLets f FBool
309311
("andp", [x, y]) -> return $ mkAppNoLets f FGhost
310312
("notb", [x]) -> return $ mkAppNoLets f FBool

src/Extraction/SpecExtraction.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ extractCEnum (CEnum n cs isVest _ _) = do
283283
return [__di|
284284
#{lhs} => #{specname}::#{caseName}(#{rhsX}),
285285
|]
286-
debugPrint $ show $ owlpretty $ map fst speccases
286+
-- debugPrint $ show $ owlpretty $ map fst speccases
287287
parseBranches <- mapM mkParseBranch (zip speccases [0..])
288288
let parse = [__di|
289289
\#[verifier::opaque]
@@ -513,7 +513,10 @@ extractCAExpr aexpr = do
513513
let [a] = args
514514
a' <- extractCAExpr a
515515
return [di|Option::Some(#{a'})|]
516-
"None" -> return [di|Option::None|]
516+
"None" -> case aexpr ^. tty of
517+
FOption FDummy -> return [di|Option::None|]
518+
FOption t -> return [di|Option::<#{pretty $ specTyOf t}>::None|]
519+
_ -> throwError $ TypeError $ "None on non-option type: " ++ show (owlpretty $ aexpr ^. tty)
517520
"eq" -> do
518521
let [a,b] = args
519522
a' <- extractCAExpr a

src/Pass/PathResolution.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import Prettyprinter
3232
import Data.IORef
3333

3434
builtinFuncs :: [String]
35-
builtinFuncs = ["UNIT", "TRUE", "FALSE", "eq", "Some", "None", "andb", "length", "plus", "mult", "zero", "concat", "cipherlen", "pk_cipherlen", "vk", "dhpk", "enc_pk", "dh_combine", "sign", "pkdec", "dec", "vrfy", "mac", "mac_vrfy", "checknonce", "prf", "H", "is_group_elem", "crh", "xor"]
35+
builtinFuncs = ["UNIT", "TRUE", "FALSE", "eq", "Some", "None", "andb", "length", "plus", "mult", "zero", "concat", "cipherlen", "pk_cipherlen", "vk", "dhpk", "enc_pk", "dh_combine", "sign", "pkdec", "dec", "vrfy", "mac", "mac_vrfy", "checknonce", "prf", "H", "is_group_elem", "crh", "xor", "Some?", "None?"]
3636

3737
data PathType =
3838
PTName

src/Typing.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,24 @@ initDetFuncs = withNormalizedTys $ [
205205
TOption _ -> return $ t^.val
206206
_ -> typeError $ show $ owlpretty "Expected type is not option type: " <> owlpretty t
207207
_ -> typeError $ show $ ErrBadArgs "None" (map snd args))),
208+
mkSimpleFunc "Some?" 1 $ \args ->
209+
case args of
210+
[t] ->
211+
case (stripRefinements t)^.val of
212+
TOption t' -> return $ TBool advLbl
213+
_ -> do
214+
l <- coveringLabel t
215+
return $ TBool l
216+
_ -> typeError $ show $ ErrBadArgs "Some?" args,
217+
mkSimpleFunc "None?" 1 $ \args ->
218+
case args of
219+
[t] ->
220+
case (stripRefinements t)^.val of
221+
TOption t' -> return $ TBool advLbl
222+
_ -> do
223+
l <- coveringLabel t
224+
return $ TBool l
225+
_ -> typeError $ show $ ErrBadArgs "None?" args,
208226
("andb", (2, \ps args -> do
209227
assert ("Bad params") $ length ps == 0
210228
case args of

tests/success/option.owl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ def alice<i>() @ A : Unit =
1515
let tst3 : Data<[M]> = Some<type Name(N<i>)>(get(M)) in
1616
// None always requires the option
1717
let tst4 : Option Name(N<i>) = None<type Name(N<i>)>() in
18+
let tst5 : Bool<adv> = None?(tst4) in
19+
let tst6 : Bool<adv> = Some?(tst4) in
20+
let tst7 : Bool<[M]> = Some?(tst3) in // ill-typed case
1821
()

0 commit comments

Comments
 (0)