Skip to content

Instantly share code, notes, and snippets.

@davidsan
Created January 2, 2013 16:49
Show Gist options
  • Select an option

  • Save davidsan/4436008 to your computer and use it in GitHub Desktop.

Select an option

Save davidsan/4436008 to your computer and use it in GitHub Desktop.
(* q1 : voir q2 en remplaçant alpha par int *)
(* q2 *)
class ['a] env =
object
val mutable ht = Hashtbl.create 10
method set (n:string) (v:'a) = Hashtbl.replace ht n v
method lookup n = Hashtbl.find ht n
end
(* q3 *)
let int_vars =
let e = new env in
e#set "x" 18;
e#set "y" 32;
e#set "z" 7;
e
;;
(* test *)
(*
print_int (int_vars#lookup "x")
*)
(* q4 *)
let int_ops =
let e = new env in
e#set "+" (fun a b -> a+b);
e#set "-" (fun a b -> a-b);
e#set "*" (fun a b -> a*b);
e#set "/" (fun a b -> a/b);
e;;
(* q5 *)
let float_ops =
let e = new env in
e#set "+." (fun a b -> a+.b);
e#set "-." (fun a b -> a-.b);
e#set "*." (fun a b -> a*.b);
e#set "/." (fun a b -> a/.b);
e;;
let str_ops =
let e = new env in
e#set "^" (fun a b -> a^b);
(* manque opérateurs d'associations, pas compris ce que c'était *)
e;;
let float_vars =
let e = new env in
e#set "x" 18.;
e#set "y" 32.;
e#set "z" 7.;
e
;;
let str_vars =
let e = new env in
e#set "str1" "toto";
e#set "str2" "plop";
e;;
(* q6 *)
class virtual ['a] expr =
object
method virtual to_string : unit -> string
method virtual eval : 'a env -> ('a->'a->'a) env -> 'a
end
(* q7 *)
class ['a] var n =
object(self)
inherit ['a] expr
val name = n
method to_string () = n
method eval vars ops = vars#lookup name
end
(* q8 *)
class ['a] op operator left right =
object(self)
inherit ['a] expr
val operator = operator
val left = left
val right = right
method to_string () = left#to_string()^" "^operator^" "^right#to_string()
method eval vars ops = (ops#lookup operator) (left#eval vars ops) (right#eval vars ops)
end
(* q9 *)
(* réponse : classe virtuelle car la méthode to_string est en fonction du type de la const
eg. si type int alors to_string() = string_of_int ...
si type float alors to_string() = string_of_float ... etc
*)
class virtual ['a] const x = object
inherit ['a] expr
method virtual to_string : unit -> string
method eval vars ops = x
end
(* q10 *)
class iconst x = object
inherit [int] const x
val value = x
method to_string() = string_of_int value
end
class fconst x = object
inherit [float] const x
val value = x
method to_string() = string_of_float value
end
class sconst x = object
inherit [string] const x
val value = x
method to_string() = value
end
(* q11 *)
(*
réponse :
en 2 mots, typage Caml
*)
(* q12 *)
let expr_float_with_vars = new op "+." (new fconst 2.0) (new var "x");;
Printf.printf
"%s = %f\n"
(expr_float_with_vars#to_string())
(expr_float_with_vars#eval float_vars float_ops);;
(*
expected output :
2. +. x = 20.000000
*)
(* q13 *)
let expr_string_with_vars = new op "^" (new sconst "apple") (new sconst "pie");;
Printf.printf
"%s = %s\n"
(expr_string_with_vars#to_string())
(expr_string_with_vars#eval str_vars str_ops);;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment