Lecture de lambda-termes et reduce

This commit is contained in:
Adrien Vannson 2022-05-11 21:29:20 +02:00
parent 359d7333f5
commit f60d98ea78
No known key found for this signature in database
GPG Key ID: FE2E66FD978C1A55
3 changed files with 53 additions and 4 deletions

View File

@ -18,6 +18,11 @@ rule token = parse
| "cut" { CUT } | "cut" { CUT }
| "False" { FALSE } | "False" { FALSE }
| "fun" { FUN }
| "=>" { MAPS_TO }
| ':' { VDOTS }
| "exf" { EXF }
| ['A'-'Z']+['0'-'9']* as s { TYPE_NAME s } | ['A'-'Z']+['0'-'9']* as s { TYPE_NAME s }
| ['a'-'z']+['0'-'9']* as s { VAR_NAME s } | ['a'-'z']+['0'-'9']* as s { VAR_NAME s }
| '.' { DOT } | '.' { DOT }

18
main.ml
View File

@ -37,6 +37,24 @@ let readline () = match file with
| Some f -> input_line f | Some f -> input_line f
in in
if !reduce_option then (
let lexbuf = Lexing.from_channel (match file with
| None -> stdin
| Some file -> file
)
in
let lambda_term =
try
Parser.main_lambda Lexer.token lexbuf
with e -> (
Printf.printf "Can't read lambda term\n";
raise e
)
in
reduce lambda_term;
exit 0
);
(* Show a message only if the input is read from stdin *) (* Show a message only if the input is read from stdin *)
let show s = match file with let show s = match file with
| None -> Printf.printf "%s" s | None -> Printf.printf "%s" s

View File

@ -11,6 +11,7 @@
%token <string> TYPE_NAME %token <string> TYPE_NAME
%token DOT INTRO ASSUMPTION APPLY ELIM CUT %token DOT INTRO ASSUMPTION APPLY ELIM CUT
%token FUN MAPS_TO VDOTS EXF
/* L'ordre de définition définit la priorité */ /* L'ordre de définition définit la priorité */
%right RARROW %right RARROW
@ -22,10 +23,20 @@
%start main_tactic %start main_tactic
%type <Tactic.tactic> main_tactic %type <Tactic.tactic> main_tactic
%start main_lambda
%type <Structs.lam> main_lambda
%% %%
main_type: main_type:
| ty EOF { $1 } | ty EOF { $1 }
main_tactic:
| tactic EOF { $1 }
main_lambda:
| lambda EOF { $1 }
/* Types */
ty: ty:
| LPAREN ty RPAREN { $2 } | LPAREN ty RPAREN { $2 }
| ty RARROW ty { TImpl ($1, $3) } | ty RARROW ty { TImpl ($1, $3) }
@ -33,12 +44,27 @@ ty:
| TILDE ty { TImpl ($2, TFalse) } | TILDE ty { TImpl ($2, TFalse) }
| FALSE { TFalse } | FALSE { TFalse }
main_tactic: /* Tactiques */
| tactic EOF { $1 }
tactic: tactic:
| INTRO VAR_NAME DOT { Intro $2 } | INTRO VAR_NAME DOT { Intro $2 }
| ASSUMPTION DOT { Assumption } | ASSUMPTION DOT { Assumption }
| APPLY VAR_NAME DOT { Apply $2 } | APPLY VAR_NAME DOT { Apply $2 }
| ELIM VAR_NAME DOT { Elim $2 } | ELIM VAR_NAME DOT { Elim $2 }
| CUT ty DOT { Cut $2 } | CUT ty DOT { Cut $2 }
/* Lambda-termes */
lambda_arg: /* Expression pouvant être en argument d'une fonction */
| VAR_NAME { LVar $1 }
| LPAREN lambda RPAREN { $2 }
/* Application d'une fonction */
lambda_app:
| lambda_app lambda_arg { LApp ($1, $2) }
| lambda_arg { $1 }
lambda:
| lambda_app { $1 }
| FUN VAR_NAME VDOTS ty MAPS_TO lambda { LFun ($2, $4, $6) }
| FUN LPAREN VAR_NAME VDOTS ty RPAREN MAPS_TO lambda
{ LFun ($3, $5, $8) }
| EXF LPAREN VAR_NAME VDOTS ty RPAREN { LExf (LVar $3, $5) }