Skip to content

Instantly share code, notes, and snippets.

@rntz
Created August 4, 2017 00:58
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 rntz/e280c8810bd2ffc812988515c5265294 to your computer and use it in GitHub Desktop.
Save rntz/e280c8810bd2ffc812988515c5265294 to your computer and use it in GitHub Desktop.
module Cast where
open import Level
record Convert {i j} (A : Set i) (B : Set j) : Set (i ⊔ j) where
field convert : A -> B
open Convert public
open Convert {{...}} public using () renaming (convert to cast)
instance
convert-id : ∀{i a} -> Convert {i} a a
convert-id .convert x = x
-- Use Cast and Cast: to define casts which compose automatically.
Cast : ∀{i j} k (A : Set i) (B : Set j) -> Set (i ⊔ j ⊔ suc k)
Cast k A B = ∀{src : Set k} {{C : Convert src A}} -> Convert src B
Cast: : ∀{i j k A B} -> (A -> B) -> Cast {i}{j} k A B
Cast: f .convert x = f (cast x)
-- A common use-case: turning things into sets.
obj : ∀{i j A} {{C : Convert {i} A (Set j)}} -> A -> Set j
obj = cast
private
module Examples where
postulate
ℕ ℤ ℝ : Set
nat->int : ℕ -> ℤ
int->real : ℤ -> ℝ
instance
cast:nat->int : ∀{i} -> Cast i ℕ ℤ
cast:nat->int = Cast: nat->int
cast:int->real : ∀{i} -> Cast i ℤ ℝ
cast:int->real = Cast: int->real
nop : ℕ -> ℕ
duh : ℕ -> ℤ
nat->real : ℕ -> ℝ
nop = cast; duh = cast; nat->real = cast
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment