0 | ||| Logic for generating interface implementation
  1 | module Deriving.SpecialiseData.Codegen.InterfaceImpls
  2 |
  3 | import Deriving.SpecialiseData.Common
  4 | import Deriving.SpecialiseData.Helpers
  5 | import Deriving.SpecialiseData.Task
  6 | import Deriving.SpecialiseData.TaskFormation
  7 | import Deriving.SpecialiseData.Unification
  8 | import Deriving.SpecialiseData.Utils.Quantifiers
  9 | import Deriving.SpecialiseData.Utils.TypeInfo
 10 | import Language.Reflection.Compat
 11 | import Language.Reflection.Expr
 12 |
 13 | -------------------------------------
 14 | --- DECIDABLE EQUALITY DERIVATION ---
 15 | -------------------------------------
 16 |
 17 | parameters (t : SpecTask)
 18 |   ||| Decidable equality clause
 19 |   mkDecEqImplClause : Clause
 20 |   mkDecEqImplClause =
 21 |     let mToPImpl = var $ inGenNS t "mToPImpl"
 22 |     in `(decEqImpl x1 x2)
 23 |         .=
 24 |         `(decEqInj {f = ~mToPImpl} $
 25 |           let x1' : ~(t.fullInvocation);
 26 |               x1' = (~mToPImpl x1);
 27 |               x2' : ~(t.fullInvocation);
 28 |               x2' = (~mToPImpl x2);
 29 |           in decEq x1' x2')
 30 |
 31 |   ||| Derive decidable equality
 32 |   export
 33 |   mkDecEqDecls :
 34 |     UniResults ->
 35 |     (mt : TypeInfo) ->
 36 |     (0 _ : AllTyArgsNamed mt) =>
 37 |     List Decl
 38 |   mkDecEqDecls _ ti = do
 39 |     [ def "decEqImpl" [ mkDecEqImplClause ]
 40 |     , def "decEq'"
 41 |       [ `(decEq') .= `((Mk DecEq) ~(var $ inGenNS t "decEqImpl")) ]
 42 |     ]
 43 |
 44 | -----------------------
 45 | --- SHOW DERIVATION ---
 46 | -----------------------
 47 |
 48 | parameters (t : SpecTask)
 49 |   ||| Derive Show implementation via cast
 50 |   export
 51 |   mkShowDecls :
 52 |     UniResults ->
 53 |     (mt : TypeInfo) ->
 54 |     (0 _ : AllTyArgsNamed mt) =>
 55 |     List Decl
 56 |   mkShowDecls _ ti = do
 57 |     let mToPImpl = var $ inGenNS t "mToPImpl"
 58 |     [ def "showImpl" [ `(showImpl x) .= `(show $ ~mToPImpl x) ]
 59 |     , def "showPrecImpl"
 60 |       [ `(showPrecImpl p x) .= `(showPrec p $ ~mToPImpl x) ]
 61 |     , def "show'" [ `(show') .= `(MkShow showImpl showPrecImpl) ]
 62 |     ]
 63 |
 64 | ---------------------
 65 | --- EQ DERIVATION ---
 66 | ---------------------
 67 |
 68 | parameters (t : SpecTask)
 69 |   ||| Derive Eq implementation via cast
 70 |   export
 71 |   mkEqDecls :
 72 |     UniResults ->
 73 |     (mt : TypeInfo) ->
 74 |     (0 _ : AllTyArgsNamed mt) =>
 75 |     List Decl
 76 |   mkEqDecls _ ti = do
 77 |     let mToPImpl = var $ inGenNS t "mToPImpl"
 78 |     [ def "eqImpl" [ `(eqImpl x y) .= `((~mToPImpl x) == (~mToPImpl y)) ]
 79 |     , def "neqImpl" [ `(neqImpl x y) .= `((~mToPImpl x) /= (~mToPImpl y)) ]
 80 |     , def "eq'" [ `(eq') .= `(MkEq eqImpl neqImpl) ]
 81 |     ]
 82 |
 83 |
 84 | -----------------------------
 85 | --- FROMSTRING DERIVATION ---
 86 | -----------------------------
 87 |
 88 | parameters (t : SpecTask)
 89 |   export
 90 |   mkFromStringDecls :
 91 |     (mt : TypeInfo) ->
 92 |     (0 _ : AllTyArgsNamed mt) =>
 93 |     List Decl
 94 |   mkFromStringDecls ti = do
 95 |     let pToMImpl = var $ inGenNS t "pToMImpl"
 96 |     [ def "fromStringImpl"
 97 |       [ `(fromStringImpl @{fs} s) .= `(~pToMImpl $ fromString @{fs} s) ]
 98 |     , def "fromString'"
 99 |         [ `(fromString' @{fs}) .= `(MkFromString $ ~(var $ inGenNS t "fromStringImpl") @{fs}) ]
100 |     ]
101 |
102 | ----------------------
103 | --- NUM DERIVATION ---
104 | ----------------------
105 |
106 | parameters (t : SpecTask)
107 |   export
108 |   mkNumDecls :
109 |     (mt : TypeInfo) ->
110 |     (0 _ : AllTyArgsNamed mt) =>
111 |     List Decl
112 |   mkNumDecls ti = do
113 |     let pToMImpl = var $ inGenNS t "pToMImpl"
114 |     let mToPImpl = var $ inGenNS t "mToPImpl"
115 |     [ def "numImpl"
116 |       [ `(numImpl @{fs} s) .= `(~pToMImpl $ Num.fromInteger @{fs} s) ]
117 |     , def "plusImpl"
118 |         [ `(plusImpl @{fs} a b ) .= `(~pToMImpl $ (+) @{fs} (~mToPImpl a) (~mToPImpl b)) ]
119 |     , def "starImpl"
120 |         [ `(starImpl @{fs} a b ) .= `(~pToMImpl $ (*) @{fs} (~mToPImpl a) (~mToPImpl b)) ]
121 |     , def "num'"
122 |         [ `(num' @{fs}) .=
123 |             `(MkNum
124 |               (~(var $ inGenNS t "plusImpl") @{fs})
125 |               (~(var $ inGenNS t "starImpl") @{fs})
126 |               (~(var $ inGenNS t "numImpl") @{fs}))
127 |         ]
128 |     ]
129 |