Skip to content

No defined implementation Cast ty ty #3479

@Termina1

Description

@Termina1

For example, there is a code that does something like:

let x : Integer = 10 in the Integer (cast x)

And I get error like:

Can't cast from Integer to Integer 

Which is strange, but we could solve this with this implementation:

Cast ty ty where
    cast = id 

I encountered this problem while doing exercise for the book Type Driven Development with Idris.
I need to implement Cast interface for some custom type.

data Expr num = Val num
              | Add (Expr num) (Expr num)
              | Sub (Expr num) (Expr num)
              | Mul (Expr num) (Expr num)
              | Div (Expr num) (Expr num)
              | Abs (Expr num)
eval : (Neg num, Integral num) => Expr num -> num
eval (Val x) = x
eval (Add x y) = eval x + eval y
eval (Sub x y) = eval x - eval y
eval (Mul x y) = eval x * eval y
eval (Div x y) = eval x `div` eval y
eval (Abs x) = abs (eval x)

I defined it as follows:

(Integral ty, Neg ty, Cast ty num) => Cast (Expr ty) num where
    cast orig = cast (eval orig) 

But the test example from the book failed because of the reason I described earlier:

let x : Expr Integer = 6 * 3 + 12 in the Integer (cast x) 

Am I getting something wrong? Why casting from type ty to ty haven't been implemented as a part of standard library for idris?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions