basics
This commit is contained in:
commit
1e94ee8299
|
@ -0,0 +1,5 @@
|
|||
MAlonzo/
|
||||
out/
|
||||
*.agdai
|
||||
|
||||
calc
|
|
@ -0,0 +1,6 @@
|
|||
data BinExpr : Set where
|
||||
binE : Token → Token → Token → BinExpr
|
||||
|
||||
evalBin : BinExpr → Maybe Nat
|
||||
evalBin (binE (Oper '+') (Digit a) (Digit b)) = just (a + b)
|
||||
evalBin (binE _ _ _) = nothing
|
|
@ -0,0 +1,14 @@
|
|||
record Map (K V : Set) : Set where
|
||||
constructor mapOf
|
||||
field
|
||||
keys : List K
|
||||
values : List V
|
||||
|
||||
showMap : {V : Set} → (V → String) → Map String V → String
|
||||
showMap f m = "map: " +++ (showList ident (zip (Map.keys m) (map f (Map.values m))))
|
||||
|
||||
findMap : Map Nat Nat → Nat → Nat
|
||||
findMap m k = takeIndex 0 (Map.values m) (findIndex 0 k (Map.keys m))
|
||||
|
||||
showNatMap : Map String Nat → String
|
||||
showNatMap m = showMap show m
|
|
@ -0,0 +1,33 @@
|
|||
data Token : Set where
|
||||
Digit : Nat → Token
|
||||
Delim : Char → Token
|
||||
Oper : Char → Token
|
||||
Term : Token
|
||||
|
||||
parseChar : Char → Token
|
||||
parseChar '0' = Digit 0
|
||||
parseChar '1' = Digit 1
|
||||
parseChar '2' = Digit 2
|
||||
parseChar '3' = Digit 3
|
||||
parseChar '4' = Digit 4
|
||||
parseChar '5' = Digit 5
|
||||
parseChar '6' = Digit 6
|
||||
parseChar '7' = Digit 7
|
||||
parseChar '8' = Digit 8
|
||||
parseChar '9' = Digit 9
|
||||
parseChar ',' = Delim ','
|
||||
parseChar '=' = Oper '='
|
||||
parseChar '+' = Oper '+'
|
||||
parseChar _ = Term
|
||||
|
||||
parseList : Nat → List Char → List Nat
|
||||
parseList a [] = a ∷ []
|
||||
parseList a (x ∷ xs) with parseChar x
|
||||
... | Digit n = parseList ((a * 10) + n) xs
|
||||
... | Delim s = a ∷ parseList 0 xs
|
||||
... | Oper o = [] -- ignore for now
|
||||
... | Term = []
|
||||
|
||||
parseExpr : String → List Nat
|
||||
parseExpr "" = []
|
||||
parseExpr x = parseList 0 (primStringToList x)
|
|
@ -0,0 +1,20 @@
|
|||
record State (V : Set) : Set where
|
||||
constructor state
|
||||
field
|
||||
globals : Map String V
|
||||
scopes : List (Map String V)
|
||||
|
||||
showGlobals : State Nat → String
|
||||
showGlobals s = "globals: " +++ (showMap show (State.globals s))
|
||||
|
||||
showScopes : State Nat → String
|
||||
showScopes s = "scopes: " +++ (showList (showMap show) (State.scopes s))
|
||||
|
||||
showState : State Nat → String
|
||||
showState s = "state: " +++ ((showGlobals s) +++ (", " +++ (showScopes s)))
|
||||
|
||||
smap : Map String Nat
|
||||
smap = mapOf ("foo" ∷ []) (4 ∷ [])
|
||||
|
||||
stest : State Nat
|
||||
stest = state (smap) (smap ∷ [])
|
|
@ -0,0 +1,44 @@
|
|||
module Util where
|
||||
|
||||
open import Agda.Builtin.Bool
|
||||
open import Agda.Builtin.List
|
||||
open import Agda.Builtin.Maybe
|
||||
open import Agda.Builtin.Nat
|
||||
|
||||
ident : {V : Set} → V → V
|
||||
ident x = x
|
||||
|
||||
map : {A B : Set} → (A → B) → List A → List B
|
||||
map f [] = []
|
||||
map f (x ∷ []) = (f x) ∷ []
|
||||
map f (x ∷ xs) = (f x) ∷ (map f xs)
|
||||
|
||||
zip : {V : Set} → List V → List V → List V
|
||||
zip [] [] = []
|
||||
zip [] (x ∷ []) = x ∷ []
|
||||
zip [] (x ∷ xs) = x ∷ (zip [] xs)
|
||||
zip (x ∷ []) [] = x ∷ []
|
||||
zip (x ∷ []) (y ∷ []) = x ∷ y ∷ []
|
||||
zip (x ∷ xs) [] = x ∷ (zip xs [])
|
||||
zip (x ∷ xs) (y ∷ ys) = x ∷ y ∷ (zip xs ys)
|
||||
|
||||
len : {A : Set} → List A → Nat
|
||||
len [] = 0
|
||||
len (x ∷ xs) = suc (len xs)
|
||||
|
||||
filter : {A : Set} → (A → Bool) → List A → List A
|
||||
filter f [] = []
|
||||
filter f (x ∷ xs) with f x
|
||||
... | true = x ∷ filter f xs
|
||||
... | false = filter f xs
|
||||
|
||||
filterNothing : {A : Set} → List (Maybe A) → List A
|
||||
filterNothing [] = []
|
||||
filterNothing (nothing ∷ xs) = filterNothing xs
|
||||
filterNothing (just x ∷ xs) = x ∷ filterNothing xs
|
||||
|
||||
takeIndex : {A : Set} → A → List A → Nat → A
|
||||
takeIndex d [] 0 = d
|
||||
takeIndex d [] (suc x) = d
|
||||
takeIndex d (x ∷ xs) 0 = x
|
||||
takeIndex d (x ∷ xs) (suc n) = takeIndex d xs n
|
Loading…
Reference in New Issue