Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

function - How can I extract data from all elements of a multiset?

Let's say I have a datatype called dtyp. It looks like this

datatype dtype = T bool int

I'd like to define a function extr :: "dtype multiset => int multiset" which takes a multiset of these dtype elements and returns a multiset containing the ints in each of the dtype elements. So for example:

value "extr {#T True 4, T False 5, T False 7#}" should give {#4,5,7#}

I first thought of iterating through the multiset, but I know it isn't possible as these multisets are based off normal sets which are not iterable. I then thought about universal quantifiers, but I'm not sure how to use them in this situation. Could I please have some help?

Thanks in advance!

question from:https://stackoverflow.com/questions/65879733/how-can-i-extract-data-from-all-elements-of-a-multiset

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Some fancy syntax to define the extractor directly:

datatype dtype = T bool (payload: int)

Thinking about sets is a good starting point: you are looking for image for multisets. That function exists and it is called image_mset. So:

definition extr :: "dtype multiset => int multiset" where
  "extr = image_mset payload"

The syntax `# for image_mset has not yet been added to the Multiset entry, but it is used by various people.

notation image_mset (infixr "`#" 90)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...