Reflection.agda 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. module Common.Reflection where
  2. open import Agda.Builtin.Reflection public renaming
  3. ( arg-info to argInfo
  4. ; function to funDef
  5. ; data-type to dataDef
  6. ; record-type to recordDef
  7. ; agda-sort to sort
  8. ; name to qname
  9. ; absurd-clause to absurdClause
  10. ; pat-lam to extLam
  11. ; proj to projP
  12. ; instance′ to inst
  13. ; Visibility to Hiding
  14. ; Name to QName)
  15. open import Common.Level
  16. open import Common.Prelude hiding (_>>=_)
  17. pattern defaultModality = modality relevant quantity-ω
  18. pattern vArg x = arg (argInfo visible defaultModality) x
  19. pattern hArg x = arg (argInfo hidden defaultModality) x
  20. pattern iArg x = arg (argInfo inst defaultModality) x
  21. Args = List (Arg Term)
  22. data FunDef : Set where
  23. funDef : Type → List Clause → FunDef
  24. Tactic = Term → TC ⊤
  25. give : Term → Tactic
  26. give v = λ hole → unify hole v
  27. define : Arg QName → FunDef → TC ⊤
  28. define (arg i f) (funDef a cs) =
  29. bindTC (declareDef (arg i f) a) λ _ →
  30. defineFun f cs
  31. newMeta : Type → TC Term
  32. newMeta a = checkType unknown a
  33. numberOfParameters : QName → TC Nat
  34. numberOfParameters d =
  35. bindTC (getDefinition d) λ
  36. { (dataDef n _) → returnTC n
  37. ; _ → typeError (strErr "Cannot get parameters of non-data type" ∷ nameErr d ∷ [])
  38. }
  39. getConstructors : QName → TC (List QName)
  40. getConstructors d =
  41. bindTC (getDefinition d) λ
  42. { (dataDef _ cs) → returnTC cs
  43. ; (recordDef c _) → returnTC (c ∷ [])
  44. ; _ → typeError (strErr "Cannot get constructors of non-data or record type" ∷ nameErr d ∷ [])
  45. }
  46. infixl 1 _>>=_
  47. _>>=_ = bindTC