on aa42f71 help sign in
Idris-dev/lib/Language/Reflection.idr
178 lines 5.1 kB history raw
1module Language.Reflection
3import Prelude.Vect
5%access public
7data TTName = UN String
8 -- ^ User-provided name
9 | NS TTName (List String)
10 -- ^ Root, namespaces
11 | MN Int String
12 -- ^ Machine chosen names
13 | NErased
14 -- ^ Name of somethng which is never used in scope
16implicit
17userSuppliedName : String -> TTName
18userSuppliedName = UN
20data TTUExp = UVar Int
21 -- ^ universe variable
22 | UVal Int
23 -- ^ explicit universe variable
25-- | Primitive constants
26data Const = I Int | BI Nat | Fl Float | Ch Char | Str String
27 | IType | BIType | FlType | ChType | StrType
28 | B8 Bits8 | B16 Bits16 | B32 Bits32 | B64 Bits64
29 | B8Type | B16Type | B32Type | B64Type
30 | PtrType | VoidType | Forgot
32abstract class ReflConst (a : Type) where
33 toConst : a -> Const
35instance ReflConst Int where
36 toConst x = I x
38instance ReflConst Nat where
39 toConst = BI
41instance ReflConst Float where
42 toConst = Fl
44instance ReflConst Char where
45 toConst = Ch
47instance ReflConst String where
48 toConst = Str
50instance ReflConst Bits8 where
51 toConst = B8
53instance ReflConst Bits16 where
54 toConst = B16
56instance ReflConst Bits32 where
57 toConst = B32
59instance ReflConst Bits64 where
60 toConst = B64
62implicit
63reflectConstant: (ReflConst a) => a -> Const
64reflectConstant = toConst
67-- | Types of named references
68data NameType = Bound
69 -- ^ reference which is just bound, e.g. by intro
70 | Ref
71 -- ^ reference to a variable
72 | DCon Int Int
73 -- ^ constructor with tag and number
74 | TCon Int Int
75 -- ^ type constructor with tag and number
77-- | Types annotations for bound variables in different
78-- binding contexts
79data Binder a = Lam a
80 | Pi a
81 | Let a a
82 | NLet a a
83 | Hole a
84 | GHole a
85 | Guess a a
86 | PVar a
87 | PVTy a
90instance Functor Binder where
91 fmap f (Lam x) = Lam (f x)
92 fmap f (Pi x) = Pi (f x)
93 fmap f (Let x y) = Let (f x) (f y)
94 fmap f (NLet x y) = NLet (f x) (f y)
95 fmap f (Hole x) = Hole (f x)
96 fmap f (GHole x) = GHole (f x)
97 fmap f (Guess x y) = Guess (f x) (f y)
98 fmap f (PVar x) = PVar (f x)
99 fmap f (PVTy x) = PVTy (f x)
101total
102traverse : (Applicative f) => (a -> f b) -> Binder a -> f (Binder b)
103traverse f (Lam x) = [| Lam (f x) |]
104traverse f (Pi x) = [| Pi (f x) |]
105traverse f (Let x y) = [| Let (f x) (f y) |]
106traverse f (NLet x y) = [| NLet (f x) (f y) |]
107traverse f (Hole x) = [| Hole (f x) |]
108traverse f (GHole x) = [| GHole (f x) |]
109traverse f (Guess x y) = [| Guess (f x) (f y) |]
110traverse f (PVar x) = [| PVar (f x) |]
111traverse f (PVTy x) = [| PVTy (f x) |]
114-- | Reflection of the well typed core language
115data TT = P NameType TTName TT
116 -- ^ named binders
117 | V Int
118 -- ^ variables
119 | Bind TTName (Binder TT) TT
120 -- ^ type annotated named bindings
121 | App TT TT
122 -- ^ (named) application of a function to a value
123 | TConst Const
124 -- ^ constants
125 | Proj TT Int
126 -- ^ argument projection; runtime only
127 | Erased
128 -- ^ erased terms
129 | Impossible
130 -- ^ impossible terms
131 | TType TTUExp
132 -- ^ types
134-- | Raw terms without types
135data Raw = Var TTName
136 | RBind TTName (Binder Raw) Raw
137 | RApp Raw Raw
138 | RType
139 | RForce Raw
140 | RConstant Const
142data Tactic = Try Tactic Tactic
143 -- ^ try the first tactic and resort to the second one on failure
144 | GoalType String Tactic
145 -- ^ only run if the goal has the right type
146 | Refine TTName
147 -- ^ resolve function name, find matching arguments in the
148 -- context and compute the proof target
149 | Seq Tactic Tactic
150 -- ^ apply both tactics in sequence
151 | Trivial
152 -- ^ intelligently construct the proof target from the context
153 | Solve
154 -- ^ infer the proof target from the context
155 | Intros
156 -- ^ introduce all variables into the context
157 | Intro TTName
158 -- ^ introduce a named variable into the context, use the
159 -- first one if the given name is not found
160 | ApplyTactic TT
161 -- ^ invoke the reflected rep. of another tactic
162 | Reflect TT
163 -- ^ turn a value into its reflected representation
164 | Fill Raw
165 -- ^ turn a raw value back into a term
166 | Exact TT
167 -- ^ use the given value to conclude the proof
168 | Focus TTName
169 -- ^ focus a named hole
170 | Rewrite TT
171 -- ^ rewrite using the reflected rep. of a equality proof
172 | LetTac TTName TT
173 -- ^ name a reflected term
174 | LetTacTy TTName TT TT
175 -- ^ name a reflected term and type it
176 | Compute
177 -- ^ normalise the context