ChildsplayOSU / bogl

Haskell implementation of the BoGL language
https://bogl.engr.oregonstate.edu
BSD 3-Clause "New" or "Revised" License
7 stars 1 forks source link

Improve tuple selection ergonomics #155

Closed alexgrejuc closed 3 years ago

alexgrejuc commented 3 years ago

The issue

Some functions need to store values in tuples and then later extract them. However, tuple extraction in bogl is not great. Every subset of every tuple requires the definition of a new function to be extracted. For example, see the situation below:

first : (Board, Piece, Int) -> Board
first(b, p, i) = b

-- draws a SE diagonal starting at (i, i)
draw : (Board, Piece, Int) -> Board
draw(b, p, i) = let triple = while i < 8 do (place(p, b, (i, i)), p, i + 1) in first(triple)

Any other triple would require its own first function.

Some possible fixes

We should improve this in the future. Some possibilities are listed below. The first two are presented some alternative options, but the third is probably what we should go with.

Martin's proposal for a generic tuple selector function:

I have another suggestion for a possible addition to BoGL (this is not urgent at all): We could add as built-in, generic functions tuple selectors with the following syntax and semantics:

    #i(e1,...,ek) --> ei

i is defined for tuples with max(2,i) or more components; it yields a type error otherwise.

We could even extend the syntax/semantics as follows:

    #(i1,...il)(e1,...,ek) --> (e{i1},...,e{il})

This extension is not really needed, but it makes some programs easier to write and saves programmers from constantly re-implementing tuple selectors.

alexgrejuc commented 3 years ago

I moved this to a wiki page since it is lower priority than fixing bogl implementation errors. Once we have fixed them up and have the bandwith to take this on, we can create an issue and assign it.