Chymyst / curryhoward

Automatic code generation for Scala functions and expressions via the Curry-Howard isomorphism
Apache License 2.0
259 stars 16 forks source link

Generate identity functions for Unit values #20

Open winitzki opened 6 years ago

winitzki commented 6 years ago

When generating a function such as Option[A] => Option[A], the preferred implementation would be x => x. Sometimes code such as this is generated:

Warning:(141, 89) type (A ⇒ Option[B]{None.type + Some[B]}) ⇒ (B ⇒ Option[C]{None.type + Some[C]}) ⇒ A ⇒ Option[C]{None.type + Some[C]} has 2 implementations (laws need checking?):
 a ⇒ b ⇒ c ⇒ a c match { d ⇒ (None() + 0); e ⇒ b e.value } [score: (0,0.5,0.0,0,0)];
 a ⇒ b ⇒ c ⇒ (None() + 0) [score: (3,0.0,0.0,0,0)].
    def kleisliOptCompose[A, B, C](f: A ⇒ Option[B], g: B ⇒ Option[C]): A ⇒ Option[C] = implement

It is better to generate code such as x match { d => d }; ... instead of x match { d => None() + 0 }; ... when d is itself already None(). This code would have a lower information loss score.

winitzki commented 6 years ago

This simplification seems to be problematic; it prevents correct typing of case objects for the example Option[Option[A]] => Option[Option[A]].