0 | ||| Facilities for constructor unification and using its results
  1 | module Deriving.SpecialiseData.Unification
  2 |
  3 | import public Data.DPair -- workaround for idris-lang/Idris2#2439
  4 | import Deriving.SpecialiseData.Task
  5 | import Deriving.SpecialiseData.Helpers
  6 | import Deriving.SpecialiseData.Utils.Quantifiers
  7 | import public Language.Reflection.Unify
  8 |
  9 | ---------------------------
 10 | --- CONSTRUCTOR MAPPING ---
 11 | ---------------------------
 12 |
 13 | ||| Unification results for the whole type
 14 | public export
 15 | UniResults : Type
 16 | UniResults = List UnificationVerdict
 17 |
 18 | parameters (t : SpecTask)
 19 |   ||| Run monadic operation on all constructors of specialised type
 20 |   export
 21 |   mapCons :
 22 |     (f : (pCon : Con) ->
 23 |          (0 _ : ConArgsNamed pCon) =>
 24 |          r) ->
 25 |     List r
 26 |   mapCons f = do
 27 |     let adp = pushIn t.polyTy.cons t.polyTyNamed.tyConArgsNamed
 28 |     map (\(Element c pc) => f c) adp
 29 |
 30 |   ||| Map over all constructors for which unification succeeded
 31 |   export
 32 |   mapUCons :
 33 |     (f : UnificationResult ->
 34 |          (pCon : Con) ->
 35 |          (0 _ : ConArgsNamed pCon) =>
 36 |          Nat ->
 37 |          r) ->
 38 |     UniResults ->
 39 |     List r
 40 |   mapUCons f rs = do
 41 |     let adp = pushIn t.polyTy.cons t.polyTyNamed.tyConArgsNamed
 42 |     let f' : List (Subset Con ConArgsNamed) -> UniResults -> Nat -> List r
 43 |         f' (Element con _ :: xs) (Success res :: ys) n = f res con n :: f' xs ys (S n)
 44 |         f' (_ :: xs)             (_ :: ys)           n = f' xs ys n
 45 |         f' _ _ _ = []
 46 |     f' adp rs 0
 47 |
 48 |   ||| Run monadic operation on all pairs of specified and polymorphic constructors
 49 |   export
 50 |   map2UConsN :
 51 |     (f : UnificationResult ->
 52 |          (mt : TypeInfo) ->
 53 |          (0 _ : AllTyArgsNamed mt) =>
 54 |          (con : Con) ->
 55 |          (0 _ : ConArgsNamed con) =>
 56 |          (mcon : Con) ->
 57 |          (0 _ : ConArgsNamed mcon) =>
 58 |          ConMeta ->
 59 |          Nat ->
 60 |          r) ->
 61 |     UniResults ->
 62 |     (mt : TypeInfo) ->
 63 |     (0 _ : AllTyArgsNamed mt) =>
 64 |     TypeMeta ->
 65 |     List r
 66 |   map2UConsN f rs mt @{mtp} meta = do
 67 |     let p1 = pushIn t.polyTy.cons t.polyTyNamed.tyConArgsNamed
 68 |     let p2 = pushIn mt.cons mtp.tyConArgsNamed
 69 |     f' 0 p1 p2 rs meta.conMeta
 70 |     where
 71 |       f' :
 72 |         Nat ->
 73 |         List (Subset Con ConArgsNamed) ->
 74 |         List (Subset Con ConArgsNamed) ->
 75 |         UniResults ->
 76 |         List ConMeta ->
 77 |         List r
 78 |       f' n (Element con _ :: xs) (Element mcon _ :: ys) (Success res :: zs) (meta' :: metas) =
 79 |         f res mt con mcon meta' n :: f' (S n) xs ys zs metas
 80 |       f' n (_             :: xs)                    ys  (_:: zs) ms =
 81 |         f' n xs ys zs ms
 82 |       f' _ _ _ _ _ = []
 83 |
 84 | -------------------------------
 85 | --- CONSTRUCTOR UNIFICATION ---
 86 | -------------------------------
 87 |
 88 | ||| Run unification for a given polymorphic constructor
 89 | export
 90 | unifyCon :
 91 |   MonadLog m => (unifier : CanUnify m) =>
 92 |   (t : SpecTask) -> (con : Con) -> (0 conN : ConArgsNamed con) => m UnificationVerdict
 93 | unifyCon t con = logBounds Debug "specialiseData.unifyCon" [t, con] $ do
 94 |   let Element ca _ = fromListAll con.args @{conArgsNamed}
 95 |   let Element ta _ = fromListAll t.tqArgs @{t.tqArgsNamed}
 96 |   let uniTask =
 97 |     MkUniTask {lfv=_} ca con.type
 98 |               {rfv=_} ta t.fullInvocation
 99 |   logPoint DetailedDebug "specialiseData.unifyCon" [t, con] "Unifier task: \{show uniTask}"
100 |   uniRes <- unify uniTask
101 |   logValue DetailedDebug "specialiseData.unifyCon" [t, con] "Unifier output: \{show uniRes}" uniRes
102 |