www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compiler/Phobos/Types problem =?UTF-8?Q?=E2=80=94?= panic level due

reply Russel Winder <russel winder.org.uk> writes:
Hi,

I had merrily asumed I could implement nth Fibonacci number with:

    takeOne(drop(recurrence!((a, n) =3D> a[n-1] + a[n-2])(zero, one), n)).f=
ront

where zero and one are of type BigInt, and n is of type size_t. However bot=
h
dmd and ldc2 complain saying:

/usr/include/dmd/phobos/std/range/package.d(5770): Error: template std.bigi=
nt.BigInt.opAssign cannot deduce function from argument types !()(immutable=
(BigInt)) immutable, candidates are:
/usr/include/dmd/phobos/std/bigint.d(178):        std.bigint.BigInt.opAssig=
n(T)(T x) if (isIntegral!T)
/usr/include/dmd/phobos/std/bigint.d(194):        std.bigint.BigInt.opAssig=
n(T : BigInt)(T x)
/usr/include/dmd/phobos/std/range/package.d(5770): Error: template std.bigi=
nt.BigInt.opAssign cannot deduce function from argument types !()(immutable=
(BigInt)) immutable, candidates are:
/usr/include/dmd/phobos/std/bigint.d(178):        std.bigint.BigInt.opAssig=
n(T)(T x) if (isIntegral!T)
/usr/include/dmd/phobos/std/bigint.d(194):        std.bigint.BigInt.opAssig=
n(T : BigInt)(T x)
fibonacci.d(25): Error: template instance `fibonacci.declarative.recurrence=
!((a, n) =3D> a[n - 1] + a[n - 2], immutable(BigInt), immutable(BigInt))` e=
rror instantiating
/usr/include/dmd/phobos/std/range/package.d(5720): Error: template std.bigi=
nt.BigInt.opAssign cannot deduce function from argument types !()(immutable=
(BigInt)) immutable, candidates are:
/usr/include/dmd/phobos/std/bigint.d(178):        std.bigint.BigInt.opAssig=
n(T)(T x) if (isIntegral!T)
/usr/include/dmd/phobos/std/bigint.d(194):        std.bigint.BigInt.opAssig=
n(T : BigInt)(T x)
/usr/include/dmd/phobos/std/range/package.d(3177): Error: template instance=
 `std.range.primitives.isInputRange!(Recurrence!(__lambda2, immutable(BigIn=
t), 2LU))` error instantiating
fibonacci.d(25): Error: template std.range.drop cannot deduce function from=
 argument types !()(Recurrence!(__lambda2, immutable(BigInt), 2LU), immutab=
le(ulong)), candidates are:
/usr/include/dmd/phobos/std/range/package.d(3176):        std.range.drop(R)=
(R range, size_t n) if (isInputRange!R)

I am now at the WTF stage =E2=80=93 how can I show this example on Thursday=
 in my
DevoxxUK presentation?=20

I am close to giving up and imbibing of too much Pernod.

--=20
Russel.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Dr Russel Winder      t: +44 20 7585 2200
41 Buckmaster Road    m: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk
May 05 2019
parent reply lithium iodate <whatdoiknow doesntexist.net> writes:
On Sunday, 5 May 2019 at 18:53:08 UTC, Russel Winder wrote:
 Hi,

 I had merrily asumed I could implement nth Fibonacci number 
 with:

     takeOne(drop(recurrence!((a, n) => a[n-1] + a[n-2])(zero, 
 one), n)).front

 where zero and one are of type BigInt, and n is of type size_t. 
 However both dmd and ldc2 complain saying:
[…]
 I am now at the WTF stage – how can I show this example on 
 Thursday in my DevoxxUK presentation?

 I am close to giving up and imbibing of too much Pernod.
`recurrence` takes the `CommonType` of the initial values and declares its internal state as an array of this type, however when at least one of the values is const or immutable, the `CommonType` is const too, or even immutable in the case when all values are immutable. The state being const/immutable means that the following step, initializing it, can't work, since, well, the array cannot be modified (hence the errors). I'd say this can be considered to be a bug with `recurrence`. You can solve this issue by constructing and passing mutable versions of `one` and `zero` to `recurrence`.
May 05 2019
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Sunday, 5 May 2019 at 19:18:47 UTC, lithium iodate wrote:
 On Sunday, 5 May 2019 at 18:53:08 UTC, Russel Winder wrote:
 Hi,

 I had merrily asumed I could implement nth Fibonacci number 
 with:

     takeOne(drop(recurrence!((a, n) => a[n-1] + a[n-2])(zero, 
 one), n)).front

 where zero and one are of type BigInt, and n is of type 
 size_t. However both dmd and ldc2 complain saying:
[…]
 I am now at the WTF stage – how can I show this example on 
 Thursday in my DevoxxUK presentation?

 I am close to giving up and imbibing of too much Pernod.
`recurrence` takes the `CommonType` of the initial values and declares its internal state as an array of this type, however when at least one of the values is const or immutable, the `CommonType` is const too, or even immutable in the case when all values are immutable. The state being const/immutable means that the following step, initializing it, can't work, since, well, the array cannot be modified (hence the errors). I'd say this can be considered to be a bug with `recurrence`. You can solve this issue by constructing and passing mutable versions of `one` and `zero` to `recurrence`.
Yep https://run.dlang.io/is/XsLrRz works for me, https://run.dlang.io/is/KxY0e9 doesn't.
May 05 2019
parent reply Russel Winder <russel winder.org.uk> writes:
On Sunday, 5 May 2019 at 19:34:05 UTC, Nicholas Wilson wrote:
 On Sunday, 5 May 2019 at 19:18:47 UTC, lithium iodate wrote:
 [...]
Yep https://run.dlang.io/is/XsLrRz works for me, https://run.dlang.io/is/KxY0e9 doesn't.
Thanks people. I now have a working code. :-)
May 06 2019
parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Monday, 6 May 2019 at 13:05:27 UTC, Russel Winder wrote:
 On Sunday, 5 May 2019 at 19:34:05 UTC, Nicholas Wilson wrote:
 On Sunday, 5 May 2019 at 19:18:47 UTC, lithium iodate wrote:
 [...]
Yep https://run.dlang.io/is/XsLrRz works for me, https://run.dlang.io/is/KxY0e9 doesn't.
Thanks people. I now have a working code. :-)
pretty please show people it with UFCS: recurrence!((a, n) => a[n-1] + a[n-2])(zero, one) .dropExactly(n) .front
May 06 2019
parent reply Russel Winder <russel winder.org.uk> writes:
On Mon, 2019-05-06 at 15:53 +0000, John Colvin via Digitalmars-d-learn
wrote:
 [=E2=80=A6]
=20
 pretty please show people it with UFCS:
=20
 recurrence!((a, n) =3D> a[n-1] + a[n-2])(zero, one)
      .dropExactly(n)
      .front
Any particular reason for preferring this form over the original? --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk
May 08 2019
next sibling parent reply Alex <sascha.orlov gmail.com> writes:
On Wednesday, 8 May 2019 at 11:53:34 UTC, Russel Winder wrote:
 On Mon, 2019-05-06 at 15:53 +0000, John Colvin via 
 Digitalmars-d-learn wrote:
 […]
 
 pretty please show people it with UFCS:
 
 recurrence!((a, n) => a[n-1] + a[n-2])(zero, one)
      .dropExactly(n)
      .front
Any particular reason for preferring this form over the original?
For example, It is more readable, as the order of execution is unwinded.
May 08 2019
parent Russel Winder <russel winder.org.uk> writes:
On Wed, 2019-05-08 at 11:56 +0000, Alex via Digitalmars-d-learn wrote:
 On Wednesday, 8 May 2019 at 11:53:34 UTC, Russel Winder wrote:
 On Mon, 2019-05-06 at 15:53 +0000, John Colvin via=20
 Digitalmars-d-learn wrote:
 [=E2=80=A6]
=20
 pretty please show people it with UFCS:
=20
 recurrence!((a, n) =3D> a[n-1] + a[n-2])(zero, one)
      .dropExactly(n)
      .front
=20 Any particular reason for preferring this form over the=20 original?
=20 For example, It is more readable, as the order of execution is=20 unwinded.
I think there is an individual element to this, and what people are used to. However I have taken John's request and changed the line he wanted changed. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk
May 08 2019
prev sibling parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Wednesday, 8 May 2019 at 11:53:34 UTC, Russel Winder wrote:
 On Mon, 2019-05-06 at 15:53 +0000, John Colvin via 
 Digitalmars-d-learn wrote:
 […]
 
 pretty please show people it with UFCS:
 
 recurrence!((a, n) => a[n-1] + a[n-2])(zero, one)
      .dropExactly(n)
      .front
Any particular reason for preferring this form over the original?
Not big benefit in this case, very big benefit with longer chains. It reads in the order of operations, as opposed to inside out.
May 09 2019
parent Russel Winder <russel winder.org.uk> writes:
On Thu, 2019-05-09 at 08:33 +0000, John Colvin via Digitalmars-d-learn
wrote:
 [=E2=80=A6]
=20
 Not big benefit in this case, very big benefit with longer chains.
=20
 It reads in the order of operations, as opposed to inside out.
John, Coming from a Haskell/Lisp background to declarative expression, I do not have a problem with the function application approach. However if the audience are more likely to understand the method application approach (and this is fundamentally a Java audience) then that is the way of should be shown. I showed the variant you suggested. :-) --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D Dr Russel Winder t: +44 20 7585 2200 41 Buckmaster Road m: +44 7770 465 077 London SW11 1EN, UK w: www.russel.org.uk
May 09 2019