Debian Codegraph @

tests/reflection/assumption/Main.agda

raw
1 {-# OPTIONS --guardedness #-}
2
3 module Main where
4
5 open import Data.String.Base using (String)
6 open import Data.Bool.Base using (if_then_else_)
7 open import Data.List.Base using (List; []; _∷_; concatMap; map)
8 open import Data.Maybe.Base using (Maybe; nothing; just)
9 open import Data.Nat.Base using (ℕ; suc)
10 open import Data.Product.Base using (_×_; _,_)
11 open import Level using (Level)
12 open import Data.Unit.Base using (⊤)
13 open import Function.Base using (case_of_; _$_)
14 open import Reflection.TCM hiding (pure)
15 open import Reflection.AST.Term
16 open import Reflection.AST.Literal hiding (_≟_)
17 open import Reflection.AST.Argument using (Arg; unArg; arg)
18 open import Reflection.AST.DeBruijn
19 open import Reflection.AST.Show
20 open import Relation.Nullary.Decidable using (does)
21
22 open import Effect.Monad
23 open RawMonad {{...}}
24
25 open import Data.Maybe.Instances
26 open import Reflection.TCM.Instances
27
28 private
29 variable
30 a b : Level
31 A : Set a
32 B : Set b
33
34 -- As the doc states (cf. https://agda.readthedocs.io/en/latest/language/reflection.html#type-checking-computations)
35 -- Note that the types in the context are valid in the rest of the context.
36 -- To use in the current context they need to be weakened by 1 + their position
37 -- in the list.
38
39 -- That is to say that the type of the current goal needs to be strengthened
40 -- before being compared to the type of the most local variable. The goal
41 -- indeed lives below that variable's binding site!
42
43 searchEntry : ℕ → Type → List (String × Arg Type) → Maybe ℕ
44 searchEntry n ty [] = nothing
45 searchEntry n ty ((_ , e) ∷ es) = do
46 ty ← strengthen ty
47 if does (ty ≟ unArg e)
48 then just n
49 else searchEntry (suc n) ty es
50
51 macro
52 assumption : Term → TC ⊤
53 assumption hole = do
54 asss ← getContext
55 goal ← inferType hole
56 debugPrint "" 10
57 (strErr "Context : "
58 ∷ concatMap (λ where (_ , arg info ty) → strErr "\n " ∷ termErr ty ∷ []) asss)
59 let res = searchEntry 0 goal asss
60 case res of λ where
61 nothing → typeError (strErr "Couldn't find an assumption of type: " ∷ termErr goal ∷ [])
62 (just idx) → unify hole (var idx [])
63
64 test₀ : A → B → B
65 test₀ x y = assumption
66
67 test₁ : A → B → A
68 test₁ x y = assumption
69
70 test₂ : A → B → B → A
71 test₂ x y z = assumption
72
73 test₃ : List (A → B) → A → B → B → List (A → B)
74 test₃ x y z a = assumption
75
76 test₄ : (A → List B) → A → B → List B → A → List B
77 test₄ x y z a = assumption
78
79 open import IO.Base using (Main; run)
80 open import IO.Finite
81 open import IO.Instances
82
83 macro
84 runTC : TC String → Term → TC ⊤
85 runTC tc t = do
86 u ← tc
87 unify t (lit (string u))
88
89 main : Main
90 main = run $ do
91 putStrLn $ runTC (showDefinition <$> getDefinition (quote test₀))
92 putStrLn $ runTC (showDefinition <$> getDefinition (quote test₁))
93 putStrLn $ runTC (showDefinition <$> getDefinition (quote test₂))
94 putStrLn $ runTC (showDefinition <$> getDefinition (quote test₃))
95 putStrLn $ runTC (showDefinition <$> getDefinition (quote test₄))