-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSugared.agda
88 lines (68 loc) · 2.67 KB
/
Sugared.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
-- LIL, but with lots of syntactic sugar added
-- Unlike core LIL, this language is programmer-facing
module LIL.Sugared where
open import Agda.Builtin.String
open import Agda.Builtin.Nat using (Nat)
open import Agda.Builtin.Bool using (Bool)
open import Agda.Builtin.Unit using (⊤)
open import Function.Base using (_∘_)
open import LIL.Core using (Type; ⟦_⟧) public
open Type public
-- Kind of terms
data Kindᵗ : Set where exp pat : Kindᵗ
-- Kind of programs
data Kindᵖ : Set where
stmt : Kindᵖ
term : Kindᵗ → Type → Kindᵖ
module Variables where variable
t t1 t2 t3 : Type
kt kt1 kt2 : Kindᵗ
kp kp1 kp2 : Kindᵖ
open Variables
module PHOAS (Var : Type → Set) where
-- LIL programs
data Prog : Kindᵖ → Set
Stmt = Prog stmt
Exp = λ t → Prog (term exp t)
Pat = λ t → Prog (term pat t)
Exp' = λ kt t → Prog (term kt t) -- Exp or Pat
data Prog where
var : Var t → Pat t
heap[_] : Exp' kt nat → Pat nat
# : ⟦ t ⟧ → Exp t
_+_ _∸_ _*_ _/r_ _%%_ : Exp' kt1 nat → Exp' kt2 nat → Exp nat
_<_ _>_ _≤_ _≥_ _=n=_ : Exp' kt1 nat → Exp' kt2 nat → Exp bool
_=b=_ _∧_ _∨_ : Exp' kt1 bool → Exp' kt2 bool → Exp bool
¬ : Exp' kt bool → Exp bool
_+=_ : Pat nat → Exp' kt nat → Exp nat
_∸=_ : Pat nat → Exp' kt nat → Exp nat
_++ : Pat nat → Exp nat
_∸∸ : Pat nat → Exp nat
_:=_ : Pat t → Exp' kt t → Exp t
skip : Stmt
print : String → Exp' kt t → Stmt
while : Exp' kt bool → Prog kp → Stmt
if_then_else_ : Exp' kt bool → Prog kp1 → Prog kp2 → Stmt
mkvar' : Exp' kt t → (Var t → Prog kp) → Stmt
_>>_ : Prog kp1 → Prog kp2 → Stmt
infixl 33 _+_ _∸_ _*_ _/r_ _%%_
infix 32 _<_ _>_ _≤_ _≥_ _=n=_
infix 31 _=b=_ _∧_ _∨_
infix 6 _:=_ _+=_ _∸=_ _++ _∸∸
infix 5 mkvar_
infixr 4 if_then_else_
infixr 2 _>>_ _>>=_
infixl 2 _=<<_
-- Variant of mkvar' with "var" pre-applied for conciseness
mkvar_ : Exp' kt t → (Pat t → Prog kp) → Stmt
mkvar_ e cont = mkvar' e (cont ∘ var)
_>>=_ : ∀{ℓa ℓb}{A : Set ℓa}{B : A → Set ℓb} → ((a : A) → B a) → (a : A) → B a
f >>= x = f x
_=<<_ : ∀{ℓa ℓb}{A : Set ℓa}{B : A → Set ℓb} → (a : A) → ((a : A) → B a) → B a
x =<< f = f >>= x
-- Natural number literal overloading for Prog
open import Agda.Builtin.FromNat
instance
Num-Prog : Number (Prog (term exp nat))
Num-Prog .Number.Constraint _ = ⊤
Num-Prog .Number.fromNat n = # n