Skip to content

Instantly share code, notes, and snippets.

@louisswarren
Last active January 10, 2020 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louisswarren/d52c4b8e44a052d1d709384d5b7c96d7 to your computer and use it in GitHub Desktop.
Save louisswarren/d52c4b8e44a052d1d709384d5b7c96d7 to your computer and use it in GitHub Desktop.
Lift types by instance resolution in Agda
data ℕ : Set where
zero : ℕ
suc : ℕ → ℕ
data ℤ : Set where
nonneg : ℕ → ℤ
negsuc : ℕ → ℤ
data ℚ : Set where
frac_/suc_ : ℤ → ℕ → ℚ
record Embedding (A B : Set) : Set where
field
map : A → B
open Embedding renaming (map to _$_)
instance trivial↪ : {A : Set} → Embedding A A
trivial↪ $ x = x
-- Types can be lifted by finding their embedding via instance resolution
lift : {B : Set} {A : Set} ⦃ A↦B : Embedding A B ⦄ → A → B
lift ⦃ A↦B ⦄ x = A↦B $ x
instance
ℕ↪ℤ : Embedding ℕ ℤ
ℕ↪ℤ $ n = nonneg n
ℕ↪ℚ : Embedding ℕ ℚ
ℕ↪ℚ $ n = frac lift n /suc zero
ℤ↪ℚ : Embedding ℤ ℚ
ℤ↪ℚ $ a = frac a /suc zero
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment