OCaml:实例化从另一个模块导入的参数化类型

时间:2017-12-06 21:19:52

标签: ocaml

我在一个文件中有一个包含类型type move = Move of int的模块。在另一个文件中,我打开此模块,可以通过ModuleName1.move引用类型。但是有可能在第二个文件中构造这种类型的实例,因为我必须使用Move i语法,并且因为Move参数/关键字不能真正从第二档?

这是我想要从第一个模块实例化类型的模块(它被称为Game并包含类型type move = Move of int。它在最后,在next_move函数中,我想要构造一个(移动0)并将其传递给make_tree,但它不识别Move,因为它是来自另一个模块的参数化类型构造函数:

#use "sig_player.ml" ;;
#use "game.ml" ;;
    module TestAIPlayer =
        struct
          module PlayerGame = Game
          open PlayerGame

          let max_depth = 4 ;;

          (* Data Definition *)
          type tree = Node of PlayerGame.state * PlayerGame.move * tree list ;;

          let rec make_tree (root: PlayerGame.state * PlayerGame.move) (d: int): tree =
            let (s, m) = root in
            let lms = PlayerGame.legal_moves s in
            match lms, d with
            | [], _ | _, 0 -> Node (s, m, [])
            | _, _ -> Node (s, m, (List.map
                        (fun mv -> make_tree ((PlayerGame.next_state s mv), mv) (d - 1))
                        lms)) ;;

          let compare_node (n1: PlayerGame.move * float) (n2: PlayerGame.move * float)
            (comp: 'a -> 'a -> 'a): PlayerGame.move * float =
            match n1, n2 with
            | (m1, f1), (m2, f2) -> if (comp f1 f2) = f1 then n1 else n2 ;;

          let rec minimax (t: tree) (mm: bool): PlayerGame.move * float =
            match t with
            | Node (s, m, []) -> (m, PlayerGame.estimate_value s)
            | Node (s, m, children) -> let propagated = List.map
                                    (fun c -> minimax c (not mm)) children in
                                    (match mm with
                                    | true -> List.fold_right
                                              (fun x y -> compare_node x y max)
                                              propagated (m, neg_infinity)
                                    | false -> List.fold_right
                                               (fun x y -> compare_node x y min)
                                               propagated (m, infinity)) ;;

           let next_move s = minimax (make_tree (s, (Move 0)) max_depth) true ;;

        end ;;


        module AIPlayer = (TestAIPlayer : PLAYER with module PlayerGame := Game) ;;

0 个答案:

没有答案