www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Another kind of pointer arithmetic

reply Ianis Vasilev <ianis ivasilev.net> writes:
Somebody joked yesterday on Twitter about how pointer levels of 
indirection can be used to represent natural numbers and how we 
can do arithmetic with them (I can't find the post right now).

I decided to try and implement adding and multiplying levels of 
indirection:
https://gist.github.com/v--/5cbd0046be6fc5dbff9a9ad2f1717cde

I think it turned out fairly trivial.
Jun 26
next sibling parent reply welkam <wwwelkam gmail.com> writes:
On Friday, 26 June 2020 at 14:00:27 UTC, Ianis Vasilev wrote:
 Somebody joked yesterday on Twitter about how pointer levels of 
 indirection can be used to represent natural numbers and how we 
 can do arithmetic with them (I can't find the post right now).

 I decided to try and implement adding and multiplying levels of 
 indirection:
 https://gist.github.com/v--/5cbd0046be6fc5dbff9a9ad2f1717cde

 I think it turned out fairly trivial.
This reminded me when one person heard that mov instruction is turing complete so he implemented c compiler that emitted only mov instructions
Jun 26
parent Igor Shirkalin <isemsoft gmail.com> writes:
On Friday, 26 June 2020 at 18:18:34 UTC, welkam wrote:
 On Friday, 26 June 2020 at 14:00:27 UTC, Ianis Vasilev wrote:
 Somebody joked yesterday on Twitter about how pointer levels 
 of indirection can be used to represent natural numbers and 
 how we can do arithmetic with them (I can't find the post 
 right now).

 I decided to try and implement adding and multiplying levels 
 of indirection:
 https://gist.github.com/v--/5cbd0046be6fc5dbff9a9ad2f1717cde

 I think it turned out fairly trivial.
This reminded me when one person heard that mov instruction is turing complete so he implemented c compiler that emitted only mov instructions
The task from the school: Say, we have six intel 8086 cpu registers: ax, bx, cx, dx, si, di. Our task is to make the rotation: ah->al->bh->bl->ch->cl->dh->dl->ah. We can only use "move reg, reg" command. The si and di registers are undefined.
Jun 26
prev sibling next sibling parent Stanislav Blinov <stanislav.blinov gmail.com> writes:
On Friday, 26 June 2020 at 14:00:27 UTC, Ianis Vasilev wrote:

 I decided to try and implement adding and multiplying levels of 
 indirection:
 https://gist.github.com/v--/5cbd0046be6fc5dbff9a9ad2f1717cde
int****** 99 indirections on the wall...
Jun 26
prev sibling parent reply Superstar64 <Hexagonalstar64 gmail.com> writes:
On Friday, 26 June 2020 at 14:00:27 UTC, Ianis Vasilev wrote:
 Somebody joked yesterday on Twitter about how pointer levels of 
 indirection can be used to represent natural numbers and how we 
 can do arithmetic with them (I can't find the post right now).

 I decided to try and implement adding and multiplying levels of 
 indirection:
 https://gist.github.com/v--/5cbd0046be6fc5dbff9a9ad2f1717cde

 I think it turned out fairly trivial.
This appears to be a form of peano numbers. See https://wiki.haskell.org/Peano_numbers
Jun 27
parent reply Ianis Vasilev <ianis ivasilev.net> writes:
On Saturday, 27 June 2020 at 20:03:30 UTC, Superstar64 wrote:
 On Friday, 26 June 2020 at 14:00:27 UTC, Ianis Vasilev wrote:
 Somebody joked yesterday on Twitter about how pointer levels 
 of indirection can be used to represent natural numbers and 
 how we can do arithmetic with them (I can't find the post 
 right now).

 I decided to try and implement adding and multiplying levels 
 of indirection:
 https://gist.github.com/v--/5cbd0046be6fc5dbff9a9ad2f1717cde

 I think it turned out fairly trivial.
This appears to be a form of peano numbers. See https://wiki.haskell.org/Peano_numbers
Yes, the whole point was implementing the corresponding semigroup operations. The nice thing is that D templates allow mapping combinations of types and values to other types and values, which allows for a lot of flexibility (this is not specific to D, but it is nice nonetheless). For example, the defined templates can be viewed as mappings (or morphisms, or whatever) with the following signatures: IndirectionCounter: Types -> Values Add: Types × Types -> Types Mul: Types × Types -> Types You can map types to values via IndirectionCounter, but you can also create types from values by implementing the inverse of the IndirectionCounter template. I remember somebody talking about compile-time metaclasses some time ago, however I think that the type system is rich enough as it is right now.
Jun 27
parent Ianis Vasilev <ianis ivasilev.net> writes:
On Saturday, 27 June 2020 at 23:16:37 UTC, Ianis Vasilev wrote:
 On Saturday, 27 June 2020 at 20:03:30 UTC, Superstar64 wrote:
 [...]
Yes, the whole point was implementing the corresponding semigroup operations. [...]
I meant semiring operations, sorry.
Jun 27