types flèches et affichage des types

This commit is contained in:
Marwan 2024-04-15 12:07:49 +02:00
parent 262f0364b7
commit 0b67d9a5eb
8 changed files with 81 additions and 9 deletions

44
: Normal file
View 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 }

View File

@ -1,14 +1,27 @@
open Expr
open Types
(* 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
Fun ((s, t), e) -> "fun ("^s^":"^t^") => "^(string_of_expr e)
| App (e1, e2) -> "("^(string_of_expr e1)^") "^(string_of_expr e2)
Fun ((s, t), e) ->
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
| Exf (e, s) -> "exf("^(string_of_expr e)^":"^s^")"
let print_expr e =
print_string (string_of_expr e)
let affiche_val v = print_string "TODO"

View File

@ -1,6 +1,6 @@
type id = string
type ty_annot = id * string
type ty_annot = id * Types.ty
type expr =
Fun of ty_annot * expr

View File

@ -1,5 +1,6 @@
%{
open Expr
open Types
%}
%token PLUS TIMES
@ -10,7 +11,9 @@ open Expr
%token <string> TYID
%token EOL
%right ARR
%start main
%type <Expr.expr> main
@ -18,13 +21,14 @@ open Expr
main:
e=expression EOL { e }
ty:
| id=TYID { id }
ty_annot:
| 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:
| FUN LPAREN annot=ty_annot RPAREN ARR e=expression
{ Fun (annot, e) }

1
tests/arrow_types.lam Normal file
View File

@ -0,0 +1 @@
fun (f : A => B) => f

1
tests/id_function.lam Normal file
View File

@ -0,0 +1 @@
fun (x : A) => x

View File

@ -0,0 +1 @@
fun (f : A => B) => fun (x : A) => f x

8
types.ml Normal file
View File

@ -0,0 +1,8 @@
type ty_id = string
type ty =
TVar of ty_id
| Arr of ty * ty
| Bot
type gam = ty_id * ty list