0 | module Language.Reflection.TT
2 | import public Data.List
3 | import public Data.String
5 | import Decidable.Equality
10 | data Namespace = MkNS (List String)
13 | isParentOf : (given, candidate : Namespace) -> Bool
14 | isParentOf (MkNS ms) (MkNS ns) = List.isSuffixOf ms ns
19 | data ModuleIdent = MkMI (List String)
21 | %name ModuleIdent
mi
24 | showSep : String -> List String -> String
27 | showSep sep (x :: xs) = x ++ sep ++ showSep sep xs
30 | Show Namespace where
31 | show (MkNS ns) = showSep "." (reverse ns)
38 | FilePos = (Int, Int)
41 | data VirtualIdent : Type where
42 | Interactive : VirtualIdent
45 | data OriginDesc : Type where
49 | PhysicalIdrSrc : (ident : ModuleIdent) -> OriginDesc
52 | PhysicalPkgSrc : (fname : String) -> OriginDesc
53 | Virtual : (ident : VirtualIdent) -> OriginDesc
55 | %name OriginDesc
origin
62 | data FC = MkFC OriginDesc FilePos FilePos
65 | MkVirtualFC OriginDesc FilePos FilePos
77 | record WithFC (ty : Type) where
84 | NoFC : a -> WithFC a
85 | NoFC = MkFCVal EmptyFC
88 | Functor WithFC where
89 | map f = { value $= f}
92 | Foldable WithFC where
93 | foldr f i v = f v.value i
96 | Traversable WithFC where
97 | traverse f (MkFCVal fc val) = map (MkFCVal fc) (f val)
101 | Eq a => Eq (WithFC a) where
102 | x == y = x.value == y.value
106 | Ord a => Ord (WithFC a) where
107 | compare x y = compare x.value y.value
110 | data NameType : Type where
113 | DataCon : (tag : Int) -> (arity : Nat) -> NameType
114 | TyCon : (tag : Int) -> (arity : Nat) -> NameType
159 | Show PrimType where
160 | show IntType = "Int"
161 | show IntegerType = "Integer"
162 | show Int8Type = "Int8"
163 | show Int16Type = "Int16"
164 | show Int32Type = "Int32"
165 | show Int64Type = "Int64"
166 | show Bits8Type = "Bits8"
167 | show Bits16Type = "Bits16"
168 | show Bits32Type = "Bits32"
169 | show Bits64Type = "Bits64"
170 | show StringType = "String"
171 | show CharType = "Char"
172 | show DoubleType = "Double"
173 | show WorldType = "%World"
176 | Show Constant where
177 | show (I x) = show x
178 | show (BI x) = show x
179 | show (I8 x) = show x
180 | show (I16 x) = show x
181 | show (I32 x) = show x
182 | show (I64 x) = show x
183 | show (B8 x) = show x
184 | show (B16 x) = show x
185 | show (B32 x) = show x
186 | show (B64 x) = show x
187 | show (Str x) = show x
188 | show (Ch x) = show x
189 | show (Db x) = show x
190 | show (PrT x) = show x
191 | show WorldVal = "%World"
204 | data Name : Type where
206 | NS : Namespace -> Name -> Name
208 | UN : UserName -> Name
210 | MN : String -> Int -> Name
212 | DN : String -> Name -> Name
214 | Nested : (Int, Int) -> Name -> Name
216 | CaseBlock : String -> Int -> Name
218 | WithBlock : String -> Int -> Name
225 | fromName : Name -> Name
229 | dropNS : Name -> Name
230 | dropNS (NS _ n) = dropNS n
234 | getNS : Name -> Namespace
235 | getNS (NS ns nm) = ns
236 | getNS nm = TT.MkNS []
239 | isOp : Name -> Bool
240 | isOp nm = case dropNS nm of
241 | UN (Basic n) => case strM n of
242 | StrCons c _ => not (isAlpha c)
247 | Show UserName where
249 | show (Field n) = "." ++ n
250 | show Underscore = "_"
253 | showPrefix : Bool -> Name -> String
254 | showPrefix b nm@(UN un) = showParens (b && isOp nm) (show un)
255 | showPrefix b (NS ns n) = show ns ++ "." ++ showPrefix True n
256 | showPrefix b (MN x y) = "{" ++ x ++ ":" ++ show y ++ "}"
257 | showPrefix b (DN str y) = str
258 | showPrefix b (Nested (outer, idx) inner)
259 | = show outer ++ ":" ++ show idx ++ ":" ++ showPrefix False inner
260 | showPrefix b (CaseBlock outer i) = "case block in " ++ show outer
261 | showPrefix b (WithBlock outer i) = "with block in " ++ show outer
265 | show = showPrefix False
268 | record NameInfo where
269 | constructor MkNameInfo
270 | nametype : NameType
274 | data Count : Type where
284 | enunciate : Count -> String
285 | enunciate M0 = "runtime irrelevant"
286 | enunciate M1 = "linear"
287 | enunciate MW = "unconstrained"
290 | showCount : Count -> String -> String
291 | showCount M0 s = "0 \{s}"
292 | showCount M1 s = "1 \{s}"
296 | data PiInfo t = ImplicitArg | ExplicitArg | AutoImplicit | DefImplicit t
300 | Functor PiInfo where
301 | map f ImplicitArg = ImplicitArg
302 | map f ExplicitArg = ExplicitArg
303 | map f AutoImplicit = AutoImplicit
304 | map f $
DefImplicit x = DefImplicit $
f x
307 | showPiInfo : Show a => {default True wrapExplicit : Bool} -> PiInfo a -> String -> String
308 | showPiInfo ImplicitArg s = "{\{s}}"
309 | showPiInfo ExplicitArg s = if wrapExplicit then "(\{s})" else s
310 | showPiInfo AutoImplicit s = "{auto \{s}}"
311 | showPiInfo (DefImplicit t) s = "{default \{assert_total $ showPrec App t} \{s}}"
314 | data IsVar : Name -> Nat -> List Name -> Type where
315 | First : IsVar n Z (n :: ns)
316 | Later : IsVar n i ns -> IsVar n (S i) (m :: ns)
320 | data LazyReason = LInf | LLazy | LUnknown
321 | %name LazyReason
lr
324 | Show LazyReason where
326 | show LLazy = "Lazy"
327 | show LUnknown = "Unknown"
330 | data TotalReq = Total | CoveringOnly | PartialOK
331 | %name TotalReq
treq
334 | Show TotalReq where
335 | show Total = "total"
336 | show CoveringOnly = "covering"
337 | show PartialOK = "partial"
340 | showTotalReq : Maybe TotalReq -> String -> String
341 | showTotalReq Nothing s = s
342 | showTotalReq (Just treq) s = unwords [show treq, s]
345 | data Visibility = Private | Export | Public
346 | %name Visibility
vis
349 | Show Visibility where
350 | show Private = "private"
351 | show Export = "export"
352 | show Public = "public export"
355 | data BuiltinType = BuiltinNatural | NaturalToInteger | IntegerToNatural
356 | %name BuiltinType
bty
359 | Show BuiltinType where
360 | show BuiltinNatural = "Natural"
361 | show NaturalToInteger = "NaturalToInteger"
362 | show IntegerToNatural = "IntegerToNatural"
366 | Total == Total = True
367 | CoveringOnly == CoveringOnly = True
368 | PartialOK == PartialOK = True
372 | Eq Visibility where
373 | Private == Private = True
374 | Export == Export = True
375 | Public == Public = True
379 | Eq BuiltinType where
380 | BuiltinNatural == BuiltinNatural = True
381 | NaturalToInteger == NaturalToInteger = True
382 | IntegerToNatural == IntegerToNatural = True
387 | Eq LazyReason where
388 | LInf == LInf = True
389 | LLazy == LLazy = True
390 | LUnknown == LUnknown = True
395 | MkNS ns == MkNS ns' = ns == ns'
406 | Basic n == Basic n' = n == n'
407 | Field n == Field n' = n == n'
408 | Underscore == Underscore = True
413 | NS ns n == NS ns' n' = ns == ns' && n == n'
414 | UN n == UN n' = n == n'
415 | MN n i == MN n' i' = n == n' && i == i'
416 | DN _ n == DN _ n' = n == n'
417 | Nested i n == Nested i' n' = i == i' && n == n'
418 | CaseBlock n i == CaseBlock n' i' = n == n' && i == i'
419 | WithBlock n i == WithBlock n' i' = n == n' && i == i'
424 | IntType == IntType = True
425 | IntegerType == IntegerType = True
426 | Int8Type == Int8Type = True
427 | Int16Type == Int16Type = True
428 | Int32Type == Int32Type = True
429 | Int64Type == Int64Type = True
430 | Bits8Type == Bits8Type = True
431 | Bits16Type == Bits16Type = True
432 | Bits32Type == Bits32Type = True
433 | Bits64Type == Bits64Type = True
434 | StringType == StringType = True
435 | CharType == CharType = True
436 | DoubleType == DoubleType = True
437 | WorldType == WorldType = True
442 | I c == I c' = c == c'
443 | BI c == BI c' = c == c'
444 | I8 c == I8 c' = c == c'
445 | I16 c == I16 c' = c == c'
446 | I32 c == I32 c' = c == c'
447 | I64 c == I64 c' = c == c'
448 | B8 c == B8 c' = c == c'
449 | B16 c == B16 c' = c == c'
450 | B32 c == B32 c' = c == c'
451 | B64 c == B64 c' = c == c'
452 | Str c == Str c' = c == c'
453 | Ch c == Ch c' = c == c'
454 | Db c == Db c' = c == c'
455 | PrT t == PrT t' = t == t'
456 | WorldVal == WorldVal = True
460 | Ord Namespace where
461 | compare (MkNS ms) (MkNS ns) = compare ms ns
473 | usernameTag : UserName -> Int
474 | usernameTag (Basic _) = 0
475 | usernameTag (Field _) = 1
476 | usernameTag Underscore = 2
480 | compare (Basic x) (Basic y) = compare x y
481 | compare (Field x) (Field y) = compare x y
482 | compare Underscore Underscore = EQ
483 | compare x y = compare (usernameTag x) (usernameTag y)
485 | nameTag : Name -> Int
486 | nameTag (NS _ _) = 0
488 | nameTag (MN _ _) = 2
489 | nameTag (DN _ _) = 3
490 | nameTag (Nested _ _) = 4
491 | nameTag (CaseBlock _ _) = 5
492 | nameTag (WithBlock _ _) = 6
496 | compare (NS x y) (NS x' y')
497 | = case compare y y' of
503 | compare (UN x) (UN y) = compare x y
504 | compare (MN x y) (MN x' y')
505 | = case compare y y' of
509 | compare (DN _ n) (DN _ n') = compare n n'
510 | compare (Nested x y) (Nested x' y')
511 | = case compare y y' of
515 | compare (CaseBlock x y) (CaseBlock x' y')
516 | = case compare y y' of
520 | compare (WithBlock x y) (WithBlock x' y')
521 | = case compare y y' of
526 | compare x y = compare (nameTag x) (nameTag y)
528 | export Injective MkNS where injective Refl = Refl
531 | DecEq Namespace where
532 | decEq (MkNS ns) (MkNS ns') = decEqCong (decEq ns ns')
534 | export Injective Basic where injective Refl = Refl
535 | export Injective Field where injective Refl = Refl
538 | DecEq UserName where
539 | decEq (Basic str) (Basic str1) = decEqCong (decEq str str1)
540 | decEq (Basic str) (Field str1) = No (\case Refl
impossible)
541 | decEq (Basic str) Underscore = No (\case Refl
impossible)
542 | decEq (Field str) (Basic str1) = No (\case Refl
impossible)
543 | decEq (Field str) (Field str1) = decEqCong (decEq str str1)
544 | decEq (Field str) Underscore = No (\case Refl
impossible)
545 | decEq Underscore (Basic str) = No (\case Refl
impossible)
546 | decEq Underscore (Field str) = No (\case Refl
impossible)
547 | decEq Underscore Underscore = Yes Refl
549 | export Biinjective NS where biinjective Refl = (Refl, Refl)
550 | export Injective UN where injective Refl = Refl
551 | export Biinjective MN where biinjective Refl = (Refl, Refl)
552 | export Biinjective DN where biinjective Refl = (Refl, Refl)
553 | export Biinjective Nested where biinjective Refl = (Refl, Refl)
554 | export Biinjective CaseBlock where biinjective Refl = (Refl, Refl)
555 | export Biinjective WithBlock where biinjective Refl = (Refl, Refl)
559 | decEq (NS ns nm) (NS ns1 nm1) = decEqCong2 (decEq ns ns1) (decEq nm nm1)
560 | decEq (NS ns nm) (UN un) = No (\case Refl
impossible)
561 | decEq (NS ns nm) (MN str i) = No (\case Refl
impossible)
562 | decEq (NS ns nm) (DN str nm1) = No (\case Refl
impossible)
563 | decEq (NS ns nm) (Nested x nm1) = No (\case Refl
impossible)
564 | decEq (NS ns nm) (CaseBlock str i) = No (\case Refl
impossible)
565 | decEq (NS ns nm) (WithBlock str i) = No (\case Refl
impossible)
566 | decEq (UN un) (NS ns nm) = No (\case Refl
impossible)
567 | decEq (UN un) (UN un1) = decEqCong (decEq un un1)
568 | decEq (UN un) (MN str i) = No (\case Refl
impossible)
569 | decEq (UN un) (DN str nm) = No (\case Refl
impossible)
570 | decEq (UN un) (Nested x nm) = No (\case Refl
impossible)
571 | decEq (UN un) (CaseBlock str i) = No (\case Refl
impossible)
572 | decEq (UN un) (WithBlock str i) = No (\case Refl
impossible)
573 | decEq (MN str i) (NS ns nm) = No (\case Refl
impossible)
574 | decEq (MN str i) (UN un) = No (\case Refl
impossible)
575 | decEq (MN str i) (MN str1 j) = decEqCong2 (decEq str str1) (decEq i j)
576 | decEq (MN str i) (DN str1 nm) = No (\case Refl
impossible)
577 | decEq (MN str i) (Nested x nm) = No (\case Refl
impossible)
578 | decEq (MN str i) (CaseBlock str1 j) = No (\case Refl
impossible)
579 | decEq (MN str i) (WithBlock str1 j) = No (\case Refl
impossible)
580 | decEq (DN str nm) (NS ns nm1) = No (\case Refl
impossible)
581 | decEq (DN str nm) (UN un) = No (\case Refl
impossible)
582 | decEq (DN str nm) (MN str1 i) = No (\case Refl
impossible)
583 | decEq (DN str nm) (DN str1 nm1) = decEqCong2 (decEq str str1) (decEq nm nm1)
584 | decEq (DN str nm) (Nested x nm1) = No (\case Refl
impossible)
585 | decEq (DN str nm) (CaseBlock str1 i) = No (\case Refl
impossible)
586 | decEq (DN str nm) (WithBlock str1 i) = No (\case Refl
impossible)
587 | decEq (Nested x nm) (NS ns nm1) = No (\case Refl
impossible)
588 | decEq (Nested x nm) (UN un) = No (\case Refl
impossible)
589 | decEq (Nested x nm) (MN str i) = No (\case Refl
impossible)
590 | decEq (Nested x nm) (DN str nm1) = No (\case Refl
impossible)
591 | decEq (Nested x nm) (Nested y nm1) = decEqCong2 (decEq x y) (decEq nm nm1)
592 | decEq (Nested x nm) (CaseBlock str i) = No (\case Refl
impossible)
593 | decEq (Nested x nm) (WithBlock str i) = No (\case Refl
impossible)
594 | decEq (CaseBlock str i) (NS ns nm) = No (\case Refl
impossible)
595 | decEq (CaseBlock str i) (UN un) = No (\case Refl
impossible)
596 | decEq (CaseBlock str i) (MN str1 j) = No (\case Refl
impossible)
597 | decEq (CaseBlock str i) (DN str1 nm) = No (\case Refl
impossible)
598 | decEq (CaseBlock str i) (Nested x nm) = No (\case Refl
impossible)
599 | decEq (CaseBlock str i) (CaseBlock str1 j) = decEqCong2 (decEq str str1) (decEq i j)
600 | decEq (CaseBlock str i) (WithBlock str1 j) = No (\case Refl
impossible)
601 | decEq (WithBlock str i) (NS ns nm) = No (\case Refl
impossible)
602 | decEq (WithBlock str i) (UN un) = No (\case Refl
impossible)
603 | decEq (WithBlock str i) (MN str1 j) = No (\case Refl
impossible)
604 | decEq (WithBlock str i) (DN str1 nm) = No (\case Refl
impossible)
605 | decEq (WithBlock str i) (Nested x nm) = No (\case Refl
impossible)
606 | decEq (WithBlock str i) (CaseBlock str1 j) = No (\case Refl
impossible)
607 | decEq (WithBlock str i) (WithBlock str1 j) = decEqCong2 (decEq str str1) (decEq i j)