k-user AT lists.cs.illinois.edu
Subject: K-user mailing list
List archive
- From: "Saha, Shambwaditya" <ssaha6 AT illinois.edu>
- To: Philip Daian <phil AT linux.com>
- Cc: "k-user AT cs.uiuc.edu" <k-user AT cs.uiuc.edu>
- Subject: Re: [K-user] Bags
- Date: Thu, 26 Dec 2013 21:56:58 -0600
- List-archive: <http://lists.cs.uiuc.edu/pipermail/k-user/>
- List-id: <k-user.cs.uiuc.edu>
Hi,
Thanks a lot for your detailed explanation about Bags and the suggestions to modify the code, it really helped. The code kompiles, but unfortunately, I was not able to krun something like search (BagItem(5) BagItem(8)) 5 or 5 in ( BagItem(5) BagItem(8) ) . It complained about a syntax error. Do you have any quick tip.
Thanks in advance!
Regards,
Shambwaditya
On Thu, Dec 26, 2013 at 9:05 PM, Philip Daian <phil AT linux.com> wrote:
As far as I know there is no way to specifically mention in the syntax that you want a bag of ints. A bag is simply .Bag, BagItem(K), or Bag Bag. Your code is failing because in your side condition, you are using an equality operator that expects a comparison of two items of sort K on a BagItem.To fix your code, change it to:
syntax Bool ::= "search" Bag Intrule search ( BagItem(V) Bg:Bag) V1:Int => true when V ==K V1rule search (BagItem(V1) Bg:Bag) V:Int => search Bg V when V1 =/=K Vrule search .Bag V:Int => false
BagItem is simply a wrapper around values stored in the bag meant to distinguish bag items from items in other builtins. Note that now you are comparing the values in the bag to those passed into the search function. If you want to require bags of ints and get stuck otherwise, you can ask for the K items stored in the bags to be of sort int.rule search ( BagItem(V:Int) Bg:Bag) V1:Int => true when V ==K V1
syntax Bool ::= "search" Bag Int
rule search (BagItem(V1:Int) Bg:Bag) V:Int => search Bg V when V1 =/=K Vrule search .Bag V:Int => false
Now, if you call search on a nonempty noninteger bag, no rules will match and your rewriting will get stuck (unless you do something like search (BagItem(5) BagItem("hi")) 5, which will still evaluate to true. To fix this change the second rule to match BagItem(V:Int) .Bag instead of Bg:Bag and add another rule moving any found item to the end of your bag).
See the K code in include/builtins/bag.k for the best documentation of bags there is at the moment to my knowledge.
That code includes "in" function, which is a more general version of your search construct for all bultins which have equality in K.
Hope that helps!
- Phil
On Thu, Dec 26, 2013 at 4:06 PM, Saha, Shambwaditya <ssaha6 AT illinois.edu> wrote:
_______________________________________________Hi,
I want to define a search function on Bag of Ints. I tried to write something of the form :
syntax Bool ::= "search" Bag Intrule search ( V:BagItem Bg:Bag) V1:Int => true when V ==K V1rule search (V1:BagItem Bg:Bag) V:Int => search Bg V when V1 =/=K Vrule search .Bag V:Int => false
This definition is obviously not correct, and I have a few questions about it :
1. How do we specifically mention in the syntax that we want Bag of Ints.2. It is showing an error : "Variable V is contextually expected to have sort K but it has been declared (or infered) of sort BagItem", how should we solve it.3. And what should be a valid instance of this program. i.e. how should we specify a bag, in krun.
Thanks in Advance!
Regards,Shambwaditya
k-user mailing list
k-user AT cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/k-user
- [K-user] Bags, Saha, Shambwaditya, 12/26/2013
- Re: [K-user] Bags, Philip Daian, 12/26/2013
- Message not available
- Re: [K-user] Bags, Saha, Shambwaditya, 12/26/2013
- Re: [K-user] Bags, Rosu, Grigore, 12/28/2013
- Re: [K-user] Bags, Saha, Shambwaditya, 12/26/2013
Archive powered by MHonArc 2.6.16.