Created
June 19, 2020 20:11
-
-
Save derrickturk/e880d3d652c2f0c4ad9dd81a148500dc to your computer and use it in GitHub Desktop.
ad-hoc overloading with interfaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Vect | |
%hide List.Nil | |
interface Addable (a : Type) (b : Type) (c : Type) | a, b where | |
add : a -> b -> c | |
-- it's redundant to case-split this for vectors, but just for example | |
Addable (Vect n a) (Vect Z a) (Vect n a) where | |
add xs _ = xs | |
Addable (Vect n a) (Vect (S m) a) (Vect (n + S m) a) where | |
add xs ys = xs ++ ys | |
Addable a (Vect n a) (Vect (S n) a) where | |
add x xs = x::xs | |
ex1 : Vect 5 Int | |
ex1 = [1, 2, 3, 4, 5] | |
ex2 : Vect 5 Int | |
ex2 = add ex1 [] | |
ex3 : Vect 10 Int | |
ex3 = add ex1 ex1 | |
ex4 : Vect 6 Int | |
ex4 = add (the Int 0) ex1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment