2 | module Language.Reflection.Compat.Constr
4 | import public Data.List.Elem
5 | import public Data.So
7 | import public Language.Reflection.Compat
8 | import Language.Reflection.Expr
10 | import public Syntax.IHateParens.List
19 | normaliseCon : Elaboration m => Con -> m Con
20 | normaliseCon orig@(MkCon n args ty) = uncurry (MkCon n) . unPi <$> normaliseAsType (piAll ty args) `try` pure orig
27 | conSubexprs : Con -> List TTImp
28 | conSubexprs con = map type con.args ++ map getExpr (snd $
unAppAny con.type)
32 | (.sig) : Con -> TTImp
33 | con.sig = piAll con.type con.args
38 | con.iTy = mkTy .| dropNS con.name .| con.sig
47 | record ConArg (0 con : Con) where
48 | constructor MkConArg
49 | conArgIdx : Fin con.args.length
54 | fromInteger : (x : Integer) -> (0 _ : So $
integerLessThanNat x con.args.length) => ConArg con
55 | fromInteger x = MkConArg $
fromInteger x
57 | elemToFin : Elem e xs -> Fin xs.length
59 | elemToFin (There x) = FS $
elemToFin x
62 | fromName : (n : Name) -> Elem (Just n) (map Arg.name con.args) => ConArg con
63 | fromName _ @{e} = MkConArg $
rewrite sym $
lengthMap con.args in elemToFin e
68 | fromString : (n : String) -> Elem (Just $
fromString n) (map Arg.name con.args) => ConArg con
69 | fromString n = fromName $
fromString n
73 | dataCon : Name -> Elab Name
75 | [n] <- mapMaybe id <$> (traverse isAccessibleDataCon =<< getInfo n)
76 | | [] => fail "Not found data constructor `\{n}`"
77 | | ns => fail "Ambiguous data constructors: \{joinBy ", " $ show <$> ns}"
81 | isAccessibleDataCon : (Name, NameInfo) -> Elab $
Maybe Name
82 | isAccessibleDataCon (n, MkNameInfo $
DataCon {}) = (catch (check {expected=()} `(let x = ~(var n) in ()
)) $> n) @{Compose}
83 | isAccessibleDataCon _ = pure Nothing
85 | export %macro (.dataCon) : Name -> Elab Name;
(.dataCon) = dataCon
90 | record IsConstructor (0 n : Name) where
95 | namespace IsConstructor
97 | data GenuineProof : IsConstructor n -> Type where
98 | ItIsGenuine : GenuineProof iscn
101 | itIsConstructor : {n : Name} -> Elab (con : IsConstructor n ** GenuineProof con)
102 | itIsConstructor = do
105 | | False => fail "Name `\{show n}` is not a full name, use either `\{show cn}` or macro `.dataCon`"
107 | let (IVar _ ty, _) = unAppAny con.type
108 | | (lhs, _) => fail "Can't get type name: \{show lhs}"
110 | pure (
ItIsCon ty con ** ItIsGenuine)