Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve handleArcs function in translate/Main.hs #50

Closed
1 of 2 tasks
jrbeaumont opened this issue Sep 15, 2016 · 15 comments
Closed
1 of 2 tasks

Improve handleArcs function in translate/Main.hs #50

jrbeaumont opened this issue Sep 15, 2016 · 15 comments

Comments

@jrbeaumont
Copy link
Member

jrbeaumont commented Sep 15, 2016

This was designed to include OR-causality handling, and has been tested and working, but was done quickly and without much thought on efficiency.

A TODO has been included in the code, but this issue serves to document it better.

What needs to be done:

  • Get rid of explicit recursion. Possibly by using groupBy
  • Generally improve performance.
@snowleopard
Copy link
Member

I suggest to have a look at function groupSortOn in Data.List.Extra library: https://hackage.haskell.org/package/extra-1.5/docs/Data-List-Extra.html

@jrbeaumont
Copy link
Member Author

groupSortOn requires that the type we're testing for is of type Ord.

@jrbeaumont
Copy link
Member Author

groupOn might work. I'm trying that now

@jrbeaumont
Copy link
Member Author

@snowleopard: OK groupOn doesn't work either. It groups them individually by snd but not all where snd are grouped together.
groupSortOn would probably do the same

@jrbeaumont
Copy link
Member Author

For example:
arcs = [([A+, B+], C+), ([A-], C-), ([B-], C-), ([D+], C+), ([E+], C+), ([D-,E-], C-)]
This is: orGate a b c <> andGate d e c

When using groupOn or groupSortOn, the result is:

[A+,B+] C+
[A-] C-
[B-] C-
[D+] C+
[E+] C+
[D-,E-] C-

It doesn't combine them like I hoped. I was hoping for:

[[A+, B+], [D+], [E+]] C+
[[A-], [B-], [D-, E-]] C-

@snowleopard
Copy link
Member

groupSortOn would probably do the same

@jrbeaumont No, actually it will sort them and then group, which is what we need. It does require a more complex constraint Ord instead of just Eq, but I think this is not a big deal for us. Essentially we ask for a to be not only comparable by ==, but also by <, so I suggest to try it.

@snowleopard
Copy link
Member

When using groupOn or groupSortOn, the result is:

Could you show the code?

@jrbeaumont
Copy link
Member Author

@snowleopard The latest version is in my repo: [https://github.com/jrbeaumont/concepts]

@jrbeaumont
Copy link
Member Author

@snowleopard: Currently this doesn't print anything usable by a .g parser, just what the list contains for debugging.

@snowleopard
Copy link
Member

Just an experiment:

Prelude Data.List.Extra> let arcs = [([1,2], 'a'), ([3], 'b'), ([4,5], 'a')]
Prelude Data.List.Extra> let grouped = groupSortOn snd arcs
Prelude Data.List.Extra> grouped
[[([1,2],'a'),([4,5],'a')],[([3],'b')]]

So it looks like groupSortOn does sort elements. There is still a bit of work to be done though :)

@snowleopard
Copy link
Member

By the way, without Ord constraint we wouldn't really be able to improve efficiency of the code anyway.

@jrbeaumont
Copy link
Member Author

jrbeaumont commented Sep 16, 2016

Yeah I found an issue.
Take a look at lines 36-44 in translate/Main.hs

This is what I've had to do to include Ord for DynSignal, as well as Ord for Transition a in order for this to work.
Are you aware of a better way?

I understand the importance of Ord but I couldn't find the best way to do it. The whole thing works with this.

@snowleopard
Copy link
Member

Mmm, what about data DynSignal = Signal Int deriving (Eq, Ord)? :)

Otherwise, I'd implement it as:

instance Ord DynSignal where
    compare (Signal x) (Signal y) = compare x y

@jrbeaumont
Copy link
Member Author

jrbeaumont commented Sep 16, 2016

Mmm, what about data DynSignal = Signal Int deriving (Eq, Ord)? :)

I have tried that, and it just throws errors in the handleArcs function, needing Ord x => included, but I cannot work out what to put there, as there isn't an a or something associated with DynSignal.

So I added my own instance. compare x y is the same, but a better than (<=) ... I'll pop that in :)

@jrbeaumont
Copy link
Member Author

Fixed in #51

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants