Skip to content

Commit 8f377ec

Browse files
Add hook LIST.make (#2272)
1 parent 8c197c4 commit 8f377ec

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

docs/hooks.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,16 @@ The number of elements in the given list.
702702
[hook{}("LIST.size")]
703703
~~~
704704

705+
### LIST.make
706+
707+
`LIST.make(n, x)` is a list of length `n` with `x` the value of every element.
708+
The result is `bottom` if n is negative.
709+
710+
~~~
711+
hooked-symbol make{}(Int{}, Elem{}) : List{}
712+
[hook{}("LIST.make")]
713+
~~~
714+
705715
## LIST.updateAll
706716

707717
`LIST.updateAll(L1, index, L2)` creates a new list using a list `L2` of size `N`

kore/src/Kore/Builtin/List.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ symbolVerifiers =
194194
, ( sizeKey
195195
, Builtin.verifySymbol Int.assertSort [assertSort]
196196
)
197+
, ( makeKey
198+
, Builtin.verifySymbol assertSort [Int.assertSort, acceptAnySort]
199+
)
197200
, ( updateAllKey
198201
, Builtin.verifySymbol assertSort [assertSort, Int.assertSort, assertSort]
199202
)
@@ -322,6 +325,15 @@ evalSize resultSort [_list] = do
322325
& return
323326
evalSize _ _ = Builtin.wrongArity sizeKey
324327

328+
evalMake :: Builtin.Function
329+
evalMake resultSort [_len, value] = do
330+
_len <- fromInteger <$> Int.expectBuiltinInt getKey _len
331+
if _len >= 0
332+
then
333+
returnList resultSort (Seq.replicate _len value)
334+
else return (Pattern.bottomOf resultSort)
335+
evalMake _ _ = Builtin.wrongArity sizeKey
336+
325337
evalUpdateAll :: Builtin.Function
326338
evalUpdateAll resultSort [_list1, _ix, _list2] = do
327339
_list1 <- expectBuiltinList getKey _list1
@@ -352,6 +364,7 @@ builtinFunctions =
352364
, (updateKey, Builtin.functionEvaluator evalUpdate)
353365
, (inKey, Builtin.functionEvaluator evalIn)
354366
, (sizeKey, Builtin.functionEvaluator evalSize)
367+
, (makeKey, Builtin.functionEvaluator evalMake)
355368
, (updateAllKey, Builtin.functionEvaluator evalUpdateAll)
356369
]
357370

kore/src/Kore/Builtin/List/List.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module Kore.Builtin.List.List
2424
, updateKey
2525
, inKey
2626
, sizeKey
27+
, makeKey
2728
, updateAllKey
2829
) where
2930

@@ -199,5 +200,8 @@ inKey = "LIST.in"
199200
sizeKey :: IsString s => s
200201
sizeKey = "LIST.size"
201202

203+
makeKey :: IsString s => s
204+
makeKey = "LIST.make"
205+
202206
updateAllKey :: IsString s => s
203207
updateAllKey = "LIST.updateAll"

kore/test/Test/Kore/Builtin/Definition.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,10 @@ getListSymbol =
384384
sizeListSymbol :: Internal.Symbol
385385
sizeListSymbol = builtinSymbol "sizeList" intSort [listSort] & hook "LIST.size"
386386

387+
makeListSymbol :: Internal.Symbol
388+
makeListSymbol =
389+
builtinSymbol "makeList" listSort [intSort, intSort] & hook "LIST.make"
390+
387391
updateListSymbol :: Internal.Symbol
388392
updateListSymbol = builtinSymbol "updateList" listSort
389393
[listSort, intSort, intSort] & hook "LIST.update"
@@ -411,6 +415,9 @@ getList list poz = mkApplySymbol getListSymbol [list, poz]
411415
sizeList :: TermLike VariableName -> TermLike VariableName
412416
sizeList l = mkApplySymbol sizeListSymbol [l]
413417

418+
makeList :: TermLike VariableName -> TermLike VariableName -> TermLike VariableName
419+
makeList len x = mkApplySymbol makeListSymbol [len, x]
420+
414421
updateList
415422
:: TermLike VariableName
416423
-> TermLike VariableName
@@ -1530,6 +1537,7 @@ listModule =
15301537
, hookedSymbolDecl updateListSymbol
15311538
, hookedSymbolDecl inListSymbol
15321539
, hookedSymbolDecl sizeListSymbol
1540+
, hookedSymbolDecl makeListSymbol
15331541
, hookedSymbolDecl updateAllListSymbol
15341542
-- A second builtin List sort, to confuse 'asPattern'.
15351543
, listSortDecl2

kore/test/Test/Kore/Builtin/List.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Test.Kore.Builtin.List
1313
, test_inUnit
1414
, test_inElement
1515
, test_inConcat
16+
, test_make
1617
, test_updateAll
1718
, hprop_unparse
1819
, test_size
@@ -418,6 +419,20 @@ test_size =
418419
(===) Pattern.top =<< evaluateT predicate
419420
]
420421

422+
test_make :: [TestTree]
423+
test_make =
424+
[ testCaseWithoutSMT "make(-1, 5) === \\bottom" $ do
425+
result <- evaluate $ makeList (mkInt (-1)) (mkInt 5)
426+
assertEqual' "" Pattern.bottom result
427+
, testCaseWithoutSMT "make(0, 5) === []" $ do
428+
result <- evaluate $ makeList (mkInt 0) (mkInt 5)
429+
assertEqual' "" (Pattern.fromTermLike (asInternal [])) result
430+
, testCaseWithoutSMT "make(3, 5) === [5, 5, 5]" $ do
431+
result <- evaluate $ makeList (mkInt 3) (mkInt 5)
432+
let expect = asInternal . fmap mkInt $ Seq.fromList [5, 5, 5]
433+
assertEqual' "" (Pattern.fromTermLike expect) result
434+
]
435+
421436
test_updateAll :: [TestTree]
422437
test_updateAll =
423438
[ testCaseWithoutSMT "updateAll([1, 2, 3], -1, [5]) === \\bottom" $ do

0 commit comments

Comments
 (0)