2
{-# LANGUAGE FlexibleInstances #-}
3
{-# LANGUAGE TypeSynonymInstances #-}
3
5
module Agda.TypeChecking.Quote where
5
7
import Control.Applicative
9
import Data.Maybe (fromMaybe)
7
11
import Agda.Syntax.Position
8
12
import Agda.Syntax.Literal
9
import Agda.Syntax.Internal
13
import Agda.Syntax.Internal as I
10
14
import Agda.Syntax.Common
16
import {-# SOURCE #-} Agda.TypeChecking.Datatypes
12
17
import Agda.TypeChecking.Monad
13
18
import Agda.TypeChecking.Monad.Builtin
14
19
import Agda.TypeChecking.Reduce
15
20
import Agda.TypeChecking.Pretty
16
21
import Agda.TypeChecking.Substitute
23
import Agda.Utils.Monad
18
25
#include "../undefined.h"
19
26
import Agda.Utils.Impossible
52
60
quoteRelevance NonStrict = relevant
53
61
quoteRelevance Forced = relevant
54
62
quoteRelevance UnusedArg = relevant
63
quoteColors _ = nil -- TODO guilhem
64
quoteArgInfo (ArgInfo h r cs) = arginfo @@ quoteHiding h
55
67
quoteLit (LitInt _ n) = iterate suc zero !! fromIntegral n
56
68
quoteLit _ = unsupported
57
69
-- We keep no ranges in the quoted term, so the equality on terms
59
71
quoteSortLevelTerm (Max []) = setLit @@ Lit (LitInt noRange 0)
60
72
quoteSortLevelTerm (Max [ClosedLevel n]) = setLit @@ Lit (LitInt noRange n)
61
73
quoteSortLevelTerm (Max [Plus 0 (NeutralLevel v)]) = set @@ quote v
62
quoteSortLevelTerm _ = unsupported
74
quoteSortLevelTerm _ = unsupportedSort
63
75
quoteSort (Type t) = quoteSortLevelTerm t
64
76
quoteSort Prop = unsupportedSort
65
77
quoteSort Inf = unsupportedSort
67
79
quoteType (El s t) = el @@ quoteSort s @@ quote t
69
81
list (a : as) = cons @@ a @@ list as
70
zero = con @@ quoteName z @@ nil
71
suc n = con @@ quoteName s @@ list [arg @@ visible @@ relevant @@ n]
72
quoteDom q (Dom h r t) = arg @@ quoteHiding h @@ quoteRelevance r @@ q t
73
quoteArg q (Arg h r t) = arg @@ quoteHiding h @@ quoteRelevance r @@ q t
82
zero = con @@ quoteConName z @@ nil
83
suc n = con @@ quoteConName s @@ list [arg @@ quoteArgInfo defaultArgInfo @@ n]
84
quoteDom q (Dom info t) = arg @@ quoteArgInfo info @@ q t
85
quoteArg q (Arg info t) = arg @@ quoteArgInfo info @@ q t
74
86
quoteArgs ts = list (map (quoteArg quote) ts)
75
quote (Var n ts) = var @@ Lit (LitInt noRange $ fromIntegral n) @@ quoteArgs ts
76
quote (Lam h t) = lam @@ quoteHiding h @@ quote (absBody t)
77
quote (Def x ts) = def @@ quoteName x @@ quoteArgs ts
78
quote (Con x ts) = con @@ quoteName x @@ quoteArgs ts
79
quote (Pi t u) = pi @@ quoteDom quoteType t
80
@@ quoteType (absBody u)
81
quote (Level _) = unsupported
82
quote (Lit lit) = quoteLit lit
83
quote (Sort s) = sort @@ quoteSort s
84
quote (Shared p) = quote $ derefPtr p
85
quote MetaV{} = unsupported
86
quote DontCare{} = unsupported -- could be exposed at some point but we have to take care
90
let ts = fromMaybe __IMPOSSIBLE__ $ allApplyElims es
91
in var @@ Lit (LitInt noRange $ fromIntegral n) @@ quoteArgs ts
92
(Lam info t) -> lam @@ quoteHiding (getHiding info) @@ quote (absBody t)
94
let ts = fromMaybe __IMPOSSIBLE__ $ allApplyElims es
95
in def @@ quoteName x @@ quoteArgs ts
96
(Con x ts) -> con @@ quoteConName x @@ quoteArgs ts
97
(Pi t u) -> pi @@ quoteDom quoteType t
98
@@ quoteType (absBody u)
99
(Level _) -> unsupported
100
(Lit lit) -> quoteLit lit
101
(Sort s) -> sort @@ quoteSort s
102
(Shared p) -> quote $ derefPtr p
103
MetaV{} -> unsupported
104
DontCare{} -> unsupported -- could be exposed at some point but we have to take care
87
105
return (quote, quoteType)
89
107
quoteName :: QName -> Term
90
108
quoteName x = Lit (LitQName noRange x)
110
quoteConName :: ConHead -> Term
111
quoteConName = quoteName . conName
92
113
quoteTerm :: Term -> TCM Term
93
114
quoteTerm v = ($v) . fst <$> quotingKit
117
138
class Unquote a where
118
139
unquote :: Term -> TCM a
120
unquoteH :: Unquote a => Arg Term -> TCM a
121
unquoteH (Arg Hidden Relevant x) = unquote x
122
unquoteH _ = unquoteFailedGeneric "argument. It should be `hidden'."
141
unquoteH :: Unquote a => I.Arg Term -> TCM a
142
unquoteH a | isHidden a && isRelevant a =
144
unquoteH _ = unquoteFailedGeneric "argument. It should be `hidden'."
124
unquoteN :: Unquote a => Arg Term -> TCM a
125
unquoteN (Arg NotHidden Relevant x) = unquote x
126
unquoteN _ = unquoteFailedGeneric "argument. It should be `visible'"
146
unquoteN :: Unquote a => I.Arg Term -> TCM a
147
unquoteN a | notHidden a && isRelevant a =
149
unquoteN _ = unquoteFailedGeneric "argument. It should be `visible'"
128
151
choice :: Monad m => [(m Bool, m a)] -> m a -> m a
129
152
choice [] dflt = dflt
130
choice ((mb, mx) : mxs) dflt = do b <- mb
131
if b then mx else choice mxs dflt
133
instance Unquote a => Unquote (Arg a) where
136
case ignoreSharing t of
137
Con c [hid,rel,x] -> do
139
[(c `isCon` primArgArg, Arg <$> unquoteN hid <*> unquoteN rel <*> unquoteN x)]
140
(unquoteFailed "Arg" "arity 3 and not the `arg' constructor" t)
141
_ -> unquoteFailed "Arg" "not of arity 3" t
153
choice ((mb, mx) : mxs) dflt = ifM mb mx $ choice mxs dflt
155
instance Unquote I.ArgInfo where
158
case ignoreSharing t of
161
[(c `isCon` primArgArgInfo, ArgInfo <$> unquoteN h <*> unquoteN r <*> return [])]
162
(unquoteFailed "ArgInfo" "arity 2 and not the `arginfo' constructor" t)
163
_ -> unquoteFailed "ArgInfo" "not of arity 2" t
165
instance Unquote a => Unquote (I.Arg a) where
168
case ignoreSharing t of
171
[(c `isCon` primArgArg, Arg <$> unquoteN info <*> unquoteN x)]
172
(unquoteFailed "Arg" "arity 2 and not the `arg' constructor" t)
173
_ -> unquoteFailed "Arg" "not of arity 2" t
175
-- Andreas, 2013-10-20: currently, post-fix projections are not part of the
177
instance Unquote a => Unquote (Elim' a) where
178
unquote t = Apply <$> unquote t
143
180
instance Unquote Integer where
195
232
Lit (LitQName _ x) -> return x
196
233
_ -> unquoteFailed "QName" "not a literal qname value" t
235
instance Unquote ConHead where
236
unquote t = getConHead =<< unquote t
198
238
instance Unquote a => Unquote (Abs a) where
199
unquote t = do x <- freshNoName_
200
Abs (show x) <$> unquote t
239
unquote t = do x <- freshNoName_ -- Andreas, 2014-07-11 This is pointless, as it does NOT generate a name suggestion.
240
Abs (nameToArgName x) <$> unquote t
202
242
instance Unquote Sort where
246
286
[(c `isCon` primAgdaTermVar, Var <$> (fromInteger <$> unquoteN x) <*> unquoteN y)
247
287
,(c `isCon` primAgdaTermCon, Con <$> unquoteN x <*> unquoteN y)
248
288
,(c `isCon` primAgdaTermDef, Def <$> unquoteN x <*> unquoteN y)
249
,(c `isCon` primAgdaTermLam, Lam <$> unquoteN x <*> unquoteN y)
289
,(c `isCon` primAgdaTermLam, Lam <$> (flip setHiding defaultArgInfo <$> unquoteN x) <*> unquoteN y)
250
290
,(c `isCon` primAgdaTermPi, Pi <$> (domFromArg <$> unquoteN x) <*> unquoteN y)]
251
291
(unquoteFailed "Term" "arity 2 and none of Var, Con, Def, Lam, Pi" t)