Skip to content

Commit

Permalink
Add modulo operator
Browse files Browse the repository at this point in the history
  • Loading branch information
rossberg committed May 30, 2024
1 parent d98963d commit a835a0e
Show file tree
Hide file tree
Showing 16 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion spectec/doc/Language.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ exp ::=
"##" exp remove possible parentheses (for syntax rewrites in hints)
unop ::= notop | "+" | "-"
binop ::= logop | "+" | "-" | "*" | "/" | "^"
binop ::= logop | "+" | "-" | "*" | "/" | "\" | "^"
arith ::=
varid meta variable
atom token
Expand Down
1 change: 1 addition & 0 deletions spectec/src/al/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type binop =
| SubOp (* `-` *)
| MulOp (* `*` *)
| DivOp (* `/` *)
| ModOp (* `\` *)
| ExpOp (* `^` *)
(* logical operation *)
| ImplOp (* `=>` *)
Expand Down
1 change: 1 addition & 0 deletions spectec/src/al/print.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ let string_of_binop = function
| SubOp -> "-"
| MulOp -> "·"
| DivOp -> "/"
| ModOp -> "\\"
| ExpOp -> "^"
| EqOp -> "is"
| NeOp -> "is not"
Expand Down
1 change: 1 addition & 0 deletions spectec/src/backend-interpreter/interpreter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ and eval_expr env expr =
| SubOp, NumV i1, NumV i2 -> Z.sub i1 i2 |> numV
| MulOp, NumV i1, NumV i2 -> Z.mul i1 i2 |> numV
| DivOp, NumV i1, NumV i2 -> Z.div i1 i2 |> numV
| ModOp, NumV i1, NumV i2 -> Z.rem i1 i2 |> numV
| ExpOp, NumV i1, NumV i2 -> Z.pow i1 (Z.to_int i2) |> numV
| AndOp, BoolV b1, BoolV b2 -> boolV (b1 && b2)
| OrOp, BoolV b1, BoolV b2 -> boolV (b1 || b2)
Expand Down
1 change: 1 addition & 0 deletions spectec/src/backend-latex/render.ml
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ let render_binop = function
| SubOp -> "-"
| MulOp -> "\\cdot"
| DivOp -> "/"
| ModOp -> "\\mathbin{mod}"
| ExpOp -> assert false

let render_cmpop = function
Expand Down
2 changes: 2 additions & 0 deletions spectec/src/backend-prose/render.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ let al_to_el_binop = function
| Al.Ast.SubOp -> Some El.Ast.SubOp
| Al.Ast.MulOp -> Some El.Ast.MulOp
| Al.Ast.DivOp -> Some El.Ast.DivOp
| Al.Ast.ModOp -> Some El.Ast.ModOp
| Al.Ast.ExpOp -> Some El.Ast.ExpOp
| _ -> None

Expand Down Expand Up @@ -247,6 +248,7 @@ let render_al_binop = function
| Al.Ast.SubOp -> "minus"
| Al.Ast.MulOp -> "multiplied by"
| Al.Ast.DivOp -> "divided by"
| Al.Ast.ModOp -> "modulo"
| Al.Ast.ExpOp -> "to the power of"
| Al.Ast.EqOp -> "is"
| Al.Ast.NeOp -> "is not"
Expand Down
1 change: 1 addition & 0 deletions spectec/src/el/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ and binop =
| SubOp (* `-` *)
| MulOp (* `*` *)
| DivOp (* `/` *)
| ModOp (* `\` *)
| ExpOp (* `^` *)

and cmpop =
Expand Down
1 change: 1 addition & 0 deletions spectec/src/el/print.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ let string_of_binop = function
| SubOp -> "-"
| MulOp -> "*"
| DivOp -> "/"
| ModOp -> "\\"
| ExpOp -> "^"

let string_of_cmpop = function
Expand Down
1 change: 1 addition & 0 deletions spectec/src/frontend/elab.ml
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ let infer_binop' = function
| SubOp -> infer_numop (fun t -> Il.SubOp t) numtyps
| MulOp -> infer_numop (fun t -> Il.MulOp t) numtyps
| DivOp -> infer_numop (fun t -> Il.DivOp t) numtyps
| ModOp -> infer_numop (fun t -> Il.ModOp t) numtyps
| ExpOp -> infer_numop (fun t -> Il.ExpOp t) numtyps

let infer_cmpop' = function
Expand Down
3 changes: 3 additions & 0 deletions spectec/src/frontend/eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ and reduce_exp env e : exp =
| DivOp, NatE (op, n1), NatE (_, n2) -> NatE (op, Z.(n1 / n2)) $ e.at
| DivOp, NatE (_, z0), _ when z0 = Z.zero -> e1'
| DivOp, _, NatE (_, z1) when z1 = Z.one -> e1'
| ModOp, NatE (op, n1), NatE (_, n2) -> NatE (op, Z.rem n1 n2) $ e.at
| ModOp, NatE (_, z0), _ when z0 = Z.zero -> e1'
| ModOp, _, NatE (op, z1) when z1 = Z.one -> NatE (op, Z.zero) $ e.at
| ExpOp, NatE (op, n1), NatE (_, n2) -> NatE (op, Z.(n1 ** to_int n2)) $ e.at
| ExpOp, NatE (_, z01), _ when z01 = Z.zero || z01 = Z.one -> e1'
| ExpOp, _, NatE (op, z0) when z0 = Z.zero -> NatE (op, Z.one) $ e.at
Expand Down
1 change: 1 addition & 0 deletions spectec/src/frontend/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ check_atom :
| MINUS { SubOp }
| STAR { MulOp }
| SLASH { DivOp }
| BACKSLASH { ModOp }

%inline cmpop :
| EQ { EqOp }
Expand Down
1 change: 1 addition & 0 deletions spectec/src/il/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ and binop =
| SubOp of numtyp (* `-` *)
| MulOp of numtyp (* `*` *)
| DivOp of numtyp (* `/` *)
| ModOp of numtyp (* `\` *)
| ExpOp of numtyp (* `^` *)

and cmpop =
Expand Down
3 changes: 3 additions & 0 deletions spectec/src/il/eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ and reduce_exp env e : exp =
| DivOp _, NatE n1, NatE n2 -> NatE Z.(n1 / n2) $> e
| DivOp _, NatE z0, _ when z0 = Z.zero -> e1'
| DivOp _, _, NatE z1 when z1 = Z.one -> e1'
| ModOp _, NatE n1, NatE n2 -> NatE Z.(rem n1 n2) $> e
| ModOp _, NatE z0, _ when z0 = Z.zero -> e1'
| ModOp _, _, NatE z1 when z1 = Z.one -> NatE Z.zero $> e
| ExpOp _, NatE n1, NatE n2 -> NatE Z.(n1 ** to_int n2) $> e
| ExpOp _, NatE z01, _ when z01 = Z.zero || z01 = Z.one -> e1'
| ExpOp _, _, NatE z0 when z0 = Z.zero -> NatE Z.one $> e
Expand Down
1 change: 1 addition & 0 deletions spectec/src/il/print.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ let string_of_binop = function
| SubOp _ -> "-"
| MulOp _ -> "*"
| DivOp _ -> "/"
| ModOp _ -> "\\"
| ExpOp _ -> "^"

let string_of_cmpop = function
Expand Down
2 changes: 1 addition & 1 deletion spectec/src/il/valid.ml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ let infer_unop = function

let infer_binop = function
| AndOp | OrOp | ImplOp | EquivOp -> BoolT, BoolT, BoolT
| AddOp t | SubOp t | MulOp t | DivOp t -> NumT t, NumT t, NumT t
| AddOp t | SubOp t | MulOp t | DivOp t | ModOp t -> NumT t, NumT t, NumT t
| ExpOp t -> NumT t, NumT NatT, NumT t

let infer_cmpop = function
Expand Down
1 change: 1 addition & 0 deletions spectec/src/il2al/translate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ and translate_exp exp =
| Il.SubOp _ -> SubOp
| Il.MulOp _ -> MulOp
| Il.DivOp _ -> DivOp
| Il.ModOp _ -> ModOp
| Il.ExpOp _ -> ExpOp
| Il.AndOp -> AndOp
| Il.OrOp -> OrOp
Expand Down

0 comments on commit a835a0e

Please sign in to comment.