types flèches et affichage des types
This commit is contained in:
parent
262f0364b7
commit
0b67d9a5eb
44
:
Normal file
44
:
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
%{
|
||||||
|
open Expr
|
||||||
|
%}
|
||||||
|
|
||||||
|
%token PLUS TIMES
|
||||||
|
%token TOP BOTTOM EXFALSO
|
||||||
|
%token LPAREN RPAREN
|
||||||
|
%token FUN ARR COLON
|
||||||
|
%token <string> VARID
|
||||||
|
%token <string> TYID
|
||||||
|
|
||||||
|
%token EOL
|
||||||
|
|
||||||
|
|
||||||
|
%left ARR
|
||||||
|
|
||||||
|
%start main
|
||||||
|
%type <Expr.expr> main
|
||||||
|
|
||||||
|
%%
|
||||||
|
main:
|
||||||
|
e=expression EOL { e }
|
||||||
|
|
||||||
|
ty_annot:
|
||||||
|
| id=VARID COLON t=ty { (id, t) }
|
||||||
|
|
||||||
|
ty:
|
||||||
|
| id=TYID { id }
|
||||||
|
| LPAREN t=ty RPAREN { ty }
|
||||||
|
| t1=ty ARR t2=ty {
|
||||||
|
|
||||||
|
expression:
|
||||||
|
| FUN LPAREN annot=ty_annot RPAREN ARR e=expression
|
||||||
|
{ Fun (annot, e) }
|
||||||
|
| e=app_expr { e }
|
||||||
|
|
||||||
|
app_expr:
|
||||||
|
| e=app_expr a=atomic { App (e, a) }
|
||||||
|
| a=atomic { a }
|
||||||
|
|
||||||
|
atomic:
|
||||||
|
| id=VARID { Var id }
|
||||||
|
| LPAREN e=expression RPAREN
|
||||||
|
{ e }
|
19
affichage.ml
19
affichage.ml
@ -1,14 +1,27 @@
|
|||||||
open Expr
|
open Expr
|
||||||
|
open Types
|
||||||
|
|
||||||
(* fonction d'affichage *)
|
(* fonction d'affichage *)
|
||||||
|
let rec string_of_ty = function
|
||||||
|
TVar s -> s
|
||||||
|
| Arr(t1, t2) ->
|
||||||
|
let s1 = string_of_ty t1 in
|
||||||
|
let s2 = string_of_ty t2 in
|
||||||
|
s1 ^ " => (" ^ s2 ^ ")"
|
||||||
|
| Bot -> "#"
|
||||||
|
|
||||||
let rec string_of_expr = function
|
let rec string_of_expr = function
|
||||||
Fun ((s, t), e) -> "fun ("^s^":"^t^") => "^(string_of_expr e)
|
Fun ((s, t), e) ->
|
||||||
| App (e1, e2) -> "("^(string_of_expr e1)^") "^(string_of_expr e2)
|
let s_ty = string_of_ty t in
|
||||||
|
let s_e = string_of_expr e in
|
||||||
|
"fun (" ^ s ^ " : " ^ s_ty ^ ") => (" ^ s_e ^ ")"
|
||||||
|
| App (e1, e2) ->
|
||||||
|
"("^(string_of_expr e1)^") "^(string_of_expr e2)
|
||||||
| Var (s) -> s
|
| Var (s) -> s
|
||||||
| Exf (e, s) -> "exf("^(string_of_expr e)^":"^s^")"
|
| Exf (e, s) -> "exf("^(string_of_expr e)^":"^s^")"
|
||||||
|
|
||||||
|
|
||||||
let print_expr e =
|
let print_expr e =
|
||||||
print_string (string_of_expr e)
|
print_string (string_of_expr e)
|
||||||
|
|
||||||
let affiche_val v = print_string "TODO"
|
let affiche_val v = print_string "TODO"
|
||||||
|
|
||||||
|
2
expr.ml
2
expr.ml
@ -1,6 +1,6 @@
|
|||||||
type id = string
|
type id = string
|
||||||
|
|
||||||
type ty_annot = id * string
|
type ty_annot = id * Types.ty
|
||||||
|
|
||||||
type expr =
|
type expr =
|
||||||
Fun of ty_annot * expr
|
Fun of ty_annot * expr
|
||||||
|
12
parser.mly
12
parser.mly
@ -1,5 +1,6 @@
|
|||||||
%{
|
%{
|
||||||
open Expr
|
open Expr
|
||||||
|
open Types
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%token PLUS TIMES
|
%token PLUS TIMES
|
||||||
@ -11,6 +12,8 @@ open Expr
|
|||||||
|
|
||||||
%token EOL
|
%token EOL
|
||||||
|
|
||||||
|
%right ARR
|
||||||
|
|
||||||
%start main
|
%start main
|
||||||
%type <Expr.expr> main
|
%type <Expr.expr> main
|
||||||
|
|
||||||
@ -18,13 +21,14 @@ open Expr
|
|||||||
main:
|
main:
|
||||||
e=expression EOL { e }
|
e=expression EOL { e }
|
||||||
|
|
||||||
|
|
||||||
ty:
|
|
||||||
| id=TYID { id }
|
|
||||||
|
|
||||||
ty_annot:
|
ty_annot:
|
||||||
| id=VARID COLON t=ty { (id, t) }
|
| id=VARID COLON t=ty { (id, t) }
|
||||||
|
|
||||||
|
ty:
|
||||||
|
| id=TYID { TVar(id) }
|
||||||
|
| LPAREN t=ty RPAREN { t }
|
||||||
|
| t1=ty ARR t2=ty { Arr(t1, t2) }
|
||||||
|
|
||||||
expression:
|
expression:
|
||||||
| FUN LPAREN annot=ty_annot RPAREN ARR e=expression
|
| FUN LPAREN annot=ty_annot RPAREN ARR e=expression
|
||||||
{ Fun (annot, e) }
|
{ Fun (annot, e) }
|
||||||
|
1
tests/arrow_types.lam
Normal file
1
tests/arrow_types.lam
Normal file
@ -0,0 +1 @@
|
|||||||
|
fun (f : A => B) => f
|
1
tests/id_function.lam
Normal file
1
tests/id_function.lam
Normal file
@ -0,0 +1 @@
|
|||||||
|
fun (x : A) => x
|
1
tests/nested_function.lam
Normal file
1
tests/nested_function.lam
Normal file
@ -0,0 +1 @@
|
|||||||
|
fun (f : A => B) => fun (x : A) => f x
|
Loading…
Reference in New Issue
Block a user