| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Zero-cost coercion to cross the FFI boundary |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible #-} |
| 8 |
|
| 9 |
module Foreign.Haskell.Coerce where |
| 10 |
|
| 11 |
------------------------------------------------------------------------ |
| 12 |
-- Motivation |
| 13 |
|
| 14 |
-- Problem: No COMPILE directives for common inductive types |
| 15 |
|
| 16 |
-- In order to guarantee that the vast majority of the libary is |
| 17 |
-- considered safe by Agda, we had to remove the COMPILE pragmas |
| 18 |
-- associated to common inductive types. |
| 19 |
-- These directives cannot possibly be checked by the compiler and |
| 20 |
-- the user could define unsound mappings (e.g. swapping Bool's |
| 21 |
-- true and false). |
| 22 |
|
| 23 |
-- Solution: Essentially identical definitions + zero-cost coercions |
| 24 |
|
| 25 |
-- To solve this problem, we have introduced a number of essentially |
| 26 |
-- identical definitions in the Foreign.Haskell.* modules. However |
| 27 |
-- converting back and forth between the FFI-friendly type and its |
| 28 |
-- safe counterpart would take linear time. |
| 29 |
-- This module defines zero cost coercions between these types. |
| 30 |
|
| 31 |
------------------------------------------------------------------------ |
| 32 |
-- Definition |
| 33 |
|
| 34 |
open import Level using (Level; _⊔_) |
| 35 |
open import Agda.Builtin.Nat |
| 36 |
open import Agda.Builtin.Int |
| 37 |
|
| 38 |
import IO.Primitive.Core as STD |
| 39 |
import Data.List.Base as STD |
| 40 |
import Data.List.NonEmpty.Base as STD |
| 41 |
import Data.Maybe.Base as STD |
| 42 |
import Data.Product.Base as STD |
| 43 |
import Data.Sum.Base as STD |
| 44 |
|
| 45 |
import Foreign.Haskell.Pair as FFI |
| 46 |
import Foreign.Haskell.Either as FFI |
| 47 |
import Foreign.Haskell.List.NonEmpty as FFI |
| 48 |
|
| 49 |
private |
| 50 |
variable |
| 51 |
a b c d e f : Level |
| 52 |
A : Set a |
| 53 |
B : Set b |
| 54 |
C : Set c |
| 55 |
D : Set d |
| 56 |
|
| 57 |
-- We define a simple indexed datatype `Coercible`. A value of |
| 58 |
-- `Coercible A B` is a proof that `A` and `B` have the same underlying |
| 59 |
-- runtime representation. The only possible proof is an incantation |
| 60 |
-- from the implementer begging to be trusted. |
| 61 |
|
| 62 |
-- We need this type to be concrete so that overlapping instances can be |
| 63 |
-- checked for equality: we do not care what proof we get as long as we |
| 64 |
-- get one. |
| 65 |
|
| 66 |
-- We need for it to be a data type rather than a record type so that |
| 67 |
-- Agda does not mistakenly build arbitrary instances by η-expansion. |
| 68 |
|
| 69 |
data Coercible (A : Set a) (B : Set b) : Set where |
| 70 |
TrustMe : Coercible A B |
| 71 |
|
| 72 |
{-# FOREIGN GHC data AgdaCoercible l1 l2 a b = TrustMe #-} |
| 73 |
{-# COMPILE GHC Coercible = data AgdaCoercible (TrustMe) #-} |
| 74 |
|
| 75 |
-- Once we get our hands on a proof that `Coercible A B` we postulate |
| 76 |
-- that it is safe to convert an `A` into a `B`. This is done under the |
| 77 |
-- hood by using `unsafeCoerce`. |
| 78 |
|
| 79 |
postulate coerce : {{_ : Coercible A B}} → A → B |
| 80 |
|
| 81 |
{-# FOREIGN GHC import Unsafe.Coerce #-} |
| 82 |
{-# COMPILE GHC coerce = \ _ _ _ _ _ -> unsafeCoerce #-} |
| 83 |
|
| 84 |
------------------------------------------------------------------------ |
| 85 |
-- Unary and binary variants for covariant type constructors |
| 86 |
|
| 87 |
Coercible₁ : ∀ a c → (T : Set a → Set b) (U : Set c → Set d) → Set _ |
| 88 |
Coercible₁ _ _ T U = ∀ {A B} → {{_ : Coercible A B}} → Coercible (T A) (U B) |
| 89 |
|
| 90 |
Coercible₂ : ∀ a b d e → (T : Set a → Set b → Set c) (U : Set d → Set e → Set f) → Set _ |
| 91 |
Coercible₂ _ _ _ _ T U = ∀ {A B} → {{_ : Coercible A B}} → Coercible₁ _ _ (T A) (U B) |
| 92 |
|
| 93 |
------------------------------------------------------------------------ |
| 94 |
-- Instances |
| 95 |
|
| 96 |
-- Nat |
| 97 |
|
| 98 |
-- Our first instance reveals one of Agda's secrets: natural numbers are |
| 99 |
-- represented by (arbitrary precision) integers at runtime! Note that |
| 100 |
-- we may only coerce in one direction: arbitrary integers may actually |
| 101 |
-- be negative and will not do as mere natural numbers. |
| 102 |
|
| 103 |
instance |
| 104 |
|
| 105 |
nat-toInt : Coercible Nat Int |
| 106 |
nat-toInt = TrustMe |
| 107 |
|
| 108 |
-- We then proceed to state that data types from the standard library |
| 109 |
-- can be converted to their FFI equivalents which are bound to actual |
| 110 |
-- Haskell types. |
| 111 |
|
| 112 |
-- Product |
| 113 |
|
| 114 |
pair-toFFI : Coercible₂ a b c d STD._×_ FFI.Pair |
| 115 |
pair-toFFI = TrustMe |
| 116 |
|
| 117 |
pair-fromFFI : Coercible₂ a b c d FFI.Pair STD._×_ |
| 118 |
pair-fromFFI = TrustMe |
| 119 |
|
| 120 |
-- Sum |
| 121 |
|
| 122 |
either-toFFI : Coercible₂ a b c d STD._⊎_ FFI.Either |
| 123 |
either-toFFI = TrustMe |
| 124 |
|
| 125 |
either-fromFFI : Coercible₂ a b c d FFI.Either STD._⊎_ |
| 126 |
either-fromFFI = TrustMe |
| 127 |
|
| 128 |
-- NonEmpty |
| 129 |
|
| 130 |
nonEmpty-toFFI : Coercible₁ a b STD.List⁺ FFI.NonEmpty |
| 131 |
nonEmpty-toFFI = TrustMe |
| 132 |
|
| 133 |
nonEmpty-fromFFI : Coercible₁ a b FFI.NonEmpty STD.List⁺ |
| 134 |
nonEmpty-fromFFI = TrustMe |
| 135 |
|
| 136 |
-- We follow up with purely structural rules for builtin data types |
| 137 |
-- which already have known low-level representations. |
| 138 |
|
| 139 |
-- Maybe |
| 140 |
|
| 141 |
coerce-maybe : Coercible₁ a b STD.Maybe STD.Maybe |
| 142 |
coerce-maybe = TrustMe |
| 143 |
|
| 144 |
-- List |
| 145 |
|
| 146 |
coerce-list : Coercible₁ a b STD.List STD.List |
| 147 |
coerce-list = TrustMe |
| 148 |
|
| 149 |
-- IO |
| 150 |
|
| 151 |
coerce-IO : Coercible₁ a b STD.IO STD.IO |
| 152 |
coerce-IO = TrustMe |
| 153 |
|
| 154 |
-- Function |
| 155 |
-- Note that functions are contravariant in their domain. |
| 156 |
|
| 157 |
coerce-fun : {{_ : Coercible A B}} → Coercible₁ c d (λ C → B → C) (λ D → A → D) |
| 158 |
coerce-fun = TrustMe |
| 159 |
|
| 160 |
-- Finally we add a reflexivity proof to discharge all the dangling |
| 161 |
-- constraints involving type variables and concrete builtin types such |
| 162 |
-- as `Bool`. |
| 163 |
|
| 164 |
-- This rule overlaps with the purely structural ones: when attempting |
| 165 |
-- to prove `Coercible (List A) (List A)`, should Agda use the proof |
| 166 |
-- obtained by `coerce-refl` or the one obtained by `coerce-list coerce-refl`? |
| 167 |
-- Because we are using a datatype with a single constructor these |
| 168 |
-- distinctions do not matter: both proofs are definitionally equal. |
| 169 |
|
| 170 |
coerce-refl : Coercible A A |
| 171 |
coerce-refl = TrustMe |