|
| 1 | +module Leios.Foreign.BaseTypes where |
| 2 | + |
| 3 | +-- TODO: copied from the formal-ledger project for now |
| 4 | +-- Added: * TotalMap |
| 5 | + |
| 6 | +open import Data.Rational |
| 7 | + |
| 8 | +open import Leios.Prelude |
| 9 | + |
| 10 | +open import Data.Fin |
| 11 | +open import Function.Related.TypeIsomorphisms |
| 12 | +open import Relation.Binary.Structures |
| 13 | + |
| 14 | +open import Tactic.Derive.Convertible |
| 15 | +open import Tactic.Derive.HsType |
| 16 | + |
| 17 | +open import Class.Convertible |
| 18 | +open import Class.Decidable.Instances |
| 19 | +open import Class.HasHsType |
| 20 | + |
| 21 | +open import Leios.Foreign.HsTypes as F |
| 22 | +open import Leios.Foreign.Util |
| 23 | +open import Foreign.Haskell |
| 24 | + |
| 25 | +instance |
| 26 | + iConvTop = Convertible-Refl {⊤} |
| 27 | + iConvNat = Convertible-Refl {ℕ} |
| 28 | + iConvString = Convertible-Refl {String} |
| 29 | + iConvBool = Convertible-Refl {Bool} |
| 30 | + |
| 31 | +instance |
| 32 | + |
| 33 | + -- * Unit and empty |
| 34 | + |
| 35 | + HsTy-⊥ = MkHsType ⊥ F.Empty |
| 36 | + Conv-⊥ = autoConvert ⊥ |
| 37 | + |
| 38 | + HsTy-⊤ = MkHsType ⊤ ⊤ |
| 39 | + |
| 40 | + -- * Rational numbers |
| 41 | + |
| 42 | + HsTy-Rational = MkHsType ℚ F.Rational |
| 43 | + Conv-Rational : HsConvertible ℚ |
| 44 | + Conv-Rational = λ where |
| 45 | + .to (mkℚ n d _) → n F., suc d |
| 46 | + .from (n F., zero) → 0ℚ -- TODO is there a safer way to do this? |
| 47 | + .from (n F., (suc d)) → n Data.Rational./ suc d |
| 48 | + |
| 49 | + -- * Maps and Sets |
| 50 | + |
| 51 | + HsTy-HSSet : ∀ {A} → ⦃ HasHsType A ⦄ → HasHsType (ℙ A) |
| 52 | + HsTy-HSSet {A} = MkHsType _ (F.HSSet (HsType A)) |
| 53 | + |
| 54 | + Conv-HSSet : ∀ {A} ⦃ _ : HasHsType A ⦄ |
| 55 | + → ⦃ HsConvertible A ⦄ |
| 56 | + → HsConvertible (ℙ A) |
| 57 | + Conv-HSSet = λ where |
| 58 | + .to → F.MkHSSet ∘ to ∘ setToList |
| 59 | + .from → fromList ∘ from ∘ F.HSSet.elems |
| 60 | + |
| 61 | + Convertible-FinSet : Convertible₁ ℙ_ List |
| 62 | + Convertible-FinSet = λ where |
| 63 | + .to → map to ∘ setToList |
| 64 | + .from → fromList ∘ map from |
| 65 | + |
| 66 | + Convertible-Map : ∀ {K K' V V'} → ⦃ DecEq K ⦄ |
| 67 | + → ⦃ Convertible K K' ⦄ → ⦃ Convertible V V' ⦄ |
| 68 | + → Convertible (K ⇀ V) (List $ Pair K' V') |
| 69 | + Convertible-Map = λ where |
| 70 | + .to → to ∘ proj₁ |
| 71 | + .from → fromListᵐ ∘ map from |
| 72 | + |
| 73 | + HsTy-Map : ∀ {A B} → ⦃ HasHsType A ⦄ → ⦃ HasHsType B ⦄ → HasHsType (A ⇀ B) |
| 74 | + HsTy-Map {A} {B} = MkHsType _ (F.HSMap (HsType A) (HsType B)) |
| 75 | + |
| 76 | + Conv-HSMap : ∀ {A B} ⦃ _ : HasHsType A ⦄ ⦃ _ : HasHsType B ⦄ |
| 77 | + → ⦃ DecEq A ⦄ |
| 78 | + → ⦃ HsConvertible A ⦄ |
| 79 | + → ⦃ HsConvertible B ⦄ |
| 80 | + → HsConvertible (A ⇀ B) |
| 81 | + Conv-HSMap = λ where |
| 82 | + .to → F.MkHSMap ∘ to |
| 83 | + .from → from ∘ F.HSMap.assocList |
| 84 | + |
| 85 | +record Listable (A : Type) : Type where |
| 86 | + field |
| 87 | + listing : ℙ A |
| 88 | + complete : ∀ {a : A} → a ∈ listing |
| 89 | + |
| 90 | +totalDec : ∀ {A B : Type} → ⦃ DecEq A ⦄ → ⦃ Listable A ⦄ → {R : Rel A B} → Dec (total R) |
| 91 | +totalDec {A} {B} {R} with all? (_∈? dom R) |
| 92 | +... | yes p = yes λ {a} → p {a} ((Listable.complete it) {a}) |
| 93 | +... | no ¬p = no λ x → ¬p λ {a} _ → x {a} |
| 94 | + |
| 95 | +instance |
| 96 | + |
| 97 | + total? : ∀ {A B : Type} → ⦃ DecEq A ⦄ → ⦃ Listable A ⦄ → {R : Rel A B} → ({a : A} → a ∈ dom R) ⁇ |
| 98 | + total? = ⁇ totalDec |
| 99 | + |
| 100 | + Convertible-TotalMap : ∀ {K K' V V'} → ⦃ DecEq K ⦄ → ⦃ Listable K ⦄ |
| 101 | + → ⦃ Convertible K K' ⦄ → ⦃ Convertible V V' ⦄ |
| 102 | + → Convertible (TotalMap K V) (List $ Pair K' V') |
| 103 | + Convertible-TotalMap {K} = λ where |
| 104 | + .to → to ∘ TotalMap.rel |
| 105 | + .from → λ x → |
| 106 | + let (r , l) = fromListᵐ (map from x) |
| 107 | + in case (¿ total r ¿) of λ where |
| 108 | + (yes p) → record { rel = r ; left-unique-rel = l ; total-rel = p } |
| 109 | + (no p) → error "Expected total map" |
| 110 | + |
| 111 | + HsTy-TotalMap : ∀ {A B} → ⦃ HasHsType A ⦄ → ⦃ HasHsType B ⦄ → HasHsType (TotalMap A B) |
| 112 | + HsTy-TotalMap {A} {B} = MkHsType _ (F.HSMap (HsType A) (HsType B)) |
| 113 | + |
| 114 | + Conv-HSTotalMap : ∀ {A B} ⦃ _ : HasHsType A ⦄ ⦃ _ : HasHsType B ⦄ |
| 115 | + → ⦃ DecEq A ⦄ |
| 116 | + → ⦃ Listable A ⦄ |
| 117 | + → ⦃ HsConvertible A ⦄ |
| 118 | + → ⦃ HsConvertible B ⦄ |
| 119 | + → HsConvertible (TotalMap A B) |
| 120 | + Conv-HSTotalMap = λ where |
| 121 | + .to → MkHSMap ∘ to |
| 122 | + .from → from ∘ F.HSMap.assocList |
| 123 | + |
| 124 | + -- * ComputationResult |
| 125 | + |
| 126 | + open import Class.Computational as C |
| 127 | + |
| 128 | + HsTy-ComputationResult : ∀ {l} {Err} {A : Type l} |
| 129 | + → ⦃ HasHsType Err ⦄ → ⦃ HasHsType A ⦄ |
| 130 | + → HasHsType (C.ComputationResult Err A) |
| 131 | + HsTy-ComputationResult {Err = Err} {A} = MkHsType _ (F.ComputationResult (HsType Err) (HsType A)) |
| 132 | + |
| 133 | + Conv-ComputationResult : ConvertibleType C.ComputationResult F.ComputationResult |
| 134 | + Conv-ComputationResult = autoConvertible |
0 commit comments